Files
Kiro/cmd/server/main.go
Daniel Romischer 299ac03939 Complete tasks 4.1-4.2: Page management service and HTTP endpoints
- Implemented PageService with full CRUD operations
- Added GetPages, CreatePage, UpdatePage, DeletePage, ReorderPages methods
- Cascade deletion of widgets when page is deleted
- Prevention of last page deletion
- Created page HTTP endpoints (GET, POST, PUT, DELETE, reorder)
- HTMX-friendly HTML fragment responses
- Comprehensive unit tests for service and handlers
- Updated dashboard to use PageService and create default pages
2026-02-19 00:08:05 -05:00

122 lines
3.7 KiB
Go

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/services"
"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)
}
// Create Pages table if it doesn't exist
if err := dbClient.CreatePagesTable(ctx); err != nil {
log.Fatalf("Failed to create Pages table: %v", err)
}
// Create Widgets table if it doesn't exist
if err := dbClient.CreateWidgetsTable(ctx); err != nil {
log.Fatalf("Failed to create Widgets table: %v", err)
}
// Initialize repositories
userRepo := storage.NewUserRepository(dbClient, "Users")
// Initialize services
pageService := services.NewPageService(dbClient)
// 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(pageService)
pageHandler := handlers.NewPageHandler(pageService)
// 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)))
// Protected page routes
mux.Handle("GET /pages/{id}", requireAuth(http.HandlerFunc(pageHandler.HandleGetPage)))
mux.Handle("POST /pages", requireAuth(http.HandlerFunc(pageHandler.HandleCreatePage)))
mux.Handle("PUT /pages/{id}", requireAuth(http.HandlerFunc(pageHandler.HandleUpdatePage)))
mux.Handle("DELETE /pages/{id}", requireAuth(http.HandlerFunc(pageHandler.HandleDeletePage)))
mux.Handle("POST /pages/reorder", requireAuth(http.HandlerFunc(pageHandler.HandleReorderPages)))
// 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)
}
}