refactor: Improve static file serving and caching middleware
- Reintroduced static file serving under the `/assets/*` path with enhanced cache control based on the environment. - Added middleware to set appropriate cache headers for production and development environments. - Improved logging for static route binding to aid in debugging and clarity.
- commit
- 4af5e8696ca4b212709382be0a523baaf079d411
- author
- bamen · · 2025-03-30 16:10 UTC
- parent
- 283cc35
- signature
- unsigned
1 file · +19 -21
M internal/httpserve/router.go +19 -21
| @@ -29,6 +29,25 @@ func RegisterRoutes(e *echo.Echo, a *server.App) *echo.Echo { | |||
| 29 | 29 | // Language detection | |
| 30 | 30 | e.Use(middleware.LanguageDetection) | |
| 31 | 31 | ||
| 32 | + | log.Debug("Binding static routes under /assets/*") | |
| 33 | + | // Middleware to set cache headers for static files | |
| 34 | + | staticCacheMiddleware := func(next echo.HandlerFunc) echo.HandlerFunc { | |
| 35 | + | return func(c echo.Context) error { | |
| 36 | + | // Determine caching based on environment | |
| 37 | + | if os.Getenv("RUN_ENV") != "dev" { | |
| 38 | + | c.Response().Header().Set("Cache-Control", "public, max-age=86400") // Cache for 1 day in production | |
| 39 | + | } else { | |
| 40 | + | c.Response().Header().Set("Cache-Control", "no-cache") // No caching in development | |
| 41 | + | } | |
| 42 | + | return next(c) | |
| 43 | + | } | |
| 44 | + | } | |
| 45 | + | // Use Echo's static file middleware to serve assets directly from the filesystem. | |
| 46 | + | // The path is relative to the project root. | |
| 47 | + | staticGroup := e.Group("/assets") | |
| 48 | + | staticGroup.Use(staticCacheMiddleware) | |
| 49 | + | staticGroup.Static("/", "internal/webui/public/assets") | |
| 50 | + | ||
| 32 | 51 | AdminPath := a.Config.Admin.Path | |
| 33 | 52 | log.Debug("Binding login routes with AdminPath", "path", AdminPath) | |
| 34 | 53 | bindLoginRoute(e, a, AdminPath) | |
| @@ -54,27 +73,6 @@ func RegisterRoutes(e *echo.Echo, a *server.App) *echo.Echo { | |||
| 54 | 73 | log.Debug("Binding admin routes with AdminPath", "path", AdminPath) | |
| 55 | 74 | bindAdminRoute(e, a, AdminPath) | |
| 56 | 75 | ||
| 57 | - | log.Debug("Binding static routes under /assets/*") | |
| 58 | - | ||
| 59 | - | // Middleware to set cache headers for static files | |
| 60 | - | staticCacheMiddleware := func(next echo.HandlerFunc) echo.HandlerFunc { | |
| 61 | - | return func(c echo.Context) error { | |
| 62 | - | // Determine caching based on environment | |
| 63 | - | if os.Getenv("RUN_ENV") != "dev" { | |
| 64 | - | c.Response().Header().Set("Cache-Control", "public, max-age=86400") // Cache for 1 day in production | |
| 65 | - | } else { | |
| 66 | - | c.Response().Header().Set("Cache-Control", "no-cache") // No caching in development | |
| 67 | - | } | |
| 68 | - | return next(c) | |
| 69 | - | } | |
| 70 | - | } | |
| 71 | - | ||
| 72 | - | // Use Echo's static file middleware to serve assets directly from the filesystem. | |
| 73 | - | // The path is relative to the project root. | |
| 74 | - | staticGroup := e.Group("/assets") | |
| 75 | - | staticGroup.Use(staticCacheMiddleware) | |
| 76 | - | staticGroup.Static("/", "internal/webui/public/assets") | |
| 77 | - | ||
| 78 | 76 | // Only bind HTMX endpoints if Admin WebUI is enabled | |
| 79 | 77 | if a.Config.Admin.IsAdminWebUIEnabled() { | |
| 80 | 78 | log.Info("Admin WebUI is enabled, binding HTMX endpoints") | |
| @@ -29,6 +29,25 @@ func RegisterRoutes(e *echo.Echo, a *server.App) *echo.Echo { | |||||
| 29 | // Language detection | 29 | // Language detection | ||
| 30 | e.Use(middleware.LanguageDetection) | 30 | e.Use(middleware.LanguageDetection) | ||
| 31 | 31 | ||||
| 32 | + | log.Debug("Binding static routes under /assets/*") | |||
| 33 | + | // Middleware to set cache headers for static files | |||
| 34 | + | staticCacheMiddleware := func(next echo.HandlerFunc) echo.HandlerFunc { | |||
| 35 | + | return func(c echo.Context) error { | |||
| 36 | + | // Determine caching based on environment | |||
| 37 | + | if os.Getenv("RUN_ENV") != "dev" { | |||
| 38 | + | c.Response().Header().Set("Cache-Control", "public, max-age=86400") // Cache for 1 day in production | |||
| 39 | + | } else { | |||
| 40 | + | c.Response().Header().Set("Cache-Control", "no-cache") // No caching in development | |||
| 41 | + | } | |||
| 42 | + | return next(c) | |||
| 43 | + | } | |||
| 44 | + | } | |||
| 45 | + | // Use Echo's static file middleware to serve assets directly from the filesystem. | |||
| 46 | + | // The path is relative to the project root. | |||
| 47 | + | staticGroup := e.Group("/assets") | |||
| 48 | + | staticGroup.Use(staticCacheMiddleware) | |||
| 49 | + | staticGroup.Static("/", "internal/webui/public/assets") | |||
| 50 | + | ||||
| 32 | AdminPath := a.Config.Admin.Path | 51 | AdminPath := a.Config.Admin.Path | ||
| 33 | log.Debug("Binding login routes with AdminPath", "path", AdminPath) | 52 | log.Debug("Binding login routes with AdminPath", "path", AdminPath) | ||
| 34 | bindLoginRoute(e, a, AdminPath) | 53 | bindLoginRoute(e, a, AdminPath) | ||
| @@ -54,27 +73,6 @@ func RegisterRoutes(e *echo.Echo, a *server.App) *echo.Echo { | |||||
| 54 | log.Debug("Binding admin routes with AdminPath", "path", AdminPath) | 73 | log.Debug("Binding admin routes with AdminPath", "path", AdminPath) | ||
| 55 | bindAdminRoute(e, a, AdminPath) | 74 | bindAdminRoute(e, a, AdminPath) | ||
| 56 | 75 | ||||
| 57 | - | log.Debug("Binding static routes under /assets/*") | |||
| 58 | - | ||||
| 59 | - | // Middleware to set cache headers for static files | |||
| 60 | - | staticCacheMiddleware := func(next echo.HandlerFunc) echo.HandlerFunc { | |||
| 61 | - | return func(c echo.Context) error { | |||
| 62 | - | // Determine caching based on environment | |||
| 63 | - | if os.Getenv("RUN_ENV") != "dev" { | |||
| 64 | - | c.Response().Header().Set("Cache-Control", "public, max-age=86400") // Cache for 1 day in production | |||
| 65 | - | } else { | |||
| 66 | - | c.Response().Header().Set("Cache-Control", "no-cache") // No caching in development | |||
| 67 | - | } | |||
| 68 | - | return next(c) | |||
| 69 | - | } | |||
| 70 | - | } | |||
| 71 | - | ||||
| 72 | - | // Use Echo's static file middleware to serve assets directly from the filesystem. | |||
| 73 | - | // The path is relative to the project root. | |||
| 74 | - | staticGroup := e.Group("/assets") | |||
| 75 | - | staticGroup.Use(staticCacheMiddleware) | |||
| 76 | - | staticGroup.Static("/", "internal/webui/public/assets") | |||
| 77 | - | ||||
| 78 | // Only bind HTMX endpoints if Admin WebUI is enabled | 76 | // Only bind HTMX endpoints if Admin WebUI is enabled | ||
| 79 | if a.Config.Admin.IsAdminWebUIEnabled() { | 77 | if a.Config.Admin.IsAdminWebUIEnabled() { | ||
| 80 | log.Info("Admin WebUI is enabled, binding HTMX endpoints") | 78 | log.Info("Admin WebUI is enabled, binding HTMX endpoints") | ||