feat: simplify architecture by removing cache layer and centralizing routes
- Removed over-engineered cache system for static CV data that only changes on deployment - Extracted all route configuration to internal/routes/routes.go for better organization - Implemented rate limiting and cache control middleware for PDF endpoint protection
This commit is contained in:
@@ -13,12 +13,11 @@ import (
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/juanatsap/cv-site/internal/config"
|
||||
"github.com/juanatsap/cv-site/internal/handlers"
|
||||
"github.com/juanatsap/cv-site/internal/middleware"
|
||||
"github.com/juanatsap/cv-site/internal/models"
|
||||
"github.com/juanatsap/cv-site/internal/routes"
|
||||
"github.com/juanatsap/cv-site/internal/templates"
|
||||
)
|
||||
|
||||
const version = "1.0.0"
|
||||
const version = "1.1.0"
|
||||
|
||||
func main() {
|
||||
// Initialize logger
|
||||
@@ -36,29 +35,6 @@ func main() {
|
||||
cfg := config.Load()
|
||||
log.Printf("✓ Configuration loaded (env: %s)", os.Getenv("GO_ENV"))
|
||||
|
||||
// Initialize cache (1 hour TTL, configurable via env)
|
||||
cacheTTL := 1 * time.Hour
|
||||
if ttlEnv := os.Getenv("CACHE_TTL_MINUTES"); ttlEnv != "" {
|
||||
if minutes, err := time.ParseDuration(ttlEnv + "m"); err == nil {
|
||||
cacheTTL = minutes
|
||||
}
|
||||
}
|
||||
models.InitCache(cacheTTL)
|
||||
|
||||
// Warm cache with default languages
|
||||
log.Println("🔥 Warming cache...")
|
||||
for _, lang := range []string{"en", "es"} {
|
||||
// Warm CV cache
|
||||
if _, err := models.LoadCV(lang); err != nil {
|
||||
log.Printf("⚠️ Failed to warm CV cache for %s: %v", lang, err)
|
||||
}
|
||||
// Warm UI cache
|
||||
if _, err := models.LoadUI(lang); err != nil {
|
||||
log.Printf("⚠️ Failed to warm UI cache for %s: %v", lang, err)
|
||||
}
|
||||
}
|
||||
log.Printf("✓ Cache warmed (TTL: %v)", cacheTTL)
|
||||
|
||||
// Initialize template manager
|
||||
templateMgr, err := templates.NewManager(&cfg.Template)
|
||||
if err != nil {
|
||||
@@ -69,37 +45,8 @@ func main() {
|
||||
cvHandler := handlers.NewCVHandler(templateMgr, cfg.Address())
|
||||
healthHandler := handlers.NewHealthHandler(version)
|
||||
|
||||
// Setup router
|
||||
mux := http.NewServeMux()
|
||||
|
||||
// Create rate limiter for PDF endpoint
|
||||
// Allow 3 PDF generations per minute per IP
|
||||
pdfRateLimiter := middleware.NewRateLimiter(3, 1*time.Minute)
|
||||
log.Printf("🔒 Rate limiter enabled for PDF endpoint (3 requests/minute)")
|
||||
|
||||
// Routes
|
||||
mux.HandleFunc("/", cvHandler.Home)
|
||||
mux.HandleFunc("/cv", cvHandler.CVContent)
|
||||
mux.HandleFunc("/health", healthHandler.Check)
|
||||
|
||||
// Protected PDF endpoint with origin checking + rate limiting
|
||||
protectedPDFHandler := middleware.OriginChecker(
|
||||
pdfRateLimiter.Middleware(
|
||||
http.HandlerFunc(cvHandler.ExportPDF),
|
||||
),
|
||||
)
|
||||
mux.Handle("/export/pdf", protectedPDFHandler)
|
||||
|
||||
// Static files with cache control
|
||||
staticHandler := http.StripPrefix("/static/", http.FileServer(http.Dir("static")))
|
||||
mux.Handle("/static/", cacheControl(staticHandler))
|
||||
|
||||
// Apply comprehensive middleware chain
|
||||
handler := middleware.Recovery(
|
||||
middleware.Logger(
|
||||
middleware.SecurityHeaders(mux),
|
||||
),
|
||||
)
|
||||
// Setup routes and middleware
|
||||
handler := routes.Setup(cvHandler, healthHandler)
|
||||
|
||||
// Create server with timeouts
|
||||
server := &http.Server{
|
||||
@@ -152,17 +99,3 @@ func main() {
|
||||
log.Println("✓ Server stopped gracefully")
|
||||
}
|
||||
}
|
||||
|
||||
// cacheControl adds cache headers to static files
|
||||
func cacheControl(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Cache static files for 1 hour in development, 1 day in production
|
||||
maxAge := "3600" // 1 hour
|
||||
if os.Getenv("GO_ENV") == "production" {
|
||||
maxAge = "86400" // 1 day
|
||||
}
|
||||
|
||||
w.Header().Set("Cache-Control", "public, max-age="+maxAge)
|
||||
h.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user