Add caching to static files based on etag

This commit is contained in:
Maximilian Friedersdorff 2025-06-27 20:39:17 +01:00
parent f2e52a18e5
commit 06495fa358
2 changed files with 16 additions and 19 deletions

View file

@ -6,8 +6,10 @@ import (
"net"
"net/http"
"os"
"time"
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
"forgejo.gwairfelin.com/max/gonotes/internal/middleware"
"forgejo.gwairfelin.com/max/gonotes/internal/notes/views"
)
@ -24,12 +26,22 @@ func main() {
router := http.NewServeMux()
notesRouter := views.GetRoutes("/notes")
cacheExpiration, err := time.ParseDuration("24h")
if err != nil {
log.Fatal(err)
}
etag := middleware.NewETag("static", cacheExpiration)
router.Handle("/", logger(http.RedirectHandler("/notes/", http.StatusFound)))
router.Handle("/notes/", logger(http.StripPrefix("/notes", notesRouter)))
router.Handle(
"/static/",
logger(
http.FileServer(http.FS(conf.Static)),
middleware.StaticEtagMiddleware(
*etag,
http.FileServer(http.FS(conf.Static)),
),
),
)