Added access log to server and modified mock-fts to use it

This commit is contained in:
bwilliams committed 2026-10-16 08:38:41 +01:00
1 parent 2490519c2b
commit 02e3d910a2
10 files changed
+389 -29

No files matched your search

+21 -4
View File
@@ -6,6 +6,7 @@ import (
"crypto/tls"
"errors"
"fmt"
"log"
"log/slog"
"net"
"net/http"
@@ -17,6 +18,7 @@ import (
"github.com/pires/go-proxyproto"
"git.avsdev.uk/Go/job-search/internal/config"
"git.avsdev.uk/Go/job-search/internal/logging"
"git.avsdev.uk/Go/job-search/internal/middleware"
"git.avsdev.uk/Go/job-search/internal/util"
"git.avsdev.uk/Go/job-search/internal/websocket"
@@ -49,6 +51,16 @@ func WithLogger(logger *slog.Logger) Option {
}
}
// WithAccessLog makes the server write a line to logger for every request, in
// the Combined Log Format (see logging.AccessLog), as well as the request
// lines it logs with slog. The line is written when the request ends, and
// covers requests that middleware refuses.
func WithAccessLog(logger *log.Logger) Option {
return func(s *Server) {
s.accessLog = logger
}
}
// WithMiddleware appends middleware to a group. This is the generic hook
// (auth, metrics, tracing, custom header rules, ...).
func WithMiddleware(group Group, middlewares ...Middleware) Option {
@@ -82,10 +94,12 @@ type limiterHook struct {
// Server is the application server.
type Server struct {
conf *config.JSCServer
log *slog.Logger
hooks map[Group][]Middleware
limiters map[Group]limiterHook
conf *config.JSCServer
log *slog.Logger
// accessLog is where the access log is written, or nil for none.
accessLog *log.Logger
hooks map[Group][]Middleware
limiters map[Group]limiterHook
// trusted are the proxies whose forwarding headers are believed.
trusted []netip.Prefix
@@ -156,6 +170,9 @@ func New(conf *config.JSCServer, opts ...Option) (*Server, error) {
server.backgroundCancel()
return nil, err
}
if server.accessLog != nil {
server.handler = logging.AccessLog(server.accessLog, true, server.handler)
}
return server, nil
}