mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-09-30 21:42:05 +00:00
feat: make logging interval configurable
This commit is contained in:
parent
30bc87089c
commit
373044f289
5 changed files with 25 additions and 2 deletions
|
@ -117,10 +117,16 @@ var (
|
||||||
},
|
},
|
||||||
&cli.UintFlag{
|
&cli.UintFlag{
|
||||||
Name: "log-most-active-ips",
|
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"},
|
EnvVars: []string{"LOG_MOST_ACTIVE_IPS"},
|
||||||
Value: 10,
|
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
|
// Default branches to fetch assets from
|
||||||
&cli.StringSliceFlag{
|
&cli.StringSliceFlag{
|
||||||
|
|
|
@ -16,6 +16,7 @@ type ServerConfig struct {
|
||||||
UseProxyProtocol bool `default:"false"`
|
UseProxyProtocol bool `default:"false"`
|
||||||
LogMostActiveIps bool `default:"false"`
|
LogMostActiveIps bool `default:"false"`
|
||||||
MostActiveIpCount uint `default:"10"`
|
MostActiveIpCount uint `default:"10"`
|
||||||
|
LoggingInterval uint `default:"0"`
|
||||||
MainDomain string
|
MainDomain string
|
||||||
RawDomain string
|
RawDomain string
|
||||||
PagesBranches []string
|
PagesBranches []string
|
||||||
|
|
|
@ -76,6 +76,9 @@ func mergeServerConfig(ctx *cli.Context, config *ServerConfig) {
|
||||||
config.LogMostActiveIps = true
|
config.LogMostActiveIps = true
|
||||||
config.MostActiveIpCount = ctx.Uint("log-most-active-ips")
|
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") {
|
if ctx.IsSet("pages-domain") {
|
||||||
config.MainDomain = ctx.String("pages-domain")
|
config.MainDomain = ctx.String("pages-domain")
|
||||||
|
|
|
@ -143,6 +143,7 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T
|
||||||
UseProxyProtocol: false,
|
UseProxyProtocol: false,
|
||||||
LogMostActiveIps: false,
|
LogMostActiveIps: false,
|
||||||
MostActiveIpCount: 10,
|
MostActiveIpCount: 10,
|
||||||
|
LoggingInterval: 0,
|
||||||
MainDomain: "original",
|
MainDomain: "original",
|
||||||
RawDomain: "original",
|
RawDomain: "original",
|
||||||
PagesBranches: []string{"original"},
|
PagesBranches: []string{"original"},
|
||||||
|
@ -185,6 +186,7 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T
|
||||||
HttpServerEnabled: true,
|
HttpServerEnabled: true,
|
||||||
UseProxyProtocol: true,
|
UseProxyProtocol: true,
|
||||||
LogMostActiveIps: true,
|
LogMostActiveIps: true,
|
||||||
|
LoggingInterval: 300,
|
||||||
MostActiveIpCount: 42,
|
MostActiveIpCount: 42,
|
||||||
MainDomain: "changed",
|
MainDomain: "changed",
|
||||||
RawDomain: "changed",
|
RawDomain: "changed",
|
||||||
|
@ -235,6 +237,7 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T
|
||||||
"--enable-http-server",
|
"--enable-http-server",
|
||||||
"--use-proxy-protocol",
|
"--use-proxy-protocol",
|
||||||
"--log-most-active-ips", "42",
|
"--log-most-active-ips", "42",
|
||||||
|
"--ip-logging-interval", "300",
|
||||||
// Forge
|
// Forge
|
||||||
"--forge-root", "changed",
|
"--forge-root", "changed",
|
||||||
"--forge-api-token", "changed",
|
"--forge-api-token", "changed",
|
||||||
|
@ -288,6 +291,7 @@ func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *tes
|
||||||
UseProxyProtocol: false,
|
UseProxyProtocol: false,
|
||||||
LogMostActiveIps: false,
|
LogMostActiveIps: false,
|
||||||
MostActiveIpCount: 10,
|
MostActiveIpCount: 10,
|
||||||
|
LoggingInterval: 0,
|
||||||
MainDomain: "original",
|
MainDomain: "original",
|
||||||
RawDomain: "original",
|
RawDomain: "original",
|
||||||
AllowedCorsDomains: []string{"original"},
|
AllowedCorsDomains: []string{"original"},
|
||||||
|
@ -304,6 +308,7 @@ func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *tes
|
||||||
UseProxyProtocol: true,
|
UseProxyProtocol: true,
|
||||||
LogMostActiveIps: true,
|
LogMostActiveIps: true,
|
||||||
MostActiveIpCount: 21,
|
MostActiveIpCount: 21,
|
||||||
|
LoggingInterval: 300,
|
||||||
MainDomain: "changed",
|
MainDomain: "changed",
|
||||||
RawDomain: "changed",
|
RawDomain: "changed",
|
||||||
AllowedCorsDomains: fixArrayFromCtx(ctx, "allowed-cors-domains", []string{"changed"}),
|
AllowedCorsDomains: fixArrayFromCtx(ctx, "allowed-cors-domains", []string{"changed"}),
|
||||||
|
@ -325,6 +330,7 @@ func TestMergeServerConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *tes
|
||||||
"--enable-http-server",
|
"--enable-http-server",
|
||||||
"--use-proxy-protocol",
|
"--use-proxy-protocol",
|
||||||
"--log-most-active-ips", "21",
|
"--log-most-active-ips", "21",
|
||||||
|
"--ip-logging-interval", "300",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -350,6 +356,7 @@ func TestMergeServerConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgE
|
||||||
sc.LogMostActiveIps = true
|
sc.LogMostActiveIps = true
|
||||||
sc.MostActiveIpCount = 42
|
sc.MostActiveIpCount = 42
|
||||||
}},
|
}},
|
||||||
|
{args: []string{"--ip-logging-interval", "300"}, callback: func(sc *ServerConfig) { sc.LoggingInterval = 300 }},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, pair := range testValuePairs {
|
for _, pair := range testValuePairs {
|
||||||
|
@ -369,6 +376,7 @@ func TestMergeServerConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgE
|
||||||
UseProxyProtocol: false,
|
UseProxyProtocol: false,
|
||||||
LogMostActiveIps: false,
|
LogMostActiveIps: false,
|
||||||
MostActiveIpCount: 10,
|
MostActiveIpCount: 10,
|
||||||
|
LoggingInterval: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedConfig := cfg
|
expectedConfig := cfg
|
||||||
|
|
|
@ -140,7 +140,12 @@ func Serve(ctx *cli.Context) error {
|
||||||
mostActiveIpMap := &sync.Map{}
|
mostActiveIpMap := &sync.Map{}
|
||||||
if cfg.Server.LogMostActiveIps {
|
if cfg.Server.LogMostActiveIps {
|
||||||
go func() {
|
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 {
|
for range ticker.C {
|
||||||
type kv struct {
|
type kv struct {
|
||||||
Key string
|
Key string
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue