diff --git a/.gitignore b/.gitignore index 5656683..a1f919d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ bin/ data.json *.json +config.yaml + diff --git a/cmd/domain-router/main.go b/cmd/domain-router/main.go index 5fbd1f8..25fed55 100644 --- a/cmd/domain-router/main.go +++ b/cmd/domain-router/main.go @@ -61,11 +61,11 @@ func main() { var wg sync.WaitGroup wg.Add(1) go func() { + defer wg.Done() <-sigs log.Info().Msg("Stopping server") server.Shutdown(context.Background()) pipeline.Stop() - wg.Done() }() if config.Server.Ssl.Enabled { @@ -127,8 +127,14 @@ func configureMiddleware(config *domainrouter.Config) *middleware.Pipeline { pipeline.AddMiddleware(&middleware.RequestLogger{}) } - metrics := middleware.NewMetrics(512, 1*time.Minute, "tmp_metrics.json") - pipeline.AddMiddleware(metrics) + if config.Metrics.Enabled { + flushInterval, err := time.ParseDuration(config.Metrics.FlushInterval) + if err != nil { + log.Fatal().Err(err).Str("flush_interval", config.Metrics.FlushInterval).Msg("Could not parse FlushInterval") + } + metrics := middleware.NewMetrics(config.Metrics.BufferSize, flushInterval, config.Metrics.File) + pipeline.AddMiddleware(metrics) + } return pipeline } diff --git a/config.go b/config.go index 3a4a1df..e65ce27 100644 --- a/config.go +++ b/config.go @@ -43,4 +43,10 @@ type Config struct { MaxBackups int `yamls:"maxBackups"` } `yaml:"file"` } `yaml:"logging"` + Metrics struct { + Enabled bool `yaml:"enabled"` + File string `yaml:"file"` + BufferSize int `yaml:"bufferSize"` + FlushInterval string `yaml:"flushInterval"` + } `yaml:"metrics"` } diff --git a/config.yaml b/example.config.yaml similarity index 57% rename from config.yaml rename to example.config.yaml index 8966442..45d6804 100644 --- a/config.yaml +++ b/example.config.yaml @@ -1,73 +1,61 @@ -server: - port: 443 - ssl: - enabled: true - certFile: server.crt - keyFile: server.key - acme: - enabled: false - email: me@pablu.de - keyFile: userKey.key - caDirUrl: https://192.168.2.154:14000/dir - tlsAlpn01Port: 5001 - http01Port: 5002 - renewTime: 30s - - -logging: - level: trace - # Pretty print for human consumption otherwise json - pretty: true - # Log incoming requests - requests: true - # Log to file aswell as stderr - file: - enabled: false - maxAge: 14 - maxBackups: 10 - path: ~/logs/router - - -rateLimit: - enabled: false - # How many requests per ip adress are allowed - bucketSize: 50 - # How many requests per ip address are refilled - refillSize: 50 - # How often requests per ip address are refilled - refillTime: 30s - # How often Ip Addresses get cleaned up (only ip addresses with max allowed requests are cleaned up) - cleanupTime: 45s - - -hosts: - # Remote address to request - - remotes: - - localhost - # Port on which to request - port: 8181 - # Domains which get redirected to host - domains: - - localhost - - test.localhost - - - remotes: - - localhost - port: 8080 - domains: - - api.hitstar.localhost - - - remotes: - - localhost - port: 5173 - domains: - - hitstar.localhost - - hipstar.localhost - rewrite: - "/api": api.hitstar.localhost - - - remotes: - - 127.0.0.1 - port: 46009 - domains: - - chat.localhost +server: + port: 80 + ssl: + enabled: false + certFile: server.crt + keyFile: server.key + acme: + enabled: false + email: user@host.dev + keyFile: userKey.key + caDirUrl: https://localhost:14000/dir + tlsAlpn01Port: 5001 + http01Port: 5002 + renewTime: 30s + +logging: + level: info + # Pretty print for human consumption otherwise json + pretty: true + # Log incoming requests + requests: true + # Log to file aswell as stderr + file: + enabled: false + maxAge: 14 + maxBackups: 10 + path: ~/logs/router + +rateLimit: + enabled: false + # How many requests per ip adress are allowed + bucketSize: 50 + # How many requests per ip address are refilled + refillSize: 50 + # How often requests per ip address are refilled + refillTime: 30s + # How often Ip Addresses get cleaned up (only ip addresses with max allowed requests are cleaned up) + cleanupTime: 45s + +# Experimental +metrics: + enabled: false + flushInterval: 1h + bufferSize: 512 + file: ~/metrics.json + +hosts: + - remotes: + - 127.0.0.1 + port: 3000 + domains: + - api.hitstar.xyz + + - remotes: + - 127.0.0.1 + port: 5173 + domains: + - localhost + - hitstar.xyz + rewrite: + "/api": api.hitstar.xyz diff --git a/middleware/metrics.go b/middleware/metrics.go index 98f7791..8ed0a0e 100644 --- a/middleware/metrics.go +++ b/middleware/metrics.go @@ -130,7 +130,7 @@ func (m *Metrics) Flush() { return } - log.Info().Str("file", m.file).Int("count", len(a)).Msg("Completed Metrics flush") + log.Debug().Str("file", m.file).Int("count", len(a)).Msg("Completed Metrics flush") } func (m *Metrics) Stop() {