diff --git a/configuration.go b/configuration.go index 5cb9b7c..46002d6 100644 --- a/configuration.go +++ b/configuration.go @@ -14,8 +14,8 @@ type Configuration struct { var smonConfig Configuration -func SmonConfigInit() (err error) { - smonConfig.Settings = make(map[string]string, 8) +func SmonConfigInit() (cfg Configuration, err error) { + cfg.Settings = make(map[string]string, 8) var rows *sql.Rows rows, err = service.Db.Conn.Query(`SELECT * FROM public.configuration`) @@ -33,12 +33,22 @@ func SmonConfigInit() (err error) { return } - smonConfig.Settings[setting] = value + cfg.Settings[setting] = value } return } +func (cfg *Configuration) Validate() (err error) { + mandatorySettings := []string{"THEME", "TIMEZONE"} + for _, settingsKey := range mandatorySettings { + if _, found := cfg.Settings[settingsKey]; !found { + return werr.New("Configuration missing setting '%s' in database", settingsKey) + } + } + return +} + func (cfg *Configuration) SetTheme(theme string) (err error) { cfg.Settings["THEME"] = theme _, err = service.Db.Conn.Exec(`UPDATE public.configuration SET value=$1 WHERE setting='THEME'`, theme) diff --git a/main.go b/main.go index 8310078..9c3d5ce 100644 --- a/main.go +++ b/main.go @@ -153,7 +153,13 @@ func main() { // {{{ go nodataLoop() - err = SmonConfigInit() + smonConfig, err = SmonConfigInit() + if err != nil { + logger.Error("configuration", "error", werr.Wrap(err)) + os.Exit(1) + } + + err = smonConfig.Validate() if err != nil { logger.Error("configuration", "error", werr.Wrap(err)) os.Exit(1) diff --git a/sql/00020.sql b/sql/00020.sql new file mode 100644 index 0000000..b8f1a06 --- /dev/null +++ b/sql/00020.sql @@ -0,0 +1 @@ +INSERT INTO public.configuration(setting, value) VALUES('TIMEZONE', 'Europe/Stockholm');