feat: add database configuration

This commit is contained in:
2026-01-04 13:45:17 +01:00
parent 2878d6a59b
commit 55d8bd53df
+35 -2
View File
@@ -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
}