mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-06-19 23:22:40 +02:00
feat: add option to log every request with path and IP
This commit is contained in:
parent
2b2f280bc3
commit
023ea17492
5 changed files with 30 additions and 0 deletions
|
@ -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{
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue