Files
gatus/storage/config.go
T

59 lines
2.2 KiB
Go
Raw Normal View History

2021-02-02 23:06:34 -05:00
package storage
import (
"errors"
"log"
)
2021-10-28 19:35:46 -04:00
var (
ErrSQLStorageRequiresFile = errors.New("sql storage requires a non-empty file to be defined")
ErrMemoryStorageDoesNotSupportFile = errors.New("memory storage does not support persistence, use sqlite if you want persistence on file")
ErrCannotSetBothFileAndPath = errors.New("file has been deprecated in favor of path: you cannot set both of them")
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
//
// XXX: Rename to path for v4.0.0
Path string `yaml:"path"`
2021-02-20 19:00:54 -05:00
// File is the path of the file to use for persistence
2021-07-15 22:07:30 -04:00
// If blank, persistence is disabled
//
// Deprecated
2021-02-02 23:06:34 -05:00
File string `yaml:"file"`
2021-07-15 22:07:30 -04:00
// Type of store
// If blank, uses the default in-memory store
Type Type `yaml:"type"`
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 len(c.File) > 0 && len(c.Path) > 0 { // XXX: Remove for v4.0.0
return ErrCannotSetBothFileAndPath
} else if len(c.File) > 0 { // XXX: Remove for v4.0.0
log.Println("WARNING: Your configuration is using 'storage.file', which is deprecated in favor of 'storage.path'")
log.Println("WARNING: storage.file will be completely removed in v4.0.0, so please update your configuration")
log.Println("WARNING: See https://github.com/TwiN/gatus/issues/197")
c.Path = c.File
}
if c.Type == "" {
c.Type = TypeMemory
}
if (c.Type == TypePostgres || c.Type == TypeSQLite) && len(c.Path) == 0 {
2021-10-28 19:35:46 -04:00
return ErrSQLStorageRequiresFile
}
if c.Type == TypeMemory && len(c.Path) > 0 {
log.Println("WARNING: Your configuration is using a storage of type memory with persistence, which has been deprecated")
log.Println("WARNING: As of v4.0.0, the default storage type (memory) will not support persistence.")
log.Println("WARNING: If you want persistence, use 'storage.type: sqlite' instead of 'storage.type: memory'")
log.Println("WARNING: See https://github.com/TwiN/gatus/issues/198")
// XXX: Uncomment the following line for v4.0.0
//return ErrMemoryStorageDoesNotSupportFile
}
2021-10-28 19:35:46 -04:00
return nil
}