package main import ( "context" "fmt" "log" "net/http" "custom-start-page/internal/auth" "custom-start-page/internal/handlers" "custom-start-page/internal/middleware" "custom-start-page/internal/storage" "custom-start-page/pkg/config" ) func main() { // Load configuration cfg, err := config.Load() if err != nil { log.Fatalf("Failed to load configuration: %v", err) } // Initialize DynamoDB client ctx := context.Background() dbEndpoint := "" if cfg.Database.UseLocalDB { dbEndpoint = cfg.Database.Endpoint } dbClient, err := storage.NewDynamoDBClient(ctx, dbEndpoint) if err != nil { log.Fatalf("Failed to create DynamoDB client: %v", err) } // Create Users table if it doesn't exist if err := dbClient.CreateUsersTable(ctx); err != nil { log.Fatalf("Failed to create Users table: %v", err) } // Initialize repositories userRepo := storage.NewUserRepository(dbClient, "Users") // Initialize auth services stateStore := auth.NewMemoryStateStore() oauthService := auth.NewOAuthService( cfg.OAuth.Google.ClientID, cfg.OAuth.Google.ClientSecret, cfg.OAuth.Google.RedirectURL, stateStore, ) userService := auth.NewUserService(userRepo) sessionStore := auth.NewCookieSessionStore(cfg.Session.SecretKey, cfg.Session.MaxAge) // Initialize handlers authHandler := handlers.NewAuthHandler(oauthService, userService, sessionStore) dashboardHandler := handlers.NewDashboardHandler() // Setup routes mux := http.NewServeMux() // Static files fs := http.FileServer(http.Dir("static")) mux.Handle("/static/", http.StripPrefix("/static/", fs)) // Health check mux.HandleFunc("GET /health", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte("OK")) }) // Auth routes mux.HandleFunc("GET /login", authHandler.HandleLogin) mux.HandleFunc("GET /auth/oauth/{provider}", authHandler.HandleOAuthInitiate) mux.HandleFunc("GET /auth/callback/{provider}", authHandler.HandleOAuthCallback) mux.HandleFunc("POST /logout", authHandler.HandleLogout) // Root redirect mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) { // Check if user is logged in if userID, err := sessionStore.GetUserID(r); err == nil && userID != "" { http.Redirect(w, r, "/dashboard", http.StatusSeeOther) return } http.Redirect(w, r, "/login", http.StatusSeeOther) }) // Create auth middleware requireAuth := middleware.RequireAuth(sessionStore) // Protected dashboard route mux.Handle("GET /dashboard", requireAuth(http.HandlerFunc(dashboardHandler.HandleDashboard))) // Start server addr := fmt.Sprintf("%s:%s", cfg.Server.Host, cfg.Server.Port) log.Printf("Starting server on %s", addr) if err := http.ListenAndServe(addr, mux); err != nil { log.Fatalf("Server failed to start: %v", err) } }