feat: add option to log every request with path and IP

This commit is contained in:
crapStone 2025-06-12 22:57:04 +02:00 committed by crapStone
parent 2b2f280bc3
commit 023ea17492
5 changed files with 30 additions and 0 deletions

View file

@ -115,6 +115,12 @@ var (
EnvVars: []string{"USE_PROXY_PROTOCOL"}, EnvVars: []string{"USE_PROXY_PROTOCOL"},
Value: false, 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 // Default branches to fetch assets from
&cli.StringSliceFlag{ &cli.StringSliceFlag{

View file

@ -14,6 +14,7 @@ type ServerConfig struct {
HttpPort uint16 `default:"80"` HttpPort uint16 `default:"80"`
HttpServerEnabled bool `default:"true"` HttpServerEnabled bool `default:"true"`
UseProxyProtocol bool `default:"false"` UseProxyProtocol bool `default:"false"`
LogEveryRequest bool `default:"false"`
MainDomain string MainDomain string
RawDomain string RawDomain string
PagesBranches []string PagesBranches []string

View file

@ -72,6 +72,9 @@ func mergeServerConfig(ctx *cli.Context, config *ServerConfig) {
if ctx.IsSet("use-proxy-protocol") { if ctx.IsSet("use-proxy-protocol") {
config.UseProxyProtocol = ctx.Bool("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") { if ctx.IsSet("pages-domain") {
config.MainDomain = ctx.String("pages-domain") config.MainDomain = ctx.String("pages-domain")

View file

@ -140,6 +140,8 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T
Port: 8080, Port: 8080,
HttpPort: 80, HttpPort: 80,
HttpServerEnabled: false, HttpServerEnabled: false,
UseProxyProtocol: false,
LogEveryRequest: false,
MainDomain: "original", MainDomain: "original",
RawDomain: "original", RawDomain: "original",
PagesBranches: []string{"original"}, PagesBranches: []string{"original"},
@ -180,6 +182,8 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T
Port: 8443, Port: 8443,
HttpPort: 443, HttpPort: 443,
HttpServerEnabled: true, HttpServerEnabled: true,
UseProxyProtocol: true,
LogEveryRequest: true,
MainDomain: "changed", MainDomain: "changed",
RawDomain: "changed", RawDomain: "changed",
PagesBranches: []string{"changed"}, PagesBranches: []string{"changed"},
@ -227,6 +231,8 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T
"--port", "8443", "--port", "8443",
"--http-port", "443", "--http-port", "443",
"--enable-http-server", "--enable-http-server",
"--use-proxy-protocol",
"--log-every-request",
// Forge // Forge
"--forge-root", "changed", "--forge-root", "changed",
"--forge-api-token", "changed", "--forge-api-token", "changed",
@ -277,6 +283,8 @@ func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *tes
Port: 8080, Port: 8080,
HttpPort: 80, HttpPort: 80,
HttpServerEnabled: false, HttpServerEnabled: false,
UseProxyProtocol: false,
LogEveryRequest: false,
MainDomain: "original", MainDomain: "original",
RawDomain: "original", RawDomain: "original",
AllowedCorsDomains: []string{"original"}, AllowedCorsDomains: []string{"original"},
@ -290,6 +298,8 @@ func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *tes
Port: 8443, Port: 8443,
HttpPort: 443, HttpPort: 443,
HttpServerEnabled: true, HttpServerEnabled: true,
UseProxyProtocol: true,
LogEveryRequest: true,
MainDomain: "changed", MainDomain: "changed",
RawDomain: "changed", RawDomain: "changed",
AllowedCorsDomains: fixArrayFromCtx(ctx, "allowed-cors-domains", []string{"changed"}), AllowedCorsDomains: fixArrayFromCtx(ctx, "allowed-cors-domains", []string{"changed"}),
@ -309,6 +319,8 @@ func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *tes
"--port", "8443", "--port", "8443",
"--http-port", "443", "--http-port", "443",
"--enable-http-server", "--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{"--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{"--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{"--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 { for _, pair := range testValuePairs {
@ -345,6 +359,8 @@ func TestMergeServerConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgE
PagesBranches: []string{"original"}, PagesBranches: []string{"original"},
AllowedCorsDomains: []string{"original"}, AllowedCorsDomains: []string{"original"},
BlacklistedPaths: []string{"original"}, BlacklistedPaths: []string{"original"},
UseProxyProtocol: false,
LogEveryRequest: false,
} }
expectedConfig := cfg expectedConfig := cfg

View file

@ -28,6 +28,10 @@ func Handler(
return func(w http.ResponseWriter, req *http.Request) { return func(w http.ResponseWriter, req *http.Request) {
ctx := context.New(w, req) ctx := context.New(w, req)
log := log.With().Str("ReqId", ctx.ReqId).Strs("Handler", []string{req.Host, req.RequestURI}).Logger() 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----------------------------------------------------------") log.Debug().Msg("\n----------------------------------------------------------")
ctx.RespWriter.Header().Set("Server", "pages-server") ctx.RespWriter.Header().Set("Server", "pages-server")