diff --git a/cmd/ratchetd/main.go b/cmd/ratchetd/main.go index e13483f..d928daf 100644 --- a/cmd/ratchetd/main.go +++ b/cmd/ratchetd/main.go @@ -44,7 +44,7 @@ func run(ctx context.Context, w io.Writer, args []string) error { // DEPENDENCY INJECTION FOR HANDLERS // Setup Logging - logger := logging.InitLogging(*logLevel, false) + logger := logging.InitLogging(*logLevel, w, false) // Setup DB Connection Pool db, err := rdb.OpenSqlite3DB(*dbPath) @@ -101,7 +101,6 @@ func run(ctx context.Context, w io.Writer, args []string) error { } // START SERVING REQUESTS - slog.Debug("Herp dirp!") slog.Info(fmt.Sprintf("Listening on http://%s:%s", *addr, *port)) //log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%s", *addr, *port), server)) // there is no log.Fatal equivalent. This is an approximation of the behavior diff --git a/internal/logging/logging.go b/internal/logging/logging.go index fbacd13..7169473 100644 --- a/internal/logging/logging.go +++ b/internal/logging/logging.go @@ -1,6 +1,7 @@ package logging import ( + "io" "log/slog" "os" "strings" @@ -22,7 +23,7 @@ func parseLogLevel(levelStr string) slog.Level { } // InitLogggin initializes global structured logging for the entire application -func InitLogging(level string, addSource bool) *slog.Logger { +func InitLogging(level string, w io.Writer, addSource bool) *slog.Logger { // Use os.Stderr // // Stderr is used for diagnostics and logging. Stdout is used for program diff --git a/internal/server/middleware.go b/internal/server/middleware.go index e98a9f6..51f7c3c 100644 --- a/internal/server/middleware.go +++ b/internal/server/middleware.go @@ -167,3 +167,13 @@ func AuthenticateMiddleware(next http.Handler, sm *scs.SessionManager, userServi }) } + +// CacheHeaders is a middleware that provides cache-control headers to be used +// for static assests. +func CacheHeaders(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // TODO parameterize cache control via config + w.Header().Set("Cache-Control", "private, max-age=21600") + next.ServeHTTP(w, r) + }) +} diff --git a/internal/server/routes.go b/internal/server/routes.go index ec1c8ba..c16f80d 100644 --- a/internal/server/routes.go +++ b/internal/server/routes.go @@ -27,7 +27,7 @@ func (a *RatchetApp) Routes() http.Handler { // Subtree pattern for static assets // This line was superceded by using the embedded filesystem // router.Handle("GET /static/", http.StripPrefix("/static/", fileServer)) - router.Handle("GET /static/", http.FileServerFS(ui.Files)) + router.Handle("GET /static/", CacheHeaders(http.FileServerFS(ui.Files))) router.Handle("GET /ping", PingHandler())