From 55d8bd53df656359326b022d452f15a4708b8e82 Mon Sep 17 00:00:00 2001 From: Kristjan Komlosi Date: Sun, 4 Jan 2026 13:45:17 +0100 Subject: [PATCH] feat: add database configuration --- internal/config/config.go | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index d636527..0e8d61c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -10,8 +10,9 @@ import ( // Config holds application configuration loaded from TOML type Config struct { - Server ServerConfig `toml:"server"` - MQTT MQTTConfig `toml:"mqtt"` + Server ServerConfig `toml:"server"` + MQTT MQTTConfig `toml:"mqtt"` + Database DatabaseConfig `toml:"database"` } type ServerConfig struct { @@ -28,6 +29,14 @@ type MQTTConfig struct { Topic string `toml:"topic"` } +type DatabaseConfig struct { + Host string `toml:"host"` + Port int `toml:"port"` + User string `toml:"user"` + Password string `toml:"password"` + Name string `toml:"name"` +} + // Load loads configuration from the given path. If path is empty, it tries // to load ./config.toml or falls back to environment defaults. func Load(path string) (*Config, error) { @@ -42,6 +51,12 @@ func Load(path string) (*Config, error) { ClientID: "lambda-iot-core", Topic: "lambda/iot", }, + Database: DatabaseConfig{ + Host: "localhost", + Port: 3306, + User: "root", + Name: "lambdaiot", + }, } // try to load TOML if provided or present @@ -90,5 +105,23 @@ func Load(path string) (*Config, error) { cfg.MQTT.Topic = v } + if v := os.Getenv("DB_HOST"); v != "" { + cfg.Database.Host = v + } + if v := os.Getenv("DB_PORT"); v != "" { + if p, err := strconv.Atoi(v); err == nil { + cfg.Database.Port = p + } + } + if v := os.Getenv("DB_USER"); v != "" { + cfg.Database.User = v + } + if v := os.Getenv("DB_PASSWORD"); v != "" { + cfg.Database.Password = v + } + if v := os.Getenv("DB_NAME"); v != "" { + cfg.Database.Name = v + } + return cfg, nil }