cache: Move creation logic into cache package
This commit is contained in:
parent
c70b82e9ab
commit
b2a1ab61c9
4 changed files with 31 additions and 37 deletions
19
cache/cache.go
vendored
19
cache/cache.go
vendored
|
@ -1,9 +1,28 @@
|
||||||
// Package cache includes a memory cache for ntfy-alertmanager.
|
// Package cache includes a memory cache for ntfy-alertmanager.
|
||||||
package cache
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.xenrox.net/~xenrox/ntfy-alertmanager/config"
|
||||||
|
)
|
||||||
|
|
||||||
// Cache is the interface that describes a cache for ntfy-alertmanager.
|
// Cache is the interface that describes a cache for ntfy-alertmanager.
|
||||||
type Cache interface {
|
type Cache interface {
|
||||||
Set(fingerprint string, status string) error
|
Set(fingerprint string, status string) error
|
||||||
Contains(fingerprint string, status string) (bool, error)
|
Contains(fingerprint string, status string) (bool, error)
|
||||||
Cleanup()
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -19,15 +19,6 @@ const (
|
||||||
Multi
|
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.
|
// Config is the configuration of the bridge.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
BaseURL string
|
BaseURL string
|
||||||
|
@ -38,7 +29,7 @@ type Config struct {
|
||||||
Password string
|
Password string
|
||||||
Ntfy ntfyConfig
|
Ntfy ntfyConfig
|
||||||
Labels labels
|
Labels labels
|
||||||
Cache cacheConfig
|
Cache CacheConfig
|
||||||
Am alertmanagerConfig
|
Am alertmanagerConfig
|
||||||
Resolved resolvedConfig
|
Resolved resolvedConfig
|
||||||
}
|
}
|
||||||
|
@ -65,9 +56,10 @@ type labelConfig struct {
|
||||||
Call string
|
Call string
|
||||||
}
|
}
|
||||||
|
|
||||||
type cacheConfig struct {
|
// CacheConfig is the configuration of the cache.
|
||||||
|
type CacheConfig struct {
|
||||||
// shared settings
|
// shared settings
|
||||||
Type CacheType
|
Type string
|
||||||
Duration time.Duration
|
Duration time.Duration
|
||||||
// memory settings
|
// memory settings
|
||||||
CleanupInterval time.Duration
|
CleanupInterval time.Duration
|
||||||
|
@ -100,7 +92,7 @@ func ReadConfig(path string) (*Config, error) {
|
||||||
config.LogLevel = "info"
|
config.LogLevel = "info"
|
||||||
config.AlertMode = Single
|
config.AlertMode = Single
|
||||||
|
|
||||||
config.Cache.Type = Memory
|
config.Cache.Type = "memory"
|
||||||
config.Cache.Duration = time.Hour * 24
|
config.Cache.Duration = time.Hour * 24
|
||||||
// memory
|
// memory
|
||||||
config.Cache.CleanupInterval = time.Hour
|
config.Cache.CleanupInterval = time.Hour
|
||||||
|
@ -295,19 +287,9 @@ func ReadConfig(path string) (*Config, error) {
|
||||||
if cacheDir != nil {
|
if cacheDir != nil {
|
||||||
d = cacheDir.Children.Get("type")
|
d = cacheDir.Children.Get("type")
|
||||||
if d != nil {
|
if d != nil {
|
||||||
var cacheType string
|
if err := d.ParseParams(&config.Cache.Type); err != nil {
|
||||||
if err := d.ParseParams(&cacheType); err != nil {
|
|
||||||
return nil, err
|
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
|
var durationString string
|
||||||
|
|
|
@ -83,8 +83,8 @@ cache {
|
||||||
"instance:example.com": {Tags: []string{"computer", "example"}},
|
"instance:example.com": {Tags: []string{"computer", "example"}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Cache: cacheConfig{
|
Cache: CacheConfig{
|
||||||
Type: Redis,
|
Type: "redis",
|
||||||
Duration: 48 * time.Hour,
|
Duration: 48 * time.Hour,
|
||||||
CleanupInterval: time.Hour,
|
CleanupInterval: time.Hour,
|
||||||
RedisURL: "redis://user:password@localhost:6789/3",
|
RedisURL: "redis://user:password@localhost:6789/3",
|
||||||
|
|
15
main.go
15
main.go
|
@ -444,16 +444,9 @@ func main() {
|
||||||
|
|
||||||
client := &httpClient{&http.Client{Timeout: time.Second * 3}}
|
client := &httpClient{&http.Client{Timeout: time.Second * 3}}
|
||||||
|
|
||||||
var c cache.Cache
|
c, err := cache.NewCache(cfg.Cache)
|
||||||
switch cfg.Cache.Type {
|
if err != nil {
|
||||||
case config.Memory:
|
logger.Fatalf("Failed to create cache: %v", err)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
bridge := &bridge{cfg: cfg, logger: logger, cache: c, client: client}
|
bridge := &bridge{cfg: cfg, logger: logger, cache: c, client: client}
|
||||||
|
|
||||||
|
@ -468,7 +461,7 @@ func main() {
|
||||||
http.HandleFunc("/silences", bridge.handleSilences)
|
http.HandleFunc("/silences", bridge.handleSilences)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Cache.Type == config.Memory {
|
if _, ok := c.(*cache.MemoryCache); ok {
|
||||||
go bridge.runCleanup()
|
go bridge.runCleanup()
|
||||||
}
|
}
|
||||||
logger.Fatal(http.ListenAndServe(cfg.HTTPAddress, nil))
|
logger.Fatal(http.ListenAndServe(cfg.HTTPAddress, nil))
|
||||||
|
|
Loading…
Reference in a new issue