diff --git a/main.go b/main.go index 66ff871..14cdf6c 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( "log/slog" "net/http" "os" + "strconv" "time" _ "time/tzdata" @@ -145,10 +146,10 @@ type InverterSettingsData struct { // Config settings struct type Config struct { - DB string `json:"db"` - OpenDTU string `json:"opendtu"` - TimescaleDBEnabled bool `json:"timescaledb"` - TZ string `json:"tz"` + DB string `json:"db"` + OpenDTU string `json:"opendtu"` + TimescaleDB bool `json:"timescaledb"` + TZ string `json:"tz"` } var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil)) @@ -180,7 +181,14 @@ func LoadConfig() Config { log.Fatal("REMOTE_URL environment variable is not set.") } - config.TimescaleDBEnabled = os.Getenv("TIMESCALEDB_ENABLED") + timescaleDBStr := os.Getenv("TIMESCALEDB_ENABLED") + if timescaleDBStr != "" { + timescaleDB, err := strconv.ParseBool(timescaleDBStr) + if err != nil { + log.Fatalf("Error parsing TIMESCALEDB_ENABLED: %v", err) + } + config.TimescaleDB = timescaleDB + } config.TZ = os.Getenv("TZ") } @@ -394,7 +402,7 @@ func createTables(db *sql.DB) { if err != nil { log.Fatal("Error creating tables: ", err) } - timescaleEnabled := config.TimescaleDBEnabled + timescaleEnabled := config.TimescaleDB enableTimescaleDB := ` -- CREATE EXTENSION IF NOT EXISTS timescaledb; @@ -406,7 +414,7 @@ func createTables(db *sql.DB) { SELECT create_hypertable('opendtu_events', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE); SELECT create_hypertable('opendtu_hints', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE); ` - if timescaleEnabled == "true" { + if timescaleEnabled { _, err := db.Exec(enableTimescaleDB) if err != nil { log.Fatal("Error enabling TimescaleDB: ", err)