Rename config.yaml to example and add config.yaml to gitignore, make Metrics optional and configurable

This commit is contained in:
Pablu23
2025-10-15 13:15:56 +02:00
parent 28d9c58a66
commit f4ca559a26
5 changed files with 79 additions and 77 deletions

2
.gitignore vendored
View File

@@ -3,3 +3,5 @@
bin/
data.json
*.json
config.yaml

View File

@@ -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
}

View File

@@ -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"`
}

View File

@@ -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

View File

@@ -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() {