Initial commit: Custom Start Page application with authentication and DynamoDB storage
This commit is contained in:
45
cmd/server/init_tables.go
Normal file
45
cmd/server/init_tables.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"custom-start-page/internal/storage"
|
||||
)
|
||||
|
||||
// InitializeTables creates all required DynamoDB tables
|
||||
func InitializeTables() error {
|
||||
ctx := context.Background()
|
||||
|
||||
// Get DynamoDB endpoint from environment (for local development)
|
||||
endpoint := os.Getenv("DYNAMODB_ENDPOINT")
|
||||
if endpoint == "" {
|
||||
endpoint = "http://localhost:8000" // Default for DynamoDB local
|
||||
}
|
||||
|
||||
// Create DynamoDB client
|
||||
db, err := storage.NewDynamoDBClient(ctx, endpoint)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create DynamoDB client: %w", err)
|
||||
}
|
||||
|
||||
log.Println("Creating Users table...")
|
||||
if err := db.CreateUsersTable(ctx); err != nil {
|
||||
return fmt.Errorf("failed to create Users table: %w", err)
|
||||
}
|
||||
log.Println("Users table created successfully")
|
||||
|
||||
// TODO: Add other table creation calls here as they are implemented
|
||||
// - Pages table
|
||||
// - Widgets table
|
||||
// - Bookmarks table
|
||||
// - Notes table
|
||||
// - TagAssociations table
|
||||
// - Groups table
|
||||
// - SharedItems table
|
||||
// - Preferences table
|
||||
|
||||
return nil
|
||||
}
|
||||
99
cmd/server/main.go
Normal file
99
cmd/server/main.go
Normal file
@@ -0,0 +1,99 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user