Handle TimescaleDBEnabled as bool.
All checks were successful
Build Docker image / build (push) Successful in 1m6s
Build Golang packages / release (push) Successful in 1m58s

This commit is contained in:
Pieter Hollander 2024-07-25 19:26:36 +02:00
parent 5282a22879
commit 2d843ceb3f
Signed by: pieter
SSH key fingerprint: SHA256:HbX+9cBXsop9SuvL+mELd29sK+7DehFfdVweFVDtMSg

22
main.go
View file

@ -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)