From c28c848b95504a5bc992c7c708cd4030bd664d69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20=C3=85hall?= Date: Thu, 27 Jun 2024 09:07:02 +0200 Subject: [PATCH] Added TIMEZONE to database app config and validate --- configuration.go | 16 +++++++++++++--- main.go | 8 +++++++- sql/00020.sql | 1 + 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 sql/00020.sql 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');