Add basic cache control for static assets
continuous-integration/drone/push Build is passing Details

main
Drew Bednar 2 months ago
parent 514ba0cba3
commit 98ba3cc681

@ -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

@ -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

@ -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)
})
}

@ -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())

Loading…
Cancel
Save