83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
package cmd
|
|
|
|
import (
|
|
"log/slog"
|
|
"time"
|
|
|
|
"github.com/spf13/cobra"
|
|
"trankilou.fr/lassistanoque/backend/internal/adapter/auth/password"
|
|
"trankilou.fr/lassistanoque/backend/internal/adapter/database"
|
|
"trankilou.fr/lassistanoque/backend/internal/adapter/file"
|
|
"trankilou.fr/lassistanoque/backend/internal/adapter/security"
|
|
"trankilou.fr/lassistanoque/backend/internal/http"
|
|
"trankilou.fr/lassistanoque/backend/internal/service/auth"
|
|
"trankilou.fr/lassistanoque/backend/internal/service/storage"
|
|
"trankilou.fr/lassistanoque/backend/internal/service/user"
|
|
)
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(
|
|
newServeCmd(),
|
|
)
|
|
}
|
|
|
|
func newServeCmd() *cobra.Command {
|
|
var short bool
|
|
cmd := &cobra.Command{
|
|
Use: "serve",
|
|
Short: "Run server",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
runServe()
|
|
},
|
|
}
|
|
cmd.Flags().BoolVarP(&short, "short", "s", false, "show only version number")
|
|
return cmd
|
|
}
|
|
|
|
func runServe() {
|
|
// database
|
|
db, err := database.GetDatabase()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer db.Close()
|
|
|
|
err = db.Migrate()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// storage
|
|
store, err := file.GetStorageProvider(db.FileRepository())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// Adapters
|
|
tokenManager := security.NewJwtTokenManager(12*time.Hour, 7*24*time.Hour, "lassistanoque")
|
|
pwdAuth := password.NewPasswordAuthenticator(tokenManager, db.UserRepository())
|
|
|
|
// services
|
|
authService := auth.NewService(
|
|
db.SettingsRepository(),
|
|
db.UserRepository(),
|
|
map[string]auth.Authenticator{
|
|
"password": pwdAuth,
|
|
},
|
|
)
|
|
userService := user.NewService(db.UserRepository())
|
|
storageService := storage.NewService(store)
|
|
|
|
// http server
|
|
router := http.NewRouter(http.Dependencies{
|
|
StorageService: storageService,
|
|
AuthService: authService,
|
|
UserService: userService,
|
|
TokenManager: tokenManager,
|
|
})
|
|
|
|
if err := router.Start(); err != nil {
|
|
slog.Error("failed to start server", "error", err)
|
|
}
|
|
}
|