From b2a1ab61c9f2e948451d4afc2a88672a745a36c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Wed, 12 Jul 2023 19:00:25 +0200 Subject: [PATCH] cache: Move creation logic into cache package --- cache/cache.go | 19 +++++++++++++++++++ config/config.go | 30 ++++++------------------------ config/config_test.go | 4 ++-- main.go | 15 ++++----------- 4 files changed, 31 insertions(+), 37 deletions(-) diff --git a/cache/cache.go b/cache/cache.go index 46b9b81..dfea537 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -1,9 +1,28 @@ // Package cache includes a memory cache for ntfy-alertmanager. package cache +import ( + "fmt" + "strings" + + "git.xenrox.net/~xenrox/ntfy-alertmanager/config" +) + // Cache is the interface that describes a cache for ntfy-alertmanager. type Cache interface { Set(fingerprint string, status string) error Contains(fingerprint string, status string) (bool, error) Cleanup() } + +// NewCache reads the cache configuration cfg and creates the cache. +func NewCache(cfg config.CacheConfig) (Cache, error) { + switch strings.ToLower(cfg.Type) { + case "memory": + return NewMemoryCache(cfg.Duration), nil + case "redis": + return NewRedisCache(cfg.RedisURL, cfg.Duration) + default: + return nil, fmt.Errorf("illegal type %q", cfg.Type) + } +} diff --git a/config/config.go b/config/config.go index 74bd8ff..f4ae1af 100644 --- a/config/config.go +++ b/config/config.go @@ -19,15 +19,6 @@ const ( Multi ) -// CacheType is the type of cache that well be used. -type CacheType int - -// The different types of caches. -const ( - Memory CacheType = iota - Redis -) - // Config is the configuration of the bridge. type Config struct { BaseURL string @@ -38,7 +29,7 @@ type Config struct { Password string Ntfy ntfyConfig Labels labels - Cache cacheConfig + Cache CacheConfig Am alertmanagerConfig Resolved resolvedConfig } @@ -65,9 +56,10 @@ type labelConfig struct { Call string } -type cacheConfig struct { +// CacheConfig is the configuration of the cache. +type CacheConfig struct { // shared settings - Type CacheType + Type string Duration time.Duration // memory settings CleanupInterval time.Duration @@ -100,7 +92,7 @@ func ReadConfig(path string) (*Config, error) { config.LogLevel = "info" config.AlertMode = Single - config.Cache.Type = Memory + config.Cache.Type = "memory" config.Cache.Duration = time.Hour * 24 // memory config.Cache.CleanupInterval = time.Hour @@ -295,19 +287,9 @@ func ReadConfig(path string) (*Config, error) { if cacheDir != nil { d = cacheDir.Children.Get("type") if d != nil { - var cacheType string - if err := d.ParseParams(&cacheType); err != nil { + if err := d.ParseParams(&config.Cache.Type); err != nil { return nil, err } - - switch strings.ToLower(cacheType) { - case "memory": - config.Cache.Type = Memory - case "redis": - config.Cache.Type = Redis - default: - return nil, fmt.Errorf("cache: illegal type %q", cacheType) - } } var durationString string diff --git a/config/config_test.go b/config/config_test.go index 98bd562..d337fc8 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -83,8 +83,8 @@ cache { "instance:example.com": {Tags: []string{"computer", "example"}}, }, }, - Cache: cacheConfig{ - Type: Redis, + Cache: CacheConfig{ + Type: "redis", Duration: 48 * time.Hour, CleanupInterval: time.Hour, RedisURL: "redis://user:password@localhost:6789/3", diff --git a/main.go b/main.go index cf15fec..a745739 100644 --- a/main.go +++ b/main.go @@ -444,16 +444,9 @@ func main() { client := &httpClient{&http.Client{Timeout: time.Second * 3}} - var c cache.Cache - switch cfg.Cache.Type { - case config.Memory: - c = cache.NewMemoryCache(cfg.Cache.Duration) - case config.Redis: - var err error - c, err = cache.NewRedisCache(cfg.Cache.RedisURL, cfg.Cache.Duration) - if err != nil { - logger.Fatalf("Failed to create redis cache: %v", err) - } + c, err := cache.NewCache(cfg.Cache) + if err != nil { + logger.Fatalf("Failed to create cache: %v", err) } bridge := &bridge{cfg: cfg, logger: logger, cache: c, client: client} @@ -468,7 +461,7 @@ func main() { http.HandleFunc("/silences", bridge.handleSilences) } - if cfg.Cache.Type == config.Memory { + if _, ok := c.(*cache.MemoryCache); ok { go bridge.runCleanup() } logger.Fatal(http.ListenAndServe(cfg.HTTPAddress, nil))