feat: better caching headers for images
This commit is contained in:
@@ -1,40 +1,50 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/images"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/page"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/translate"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/rest"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/resthome"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/restimage"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/filesystem"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/middleware"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/templer"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/web"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/web/layouts"
|
||||
"github.com/studio-b12/gowebdav"
|
||||
"github.com/valkey-io/valkey-go"
|
||||
)
|
||||
|
||||
func (s *Server) RegisterRoutes() (h http.Handler) {
|
||||
func (s *Server) allRoutes() (h http.Handler, err error) {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
registerStatic(mux)
|
||||
mux.Handle("/", s.registerOther())
|
||||
|
||||
other, err := s.dynamicRoutes()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
mux.Handle("/", other)
|
||||
|
||||
h = mux
|
||||
h = templer.Middleware(h)
|
||||
h = s.loggingMiddleware(h)
|
||||
return h
|
||||
h = middleware.Logging(h)
|
||||
return h, nil
|
||||
}
|
||||
|
||||
func (s *Server) registerOther() (h http.Handler) {
|
||||
func (s *Server) dynamicRoutes() (h http.Handler, err error) {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
webdavClient := gowebdav.NewClient(s.cfg.FileSystem.URL, s.cfg.FileSystem.User, s.cfg.FileSystem.Password)
|
||||
if err := webdavClient.Connect(); err != nil {
|
||||
slog.Error("could not connect webdavClient", "err", err)
|
||||
err = webdavClient.Connect()
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not connect webdavClient: %w", err)
|
||||
return
|
||||
}
|
||||
|
||||
var fs filesystem.FS = webdavClient
|
||||
@@ -44,22 +54,27 @@ func (s *Server) registerOther() (h http.Handler) {
|
||||
Password: s.cfg.Cache.Password,
|
||||
})
|
||||
if err != nil {
|
||||
slog.Error("failed to create valkey client", "err", err)
|
||||
err = fmt.Errorf("failed to create valkey client: %w", err)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
} else {
|
||||
fs = filesystem.NewCachedClient(webdavClient, valkeyClient, 5*time.Hour)
|
||||
}
|
||||
|
||||
renderer := layouts.New(s.cfg)
|
||||
|
||||
_ = resthome.New(mux, renderer, page.New(fs))
|
||||
_ = restimage.New(mux, images.New(fs, s.cfg.Image, valkeyClient))
|
||||
|
||||
mux.Handle("/", http.FileServer(http.FS(web.GetStaticFS())))
|
||||
mux.Handle("/images/", rest.Use(
|
||||
restimage.New(images.New(fs, s.cfg.Image, valkeyClient)),
|
||||
middleware.Cache(time.Hour*24*5),
|
||||
))
|
||||
|
||||
translator := translate.New()
|
||||
renderer := layouts.New(s.cfg)
|
||||
|
||||
h = mux
|
||||
h = translator.Middleware(h)
|
||||
h = cacheMiddleware(time.Minute * 5)(h)
|
||||
return h
|
||||
mux.Handle("/", rest.Use(
|
||||
resthome.New(renderer, page.New(fs)),
|
||||
translator.Middleware,
|
||||
middleware.Cache(time.Minute),
|
||||
))
|
||||
|
||||
return mux, nil
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type captureWriter struct {
|
||||
http.ResponseWriter
|
||||
code int
|
||||
}
|
||||
|
||||
func (c *captureWriter) WriteHeader(statusCode int) {
|
||||
c.code = statusCode
|
||||
c.ResponseWriter.WriteHeader(statusCode)
|
||||
}
|
||||
|
||||
func (s *Server) loggingMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
cw := &captureWriter{
|
||||
ResponseWriter: w,
|
||||
code: 200,
|
||||
}
|
||||
next.ServeHTTP(cw, r)
|
||||
slog.Info("request",
|
||||
"method", r.Method,
|
||||
"path", r.URL.Path,
|
||||
"duration", time.Since(start),
|
||||
"status", cw.code,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
// cacheMiddleware adds Cache-Control headers to the response.
|
||||
func cacheMiddleware(maxAge time.Duration) func(next http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%.0f", maxAge.Seconds()))
|
||||
w.Header().Set("Vary", "HX-Request,Accept-Language,Accept-Encoding")
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -27,10 +27,16 @@ func Start(ctx context.Context, cfg config.Cfg) (err error) {
|
||||
cfg: cfg,
|
||||
}
|
||||
|
||||
hander, err := s.allRoutes()
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not setup routes: %w", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Declare Server config
|
||||
srv := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", s.port),
|
||||
Handler: s.RegisterRoutes(),
|
||||
Handler: hander,
|
||||
IdleTimeout: time.Minute,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 30 * time.Second,
|
||||
@@ -45,7 +51,7 @@ func Start(ctx context.Context, cfg config.Cfg) (err error) {
|
||||
// Shutdown server when ctx done
|
||||
if err = srv.Shutdown(context.Background()); err != nil {
|
||||
// Error from closing listeners, or context timeout:
|
||||
err = fmt.Errorf("error whilse shutting down server: %w", err)
|
||||
err = fmt.Errorf("error while shutting down server: %w", err)
|
||||
}
|
||||
|
||||
close(idleConnsClosed)
|
||||
@@ -53,7 +59,7 @@ func Start(ctx context.Context, cfg config.Cfg) (err error) {
|
||||
|
||||
slog.Info("starting server", "address", srv.Addr)
|
||||
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
|
||||
return fmt.Errorf("error while server ListenAndServe: %w", err)
|
||||
return fmt.Errorf("error while serving: %w", err)
|
||||
}
|
||||
|
||||
<-idleConnsClosed
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/middleware"
|
||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/web"
|
||||
)
|
||||
|
||||
@@ -14,7 +15,7 @@ func registerStatic(mux *http.ServeMux) {
|
||||
fileSystem := web.GetStaticFS()
|
||||
|
||||
// we still use go's fileServer to avoid unnecessary implementation
|
||||
fileServer := cacheMiddleware(time.Hour * 24)(http.FileServer(http.FS(fileSystem)))
|
||||
fileServer := middleware.Cache(time.Hour * 24)(http.FileServer(http.FS(fileSystem)))
|
||||
|
||||
err := fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
@@ -26,7 +27,7 @@ func registerStatic(mux *http.ServeMux) {
|
||||
|
||||
path = "/" + path
|
||||
|
||||
slog.Debug("registering file", "path", path)
|
||||
slog.Debug("registered static file", "path", path)
|
||||
|
||||
mux.Handle(path, fileServer)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user