From b4110b81eb770621817e4f9296ac1c6509b82ccd Mon Sep 17 00:00:00 2001 From: Klagarge Date: Sun, 3 May 2026 18:10:42 +0200 Subject: [PATCH] refactor(db): unify env getters Signed-off-by: Klagarge --- db/src/main.go | 45 ++++++++++----------------------------------- 1 file changed, 10 insertions(+), 35 deletions(-) diff --git a/db/src/main.go b/db/src/main.go index 1863386..ec97ed6 100644 --- a/db/src/main.go +++ b/db/src/main.go @@ -27,20 +27,10 @@ type ProvenceData struct { } func mqttConnection() *mqtt.MqttGateway { - BrokerUrl, ok := os.LookupEnv("MQTT_BROKER_URL") - if !ok { - BrokerUrl = "tls://localhost:8883" - } + BrokerUrl := getEnv("MQTT_BROKER_URL", "tls://localhost:8883") + Username := getEnv("MQTT_USERNAME", "user") + Password := getEnv("MQTT_PASSWORD", "password") - Username, ok := os.LookupEnv("MQTT_USERNAME") - if !ok { - Username = "user" - } - - Password, ok := os.LookupEnv("MQTT_PASSWORD") - if !ok { - Password = "password" - } ClientId := "mqtt-gateway-test-client_" + fmt.Sprint(time.Now().Unix()) // Create config & gateway @@ -64,18 +54,12 @@ func mqttConnection() *mqtt.MqttGateway { } func influxConnection() *influx.InfluxGateway { - influxUrl, ok := os.LookupEnv("INFLUX_URL") - if !ok { - influxUrl = "https://db.e.kb28.ch:443" - } - influxDatabase, ok := os.LookupEnv("INFLUX_DATABASE") - if !ok { - influxDatabase = "provence" - } + influxUrl := getEnv("INFLUX_URL", "https://db.e.kb28.ch:443") - influxToken := os.Getenv("INFLUX_TOKEN") + influxDatabase := getEnv("INFLUX_DATABASE", "provence") + influxToken := getEnv("INFLUX_TOKEN", "") if influxToken == "" { - if tokenFile := os.Getenv("INFLUX_TOKEN_FILE"); tokenFile != "" { + if tokenFile := getEnv("INFLUX_TOKEN_FILE", "/run/secrets/admin-token"); tokenFile != "" { content, err := os.ReadFile(tokenFile) if err == nil { influxToken = strings.TrimSpace(string(content)) @@ -108,10 +92,7 @@ func influxConnection() *influx.InfluxGateway { // @securityDefinitions.basic BasicAuth func main() { // Load mapping configuration - mappingPath := os.Getenv("MAPPING_CONFIG_PATH") - if mappingPath == "" { - mappingPath = "mapping.json" - } + mappingPath := getEnv("MAPPING_CONFIG_PATH", "mapping.json") mapping, err := LoadMapping(mappingPath) if err != nil { log.Printf("[Main] Warning: could not load mapping file: %v. Using defaults.", err) @@ -124,10 +105,7 @@ func main() { influxGateway := influxConnection() defer influxGateway.Close() - measurementName := os.Getenv("CAMPUS") - if measurementName == "" { - measurementName = "provence" - } + measurementName := getEnv("CAMPUS", "provence") // Create measurement for provence topic provenceMeasurement := point.CreateMeasurement[ProvenceData](measurementName) @@ -177,10 +155,7 @@ func main() { restPassword := getEnv("REST_PASSWORD", "password") restGateway := rest.NewRestGateway(influxGateway, measurementName, restUsername, restPassword) - port, ok := os.LookupEnv("REST_PORT") - if !ok { - port = "8080" - } + port := getEnv("REST_PORT", "8080") log.Printf("[Main] Starting REST Gateway on port %s\n", port) if err := restGateway.Run(":" + port); err != nil {