61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
type Config struct {
|
|
Database struct {
|
|
Host string `json:"host"`
|
|
Port string `json:"port"`
|
|
User string `json:"user"`
|
|
Password string `json:"password"`
|
|
DBName string `json:"dbname"`
|
|
SSLMode string `json:"sslmode"`
|
|
} `json:"database"`
|
|
Temp string `json:"temp"`
|
|
}
|
|
|
|
func LoadConfig(file string) (Config, error) {
|
|
var config Config
|
|
configFile, err := os.Open(file)
|
|
if err != nil {
|
|
return config, fmt.Errorf("failed to open config file: %w", err)
|
|
}
|
|
defer configFile.Close()
|
|
|
|
jsonParser := json.NewDecoder(configFile)
|
|
if err := jsonParser.Decode(&config); err != nil {
|
|
return config, fmt.Errorf("failed to parse config file: %w", err)
|
|
}
|
|
|
|
// Override with environment variables if they are set
|
|
if host := os.Getenv("DB_HOST"); host != "" {
|
|
config.Database.Host = host
|
|
}
|
|
if port := os.Getenv("DB_PORT"); port != "" {
|
|
config.Database.Port = port
|
|
}
|
|
if user := os.Getenv("DB_USER"); user != "" {
|
|
config.Database.User = user
|
|
}
|
|
if password := os.Getenv("DB_PASSWORD"); password != "" {
|
|
config.Database.Password = password
|
|
}
|
|
if dbname := os.Getenv("DB_NAME"); dbname != "" {
|
|
config.Database.DBName = dbname
|
|
}
|
|
if sslmode := os.Getenv("DB_SSLMODE"); sslmode != "" {
|
|
config.Database.SSLMode = sslmode
|
|
}
|
|
if temp := os.Getenv("TEMP"); temp != "" {
|
|
config.Temp = temp
|
|
}
|
|
log.Println("Temp value:", config.Temp)
|
|
|
|
return config, nil
|
|
}
|