This commit is contained in:
$(pass /github/name)
2024-07-02 00:27:43 +02:00
parent fbb3bdfcb3
commit 9bd8d48243
11 changed files with 251 additions and 102 deletions

View File

@@ -1,20 +1,38 @@
package config
import (
"os"
"encoding/json"
"log"
"os"
"path/filepath"
)
type DatabaseConfig struct {
DBPath string `json:"DBPath"`
}
type Config struct {
DBUser string
DBPassword string
DBName string
DB DatabaseConfig `json:"db"`
}
func LoadConfig() *Config {
return &Config{
DBUser: os.Getenv("DB_USER"),
DBPassword: os.Getenv("DB_PASSWORD"),
DBName: os.Getenv("DB_NAME"),
}
}
path, err := os.Getwd()
if err != nil {
log.Fatalf("could not get working directory: %v", err)
}
configFile, err := os.Open(filepath.Join(path, "configs", "config.json"))
if err != nil {
log.Fatalf("could not open config file: %v", err)
}
defer configFile.Close()
decoder := json.NewDecoder(configFile)
config := &Config{}
err = decoder.Decode(config)
if err != nil {
log.Fatalf("could not decode config file: %v", err)
}
return config
}