From 584d295c25697c19ffd369d22281f7d53a139f18 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Mon, 20 Jan 2025 10:24:23 +0100 Subject: feat: allow HTTP request logging to be disabled --- defaults.toml | 2 ++ internal/config/default.go | 1 + internal/config/structs.go | 1 + internal/server/logging.go | 5 +++++ internal/server/mux.go | 1 + nix/modules/default.nix | 6 ++++++ 6 files changed, 16 insertions(+) diff --git a/defaults.toml b/defaults.toml index bfd2efa..5b417b1 100644 --- a/defaults.toml +++ b/defaults.toml @@ -17,6 +17,8 @@ SentryDSN = '' Environment = 'development' # Content to add to HTML . Can be used to override styling, add scripts, etc. ExtraHeadHTML = '' +# Whether to log incoming HTTP requests +LogRequests = true # Content-Security-Policy header to send with requests. Should only need changing if ExtraHeadHTML is used. [Web.ContentSecurityPolicy] diff --git a/internal/config/default.go b/internal/config/default.go index 5202678..752d4e3 100644 --- a/internal/config/default.go +++ b/internal/config/default.go @@ -45,6 +45,7 @@ var DefaultConfig = Config{ "x-content-type-options": "nosniff", "x-frame-options": "DENY", }, + LogRequests: true, }, Importer: &Importer{ LowMemory: false, diff --git a/internal/config/structs.go b/internal/config/structs.go index e73425b..8c69733 100644 --- a/internal/config/structs.go +++ b/internal/config/structs.go @@ -25,6 +25,7 @@ type Web struct { Environment string `comment:"Affects logging parameters. One of 'development' or 'production'"` ExtraHeadHTML string `comment:"Content to add to HTML . Can be used to override styling, add scripts, etc."` Headers map[string]string `comment:"Extra headers to send with HTTP requests"` + LogRequests bool `comment:"Whether to log incoming HTTP requests"` } type Importer struct { diff --git a/internal/server/logging.go b/internal/server/logging.go index 6e2f7c8..5930b9d 100644 --- a/internal/server/logging.go +++ b/internal/server/logging.go @@ -42,9 +42,14 @@ func NewLoggingResponseWriter(w http.ResponseWriter) *LoggingResponseWriter { type wrappedHandlerOptions struct { defaultHostname string logger *log.Logger + enabled bool } func wrapHandlerWithLogging(wrappedHandler http.Handler, opts wrappedHandlerOptions) http.Handler { + if !opts.enabled { + return wrappedHandler + } + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { scheme := r.Header.Get("X-Forwarded-Proto") if scheme == "" { diff --git a/internal/server/mux.go b/internal/server/mux.go index 0403470..9dbcc86 100644 --- a/internal/server/mux.go +++ b/internal/server/mux.go @@ -389,6 +389,7 @@ func NewMux( wrapHandlerWithLogging(mux, wrappedHandlerOptions{ defaultHostname: cfg.Web.BaseURL.Hostname(), logger: log, + enabled: cfg.Web.LogRequests, }), cfg, ), diff --git a/nix/modules/default.nix b/nix/modules/default.nix index b3a0211..fdd2977 100644 --- a/nix/modules/default.nix +++ b/nix/modules/default.nix @@ -130,6 +130,12 @@ in default = ""; }; + logRequests = mkOption { + type = types.bool; + description = "Whether to log HTTP requests"; + default = false; + }; + contentSecurityPolicy = mkOption { type = types.submodule { freeformType = settingsFormat.type; -- cgit 1.4.1