Files
minstrel/internal/server/requestlog.go
T
bvandeusenandClaude Opus 5.5 e1f270362d
release / go (push) Successful in 2m7s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 4m30s
release / android (push) Successful in 4m39s
release / Build signed APK (push) Successful in 5m10s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
chore(release): everything that ships points at GitHub and GHCR (#7897)
- Module path: github.com/Fabled-Sword/minstrel, in go.mod, every
  import and the Dockerfile's version ldflags.
- buildinfo.ProjectURL, the contact outside services see in Minstrel's
  User-Agent: https://github.com/Fabled-Sword/minstrel.
- README's compose example pulls ghcr.io/fabled-sword/minstrel:latest.
- docs/security.md sends problem reports to the GitHub issues.
- The demo-library stack builds Minstrel from the checkout, since :dev
  is not published to GHCR.
- hygiene.go's same-site example uses example.com names.

The workbench CI (.gitea/, ci/, export-ignored) still builds and
publishes :dev to the private registry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 19:28:16 -05:00

69 lines
2.3 KiB
Go

package server
import (
"log/slog"
"net/http"
"time"
"github.com/go-chi/chi/v5/middleware"
"github.com/Fabled-Sword/minstrel/internal/auth"
)
// requestLog is an slog-based access log middleware. chi ships
// middleware.Logger but it writes to the standard library's log package,
// not slog, so output ordering and formatting drift from the rest of the
// server's logs. We use a thin wrapper instead.
//
// The /healthz path is skipped because the docker-compose healthcheck
// hits it every 5s and would otherwise drown real signals (~17k lines/day).
//
// Severity is keyed off the response status so 4xx/5xx surface even when
// the logger level is set above Info.
//
// The `remote` attribute holds the address resolved through the configured
// reverse-proxy depth, NOT the raw socket peer. Behind a proxy — the normal
// deployment for anything public — the socket peer is the proxy, so every
// line would carry the same useless address, and the access log would
// disagree with the Active-sessions list about who connected.
//
// trustedHops is a func because this middleware is constructed at boot while
// the value is editable at runtime, and — since Router() registers
// this before it builds the settings service — because it lets the accessor
// be wired before the thing it reads exists. auth.ClientIP tolerates a depth
// of 0, which is what a nil service reports.
func requestLog(logger *slog.Logger, trustedHops func() int) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/healthz" {
next.ServeHTTP(w, r)
return
}
start := time.Now()
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
next.ServeHTTP(ww, r)
status := ww.Status()
hops := 0
if trustedHops != nil {
hops = trustedHops()
}
attrs := []any{
"method", r.Method,
"path", r.URL.Path,
"status", status,
"duration_ms", time.Since(start).Milliseconds(),
"request_id", middleware.GetReqID(r.Context()),
"remote", auth.ClientIP(r, hops),
}
switch {
case status >= 500:
logger.Error("http", attrs...)
case status >= 400:
logger.Warn("http", attrs...)
default:
logger.Info("http", attrs...)
}
})
}
}