detached 5a42572

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
5a4257277048a3de2f8f9402a206a4a5ad76c9e0
author
bamen · · 2025-03-30 16:10 UTC
parent
21cfe2a
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 {
2929 // Language detection
3030 e.Use(middleware.LanguageDetection)
3131
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+
3251 AdminPath := a.Config.Admin.Path
3352 log.Debug("Binding login routes with AdminPath", "path", AdminPath)
3453 bindLoginRoute(e, a, AdminPath)
@@ -54,27 +73,6 @@ func RegisterRoutes(e *echo.Echo, a *server.App) *echo.Echo {
5473 log.Debug("Binding admin routes with AdminPath", "path", AdminPath)
5574 bindAdminRoute(e, a, AdminPath)
5675
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-
7876 // Only bind HTMX endpoints if Admin WebUI is enabled
7977 if a.Config.Admin.IsAdminWebUIEnabled() {
8078 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 detection29 // 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.Path51 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 enabled76 // 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")

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close