diff --git a/cmd/ncro/main.go b/cmd/ncro/main.go index 5b49192..bbf529b 100644 --- a/cmd/ncro/main.go +++ b/cmd/ncro/main.go @@ -157,7 +157,7 @@ func main() { srv := &http.Server{ Addr: cfg.Server.Listen, - Handler: server.New(r, p, db, cfg.Upstreams, 30), + Handler: server.New(r, p, db, cfg.Upstreams, cfg.Server.CachePriority), ReadTimeout: cfg.Server.ReadTimeout.Duration, WriteTimeout: cfg.Server.WriteTimeout.Duration, } diff --git a/internal/config/config.go b/internal/config/config.go index bc68fb8..2fd4c78 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -43,9 +43,10 @@ type UpstreamConfig struct { } type ServerConfig struct { - Listen string `yaml:"listen"` - ReadTimeout Duration `yaml:"read_timeout"` - WriteTimeout Duration `yaml:"write_timeout"` + Listen string `yaml:"listen"` + ReadTimeout Duration `yaml:"read_timeout"` + WriteTimeout Duration `yaml:"write_timeout"` + CachePriority int `yaml:"cache_priority"` } type CacheConfig struct { @@ -86,9 +87,10 @@ type Config struct { func defaults() Config { return Config{ Server: ServerConfig{ - Listen: ":8080", - ReadTimeout: Duration{30 * time.Second}, - WriteTimeout: Duration{30 * time.Second}, + Listen: ":8080", + ReadTimeout: Duration{30 * time.Second}, + WriteTimeout: Duration{30 * time.Second}, + CachePriority: 30, }, Upstreams: []UpstreamConfig{ {URL: "https://cache.nixos.org", Priority: 10}, @@ -130,6 +132,9 @@ func (c *Config) Validate() error { if c.Server.Listen == "" { return fmt.Errorf("server.listen is empty") } + if c.Server.CachePriority < 1 { + return fmt.Errorf("server.cache_priority must be >= 1, got %d", c.Server.CachePriority) + } if c.Cache.LatencyAlpha <= 0 || c.Cache.LatencyAlpha >= 1 { return fmt.Errorf("cache.latency_alpha must be between 0 and 1 exclusive, got %f", c.Cache.LatencyAlpha) } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 10b74fe..b71801e 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -178,6 +178,24 @@ func TestValidateUpstreamBadPublicKey(t *testing.T) { } } +func TestCachePriorityDefault(t *testing.T) { + cfg, err := config.Load("") + if err != nil { + t.Fatal(err) + } + if cfg.Server.CachePriority != 30 { + t.Errorf("default CachePriority = %d, want 30", cfg.Server.CachePriority) + } +} + +func TestCachePriorityValidation(t *testing.T) { + cfg, _ := config.Load("") + cfg.Server.CachePriority = 0 + if err := cfg.Validate(); err == nil { + t.Error("expected error for CachePriority = 0") + } +} + func TestInvalidDuration(t *testing.T) { yamlContent := ` server: