feat: add database configuration
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
Server ServerConfig `toml:"server"`
|
Server ServerConfig `toml:"server"`
|
||||||
MQTT MQTTConfig `toml:"mqtt"`
|
MQTT MQTTConfig `toml:"mqtt"`
|
||||||
|
Database DatabaseConfig `toml:"database"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerConfig struct {
|
type ServerConfig struct {
|
||||||
@@ -28,6 +29,14 @@ type MQTTConfig struct {
|
|||||||
Topic string `toml:"topic"`
|
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
|
// Load loads configuration from the given path. If path is empty, it tries
|
||||||
// to load ./config.toml or falls back to environment defaults.
|
// to load ./config.toml or falls back to environment defaults.
|
||||||
func Load(path string) (*Config, error) {
|
func Load(path string) (*Config, error) {
|
||||||
@@ -42,6 +51,12 @@ func Load(path string) (*Config, error) {
|
|||||||
ClientID: "lambda-iot-core",
|
ClientID: "lambda-iot-core",
|
||||||
Topic: "lambda/iot",
|
Topic: "lambda/iot",
|
||||||
},
|
},
|
||||||
|
Database: DatabaseConfig{
|
||||||
|
Host: "localhost",
|
||||||
|
Port: 3306,
|
||||||
|
User: "root",
|
||||||
|
Name: "lambdaiot",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to load TOML if provided or present
|
// try to load TOML if provided or present
|
||||||
@@ -90,5 +105,23 @@ func Load(path string) (*Config, error) {
|
|||||||
cfg.MQTT.Topic = v
|
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
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user