all repos — searchix @ 584d295c25697c19ffd369d22281f7d53a139f18

Search engine for NixOS, nix-darwin, home-manager and NUR users

feat: allow HTTP request logging to be disabled
Alan Pearce alan@alanpearce.eu
Mon, 20 Jan 2025 10:24:23 +0100
commit

584d295c25697c19ffd369d22281f7d53a139f18

parent

19d8046f99d9f395f8529886f37292d99dc1a4d9

M defaults.tomldefaults.toml
@@ -17,6 +17,8 @@ # Affects logging parameters. One of 'development' or 'production' Environment = 'development'
 # Content to add to HTML <head>. 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]
M internal/config/default.gointernal/config/default.go
@@ -45,6 +45,7 @@ ), 			"x-content-type-options": "nosniff",
 			"x-frame-options":        "DENY",
 		},
+		LogRequests: true,
 	},
 	Importer: &Importer{
 		LowMemory: false,
M internal/config/structs.gointernal/config/structs.go
@@ -25,6 +25,7 @@ SentryDSN             string            `comment:"If set, will send server errors to Sentry"` 	Environment           string            `comment:"Affects logging parameters. One of 'development' or 'production'"`
 	ExtraHeadHTML         string            `comment:"Content to add to HTML <head>. 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 {
M internal/server/logging.gointernal/server/logging.go
@@ -42,9 +42,14 @@ 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 == "" {
M internal/server/mux.gointernal/server/mux.go
@@ -389,6 +389,7 @@ AddHeadersMiddleware( 			wrapHandlerWithLogging(mux, wrappedHandlerOptions{
 				defaultHostname: cfg.Web.BaseURL.Hostname(),
 				logger:          log,
+				enabled:         cfg.Web.LogRequests,
 			}),
 			cfg,
 		),
M nix/modules/default.nixnix/modules/default.nix
@@ -130,6 +130,12 @@ description = "Optionally enable sentry to track errors.";                   default = "";
                 };
 
+                logRequests = mkOption {
+                  type = types.bool;
+                  description = "Whether to log HTTP requests";
+                  default = false;
+                };
+
                 contentSecurityPolicy = mkOption {
                   type = types.submodule {
                     freeformType = settingsFormat.type;