package cmd import ( "context" "fmt" "log" "net/http" "os" "os/signal" "syscall" "github.com/spf13/viper" "otpm/config" "otpm/database" "otpm/handlers" "otpm/models" "otpm/server" "otpm/services" ) func init() { // Set config file with multi-environment support viper.SetConfigName("config") viper.SetConfigType("yaml") viper.AddConfigPath(".") // Set environment specific config (e.g. config.production.yaml) env := os.Getenv("OTPM_ENV") if env != "" { viper.SetConfigName(fmt.Sprintf("config.%s", env)) } // Set default values viper.SetDefault("server.port", "8080") viper.SetDefault("server.timeout.read", "15s") viper.SetDefault("server.timeout.write", "15s") viper.SetDefault("server.timeout.idle", "60s") viper.SetDefault("database.max_open_conns", 25) viper.SetDefault("database.max_idle_conns", 5) viper.SetDefault("database.conn_max_lifetime", "5m") // Set environment variable prefix viper.SetEnvPrefix("OTPM") viper.AutomaticEnv() // Bind environment variables viper.BindEnv("database.url", "OTPM_DB_URL") viper.BindEnv("database.password", "OTPM_DB_PASSWORD") } // Execute is the entry point for the application func Execute() error { // Load configuration cfg, err := config.LoadConfig() if err != nil { return fmt.Errorf("failed to load config: %w", err) } // Create context with cancellation ctx, cancel := context.WithCancel(context.Background()) defer cancel() // Setup signal handling sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) go func() { sig := <-sigChan log.Printf("Received signal: %v", sig) cancel() }() // Initialize database db, err := database.New(&cfg.Database) if err != nil { return fmt.Errorf("failed to initialize database: %w", err) } defer db.Close() // Run database migrations if err := database.MigrateWithContext(ctx, db.DB, cfg.Database.SkipMigration); err != nil { return fmt.Errorf("failed to run migrations: %w", err) } // Initialize repositories userRepo := models.NewUserRepository(db.DB) otpRepo := models.NewOTPRepository(db.DB) // Initialize services authService := services.NewAuthService(cfg, userRepo) otpService := services.NewOTPService(otpRepo) // Initialize handlers authHandler := handlers.NewAuthHandler(authService) otpHandler := handlers.NewOTPHandler(otpService) // Create and configure server srv := server.New(cfg) // Register health check endpoint srv.RegisterHealthCheck() // Register public routes with type conversion authRoutes := make(map[string]http.Handler) for path, handler := range authHandler.Routes() { authRoutes[path] = http.HandlerFunc(handler) } srv.RegisterRoutes(authRoutes) // Register authenticated routes with type conversion otpRoutes := make(map[string]http.Handler) for path, handler := range otpHandler.Routes() { otpRoutes[path] = http.HandlerFunc(handler) } srv.RegisterAuthRoutes(otpRoutes) // Start server in goroutine serverErr := make(chan error, 1) go func() { log.Printf("Starting server on %s:%d", cfg.Server.Host, cfg.Server.Port) if err := srv.Start(); err != nil { serverErr <- fmt.Errorf("server error: %w", err) } }() // Wait for shutdown signal or server error select { case err := <-serverErr: return err case <-ctx.Done(): // Graceful shutdown with timeout log.Println("Shutting down server...") if err := srv.Shutdown(); err != nil { return fmt.Errorf("server shutdown error: %w", err) } log.Println("Server stopped gracefully") } return nil }