Files
gatus/storage/config.go
T

59 lines
1.9 KiB
Go
Raw Normal View History

2021-02-02 23:06:34 -05:00
package storage
import (
"errors"
)
2021-10-28 19:35:46 -04:00
const (
DefaultMaximumNumberOfResults = 100
DefaultMaximumNumberOfEvents = 50
)
2021-10-28 19:35:46 -04:00
var (
2021-11-09 00:06:41 -05:00
ErrSQLStorageRequiresPath = errors.New("sql storage requires a non-empty path to be defined")
ErrMemoryStorageDoesNotSupportPath = errors.New("memory storage does not support persistence, use sqlite if you want persistence on file")
2021-10-28 19:35:46 -04:00
)
2021-07-15 22:07:30 -04:00
// Config is the configuration for storage
2021-02-02 23:06:34 -05:00
type Config struct {
// Path is the path used by the store to achieve persistence
// If blank, persistence is disabled.
// Note that not all Type support persistence
Path string `yaml:"path"`
2021-07-15 22:07:30 -04:00
// Type of store
// If blank, uses the default in-memory store
Type Type `yaml:"type"`
// Caching is whether to enable caching.
// This is used to drastically decrease read latency by pre-emptively caching writes
// as they happen, also known as the write-through caching strategy.
// Does not apply if Config.Type is not TypePostgres or TypeSQLite.
Caching bool `yaml:"caching,omitempty"`
// MaximumNumberOfResults is the number of results each endpoint should be able to provide
MaximumNumberOfResults int `yaml:"maximum-number-of-results,omitempty"`
// MaximumNumberOfEvents is the number of events each endpoint should be able to provide
MaximumNumberOfEvents int `yaml:"maximum-number-of-events,omitempty"`
2021-02-02 23:06:34 -05:00
}
2021-10-28 19:35:46 -04:00
// ValidateAndSetDefaults validates the configuration and sets the default values (if applicable)
func (c *Config) ValidateAndSetDefaults() error {
if c.Type == "" {
c.Type = TypeMemory
}
if (c.Type == TypePostgres || c.Type == TypeSQLite) && len(c.Path) == 0 {
2021-11-09 00:06:41 -05:00
return ErrSQLStorageRequiresPath
2021-10-28 19:35:46 -04:00
}
if c.Type == TypeMemory && len(c.Path) > 0 {
return ErrMemoryStorageDoesNotSupportPath
}
if c.MaximumNumberOfResults <= 0 {
c.MaximumNumberOfResults = DefaultMaximumNumberOfResults
}
if c.MaximumNumberOfEvents <= 0 {
c.MaximumNumberOfEvents = DefaultMaximumNumberOfEvents
}
2021-10-28 19:35:46 -04:00
return nil
}