From 023ea17492fa35fec88f769cb785d93ed6e746de Mon Sep 17 00:00:00 2001 From: crapStone Date: Thu, 12 Jun 2025 22:57:04 +0200 Subject: [PATCH] feat: add option to log every request with path and IP --- cli/flags.go | 6 ++++++ config/config.go | 1 + config/setup.go | 3 +++ config/setup_test.go | 16 ++++++++++++++++ server/handler/handler.go | 4 ++++ 5 files changed, 30 insertions(+) diff --git a/cli/flags.go b/cli/flags.go index 542b6ca..7f41d47 100644 --- a/cli/flags.go +++ b/cli/flags.go @@ -115,6 +115,12 @@ var ( EnvVars: []string{"USE_PROXY_PROTOCOL"}, Value: false, }, + &cli.BoolFlag{ + Name: "log-every-request", + Usage: "logs every request with reqID, host and path", + EnvVars: []string{"LOG_EVERY_REQUEST"}, + Value: false, + }, // Default branches to fetch assets from &cli.StringSliceFlag{ diff --git a/config/config.go b/config/config.go index 8db9336..0a226b7 100644 --- a/config/config.go +++ b/config/config.go @@ -14,6 +14,7 @@ type ServerConfig struct { HttpPort uint16 `default:"80"` HttpServerEnabled bool `default:"true"` UseProxyProtocol bool `default:"false"` + LogEveryRequest bool `default:"false"` MainDomain string RawDomain string PagesBranches []string diff --git a/config/setup.go b/config/setup.go index 0846314..b111eab 100644 --- a/config/setup.go +++ b/config/setup.go @@ -72,6 +72,9 @@ func mergeServerConfig(ctx *cli.Context, config *ServerConfig) { if ctx.IsSet("use-proxy-protocol") { config.UseProxyProtocol = ctx.Bool("use-proxy-protocol") } + if ctx.IsSet("log-every-request") { + config.LogEveryRequest = ctx.Bool("log-every-request") + } if ctx.IsSet("pages-domain") { config.MainDomain = ctx.String("pages-domain") diff --git a/config/setup_test.go b/config/setup_test.go index 6ca9712..91e82bb 100644 --- a/config/setup_test.go +++ b/config/setup_test.go @@ -140,6 +140,8 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T Port: 8080, HttpPort: 80, HttpServerEnabled: false, + UseProxyProtocol: false, + LogEveryRequest: false, MainDomain: "original", RawDomain: "original", PagesBranches: []string{"original"}, @@ -180,6 +182,8 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T Port: 8443, HttpPort: 443, HttpServerEnabled: true, + UseProxyProtocol: true, + LogEveryRequest: true, MainDomain: "changed", RawDomain: "changed", PagesBranches: []string{"changed"}, @@ -227,6 +231,8 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T "--port", "8443", "--http-port", "443", "--enable-http-server", + "--use-proxy-protocol", + "--log-every-request", // Forge "--forge-root", "changed", "--forge-api-token", "changed", @@ -277,6 +283,8 @@ func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *tes Port: 8080, HttpPort: 80, HttpServerEnabled: false, + UseProxyProtocol: false, + LogEveryRequest: false, MainDomain: "original", RawDomain: "original", AllowedCorsDomains: []string{"original"}, @@ -290,6 +298,8 @@ func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *tes Port: 8443, HttpPort: 443, HttpServerEnabled: true, + UseProxyProtocol: true, + LogEveryRequest: true, MainDomain: "changed", RawDomain: "changed", AllowedCorsDomains: fixArrayFromCtx(ctx, "allowed-cors-domains", []string{"changed"}), @@ -309,6 +319,8 @@ func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *tes "--port", "8443", "--http-port", "443", "--enable-http-server", + "--use-proxy-protocol", + "--log-every-request", }, ) } @@ -329,6 +341,8 @@ func TestMergeServerConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgE {args: []string{"--pages-branch", "changed"}, callback: func(sc *ServerConfig) { sc.PagesBranches = []string{"changed"} }}, {args: []string{"--allowed-cors-domains", "changed"}, callback: func(sc *ServerConfig) { sc.AllowedCorsDomains = []string{"changed"} }}, {args: []string{"--blacklisted-paths", "changed"}, callback: func(sc *ServerConfig) { sc.BlacklistedPaths = []string{"changed"} }}, + {args: []string{"--use-proxy-protocol"}, callback: func(sc *ServerConfig) { sc.UseProxyProtocol = true }}, + {args: []string{"--log-every-request"}, callback: func(sc *ServerConfig) { sc.LogEveryRequest = true }}, } for _, pair := range testValuePairs { @@ -345,6 +359,8 @@ func TestMergeServerConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgE PagesBranches: []string{"original"}, AllowedCorsDomains: []string{"original"}, BlacklistedPaths: []string{"original"}, + UseProxyProtocol: false, + LogEveryRequest: false, } expectedConfig := cfg diff --git a/server/handler/handler.go b/server/handler/handler.go index 437697a..254f40b 100644 --- a/server/handler/handler.go +++ b/server/handler/handler.go @@ -28,6 +28,10 @@ func Handler( return func(w http.ResponseWriter, req *http.Request) { ctx := context.New(w, req) log := log.With().Str("ReqId", ctx.ReqId).Strs("Handler", []string{req.Host, req.RequestURI}).Logger() + + if cfg.LogEveryRequest { + log.Log().Str("IP", req.RemoteAddr).Msg("new request") + } log.Debug().Msg("\n----------------------------------------------------------") ctx.RespWriter.Header().Set("Server", "pages-server")