Base project
This commit is contained in:
commit
922695c23e
9 files changed
+299
No files matched your search
@@ -0,0 +1,50 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
URI string
|
||||
Username string
|
||||
Password string
|
||||
Topics []string
|
||||
}
|
||||
|
||||
func NewConfig(logger *zap.Logger) *Config {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
logger.Warn("Error loading .env file, using environment variables")
|
||||
}
|
||||
|
||||
uri := os.Getenv("MQTT_URI")
|
||||
if uri == "" {
|
||||
logger.Fatal("MQTT_URI is not set")
|
||||
}
|
||||
|
||||
username := os.Getenv("MQTT_USERNAME")
|
||||
if username == "" {
|
||||
logger.Warn("MQTT_USERNAME is not set")
|
||||
}
|
||||
|
||||
password := os.Getenv("MQTT_PASSWORD")
|
||||
if password == "" {
|
||||
logger.Warn("MQTT_PASSWORD is not set")
|
||||
}
|
||||
|
||||
topics := os.Getenv("MQTT_TOPICS")
|
||||
if topics == "" {
|
||||
logger.Warn("MQTT_TOPICS is not set, defaulting to #")
|
||||
topics = "#"
|
||||
}
|
||||
|
||||
return &Config{
|
||||
URI: uri,
|
||||
Username: username,
|
||||
Password: password,
|
||||
Topics: []string{topics},
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user