gatus/storage/config.go

60 lines
2.2 KiB
Go
Raw Normal View History

2021-02-03 05:06:34 +01:00
package storage
import (
"errors"
"log"
)
var (
2021-11-09 06:06:41 +01: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")
ErrCannotSetBothFileAndPath = errors.New("file has been deprecated in favor of path: you cannot set both of them")
)
2021-07-16 04:07:30 +02:00
// Config is the configuration for storage
2021-02-03 05:06:34 +01: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-21 01:00:54 +01:00
// File is the path of the file to use for persistence
2021-07-16 04:07:30 +02:00
// If blank, persistence is disabled
//
// Deprecated
2021-02-03 05:06:34 +01:00
File string `yaml:"file"`
2021-07-16 04:07:30 +02:00
// Type of store
// If blank, uses the default in-memory store
Type Type `yaml:"type"`
2021-02-03 05:06:34 +01: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-11-09 06:06:41 +01:00
return ErrSQLStorageRequiresPath
}
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
2021-11-09 06:06:41 +01:00
//return ErrMemoryStorageDoesNotSupportPath
}
return nil
}