63ede5c237
- refactor: move HTTP status and DTO concerns out of model and service layers into API and handler code. - feat: add admin service boundary and route auth through services instead of direct repository access. - test: add architecture and error tests covering package boundaries, redaction, and service behavior. - docs: record the protocol-neutral boundary decision and update architecture and roadmap notes.
116 lines
3.1 KiB
Go
116 lines
3.1 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/dhao2001/mygo/internal/config"
|
|
"github.com/dhao2001/mygo/internal/repository"
|
|
"github.com/dhao2001/mygo/internal/service"
|
|
"github.com/dhao2001/mygo/internal/storage"
|
|
)
|
|
|
|
// WebApp contains application-wide runtime dependencies and metadata.
|
|
type WebApp struct {
|
|
Config *config.Config
|
|
Version string
|
|
|
|
DB *gorm.DB
|
|
UserRepo repository.UserRepository
|
|
SessionRepo repository.SessionRepository
|
|
FileRepo repository.FileRepository
|
|
CredentialRepo repository.CredentialRepository
|
|
AuthService *service.AuthService
|
|
AdminService *service.AdminService
|
|
FileService *service.FileService
|
|
Storage storage.StorageBackend
|
|
}
|
|
|
|
// Bootstrap creates a fully initialized WebApp from config.
|
|
// It opens the database, runs migrations, and wires all repositories and services.
|
|
func Bootstrap(cfg *config.Config) (*WebApp, error) {
|
|
db, err := repository.Open(cfg.Database)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("open database: %w", err)
|
|
}
|
|
|
|
if err := repository.AutoMigrate(db); err != nil {
|
|
return nil, fmt.Errorf("migrate database: %w", err)
|
|
}
|
|
|
|
userRepo := repository.NewUserRepository(db)
|
|
sessionRepo := repository.NewSessionRepository(db)
|
|
fileRepo := repository.NewFileRepository(db)
|
|
credentialRepo := repository.NewCredentialRepository(db)
|
|
|
|
jwtSecret := []byte(cfg.JWT.Secret)
|
|
authService := service.NewAuthService(
|
|
userRepo, sessionRepo, credentialRepo,
|
|
jwtSecret,
|
|
cfg.JWT.AccessTTL,
|
|
cfg.JWT.RefreshTTL,
|
|
)
|
|
|
|
store, err := storage.NewLocalStorage(cfg.Storage.Local.Path)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("init storage: %w", err)
|
|
}
|
|
|
|
fileService := service.NewFileService(fileRepo, store, cfg.Storage.MaxUploadSize, slog.Default())
|
|
adminService := service.NewAdminService(userRepo)
|
|
|
|
return &WebApp{
|
|
Config: cfg,
|
|
Version: AppVersion,
|
|
DB: db,
|
|
UserRepo: userRepo,
|
|
SessionRepo: sessionRepo,
|
|
FileRepo: fileRepo,
|
|
CredentialRepo: credentialRepo,
|
|
AuthService: authService,
|
|
AdminService: adminService,
|
|
FileService: fileService,
|
|
Storage: store,
|
|
}, nil
|
|
}
|
|
|
|
// NewWebApp creates a WebApp with pre-built dependencies (useful for testing).
|
|
func NewWebApp(cfg *config.Config, db *gorm.DB,
|
|
userRepo repository.UserRepository,
|
|
sessionRepo repository.SessionRepository,
|
|
fileRepo repository.FileRepository,
|
|
credentialRepo repository.CredentialRepository,
|
|
authService *service.AuthService,
|
|
adminService *service.AdminService,
|
|
fileService *service.FileService,
|
|
store storage.StorageBackend,
|
|
) *WebApp {
|
|
return &WebApp{
|
|
Config: cfg,
|
|
Version: AppVersion,
|
|
DB: db,
|
|
UserRepo: userRepo,
|
|
SessionRepo: sessionRepo,
|
|
FileRepo: fileRepo,
|
|
CredentialRepo: credentialRepo,
|
|
AuthService: authService,
|
|
AdminService: adminService,
|
|
FileService: fileService,
|
|
Storage: store,
|
|
}
|
|
}
|
|
|
|
// Close releases resources held by the application (e.g., database connections).
|
|
func (w *WebApp) Close() error {
|
|
if w.DB == nil {
|
|
return nil
|
|
}
|
|
sqlDB, err := w.DB.DB()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return sqlDB.Close()
|
|
}
|