From 2e271575c96f18da647c823676e84ffceaebc691 Mon Sep 17 00:00:00 2001 From: crapStone Date: Wed, 10 Sep 2025 14:22:10 +0200 Subject: [PATCH] feat: make logging interval configurable (#511) Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/511 Reviewed-by: Andreas Shimokawa Co-authored-by: crapStone Co-committed-by: crapStone --- cli/flags.go | 8 +++++++- config/config.go | 1 + config/setup.go | 3 +++ config/setup_test.go | 8 ++++++++ flake.lock | 6 +++--- server/dns/dns.go | 4 ++-- server/gitea/cache.go | 2 +- server/startup.go | 7 ++++++- 8 files changed, 31 insertions(+), 8 deletions(-) diff --git a/cli/flags.go b/cli/flags.go index 0260d24..c6e5d00 100644 --- a/cli/flags.go +++ b/cli/flags.go @@ -117,10 +117,16 @@ var ( }, &cli.UintFlag{ Name: "log-most-active-ips", - Usage: "logs a the n most active IPs every hour", + Usage: "logs a the n most active IPs every hour (or value of --ip-logging-interval)", EnvVars: []string{"LOG_MOST_ACTIVE_IPS"}, Value: 10, }, + &cli.UintFlag{ + Name: "ip-logging-interval", + Usage: "interval in seconds for ip address logging", + EnvVars: []string{"IP_LOGGING_INTERVAL"}, + Value: 0, + }, // Default branches to fetch assets from &cli.StringSliceFlag{ diff --git a/config/config.go b/config/config.go index 0901bb7..52dfe5c 100644 --- a/config/config.go +++ b/config/config.go @@ -16,6 +16,7 @@ type ServerConfig struct { UseProxyProtocol bool `default:"false"` LogMostActiveIps bool `default:"false"` MostActiveIpCount uint `default:"10"` + LoggingInterval uint `default:"0"` MainDomain string RawDomain string PagesBranches []string diff --git a/config/setup.go b/config/setup.go index a0307a1..484edec 100644 --- a/config/setup.go +++ b/config/setup.go @@ -76,6 +76,9 @@ func mergeServerConfig(ctx *cli.Context, config *ServerConfig) { config.LogMostActiveIps = true config.MostActiveIpCount = ctx.Uint("log-most-active-ips") } + if ctx.IsSet("ip-logging-interval") { + config.LoggingInterval = ctx.Uint("ip-logging-interval") + } if ctx.IsSet("pages-domain") { config.MainDomain = ctx.String("pages-domain") diff --git a/config/setup_test.go b/config/setup_test.go index 9e6b886..683aa37 100644 --- a/config/setup_test.go +++ b/config/setup_test.go @@ -143,6 +143,7 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T UseProxyProtocol: false, LogMostActiveIps: false, MostActiveIpCount: 10, + LoggingInterval: 0, MainDomain: "original", RawDomain: "original", PagesBranches: []string{"original"}, @@ -185,6 +186,7 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T HttpServerEnabled: true, UseProxyProtocol: true, LogMostActiveIps: true, + LoggingInterval: 300, MostActiveIpCount: 42, MainDomain: "changed", RawDomain: "changed", @@ -235,6 +237,7 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T "--enable-http-server", "--use-proxy-protocol", "--log-most-active-ips", "42", + "--ip-logging-interval", "300", // Forge "--forge-root", "changed", "--forge-api-token", "changed", @@ -288,6 +291,7 @@ func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *tes UseProxyProtocol: false, LogMostActiveIps: false, MostActiveIpCount: 10, + LoggingInterval: 0, MainDomain: "original", RawDomain: "original", AllowedCorsDomains: []string{"original"}, @@ -304,6 +308,7 @@ func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *tes UseProxyProtocol: true, LogMostActiveIps: true, MostActiveIpCount: 21, + LoggingInterval: 300, MainDomain: "changed", RawDomain: "changed", AllowedCorsDomains: fixArrayFromCtx(ctx, "allowed-cors-domains", []string{"changed"}), @@ -325,6 +330,7 @@ func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *tes "--enable-http-server", "--use-proxy-protocol", "--log-most-active-ips", "21", + "--ip-logging-interval", "300", }, ) } @@ -350,6 +356,7 @@ func TestMergeServerConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgE sc.LogMostActiveIps = true sc.MostActiveIpCount = 42 }}, + {args: []string{"--ip-logging-interval", "300"}, callback: func(sc *ServerConfig) { sc.LoggingInterval = 300 }}, } for _, pair := range testValuePairs { @@ -369,6 +376,7 @@ func TestMergeServerConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgE UseProxyProtocol: false, LogMostActiveIps: false, MostActiveIpCount: 10, + LoggingInterval: 0, } expectedConfig := cfg diff --git a/flake.lock b/flake.lock index f7b666b..35317a1 100644 --- a/flake.lock +++ b/flake.lock @@ -20,11 +20,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1751271578, - "narHash": "sha256-P/SQmKDu06x8yv7i0s8bvnnuJYkxVGBWLWHaU+tt4YY=", + "lastModified": 1757068644, + "narHash": "sha256-NOrUtIhTkIIumj1E/Rsv1J37Yi3xGStISEo8tZm3KW4=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "3016b4b15d13f3089db8a41ef937b13a9e33a8df", + "rev": "8eb28adfa3dc4de28e792e3bf49fcf9007ca8ac9", "type": "github" }, "original": { diff --git a/server/dns/dns.go b/server/dns/dns.go index e29e42c..371cccf 100644 --- a/server/dns/dns.go +++ b/server/dns/dns.go @@ -45,7 +45,7 @@ func GetTargetFromDNS(domain, mainDomainSuffix, firstDefaultBranch string) (targ _ = lookupCache.Add(domain, cname) } if cname == "" { - return + return targetOwner, targetRepo, targetBranch } cnameParts := strings.Split(strings.TrimSuffix(cname, mainDomainSuffix), ".") targetOwner = cnameParts[len(cnameParts)-1] @@ -62,5 +62,5 @@ func GetTargetFromDNS(domain, mainDomainSuffix, firstDefaultBranch string) (targ targetBranch = firstDefaultBranch } // if targetBranch is still empty, the caller must find the default branch - return + return targetOwner, targetRepo, targetBranch } diff --git a/server/gitea/cache.go b/server/gitea/cache.go index 03f40a9..878455e 100644 --- a/server/gitea/cache.go +++ b/server/gitea/cache.go @@ -111,7 +111,7 @@ func (t *writeCacheReader) Read(p []byte) (n int, err error) { _, _ = t.buffer.Write(p[:n]) } } - return + return n, err } func (t *writeCacheReader) Close() error { diff --git a/server/startup.go b/server/startup.go index cdd87c2..cf0ba4d 100644 --- a/server/startup.go +++ b/server/startup.go @@ -140,7 +140,12 @@ func Serve(ctx *cli.Context) error { mostActiveIpMap := &sync.Map{} if cfg.Server.LogMostActiveIps { go func() { - ticker := time.NewTicker(1 * time.Hour) + interval := 1 * time.Hour + if cfg.Server.LoggingInterval > 0 { + interval = time.Duration(cfg.Server.LoggingInterval) * time.Second + } + + ticker := time.NewTicker(interval) for range ticker.C { type kv struct { Key string