Restructure again

This commit is contained in:
Eden Kirin
2023-10-26 16:49:24 +02:00
parent 7512d75a4d
commit dd671d561c
5 changed files with 32 additions and 27 deletions

99
app/lib/cfg/config.go Normal file
View File

@ -0,0 +1,99 @@
package cfg
import (
"fmt"
"os"
"github.com/kelseyhightower/envconfig"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
type configStruct struct {
Application struct {
SecretKey string `yaml:"secretKey" json:"-"`
LogLevel string `yaml:"logLevel"`
LogFile string `yaml:"logFile"`
Debug bool `yaml:"debug"`
DebugSQL bool `yaml:"debugSQL"`
DisableSendMail bool `yaml:"disableSendMail"`
IsProduction bool `yaml:"isProduction"`
LoopDelay uint32 `yaml:"loopDelay"`
} `yaml:"application"`
Database struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
Name string `yaml:"name"`
Username string `yaml:"username"`
Password string `yaml:"password" json:"-"`
} `yaml:"database"`
Redis struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Username string `yaml:"username"`
Password string `yaml:"password" json:"-"`
Database string `yaml:"database"`
Prefix string `yaml:"prefix"`
} `yaml:"redis"`
SMTP struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Username string `yaml:"username"`
Password string `yaml:"password" json:"-"`
StartTLS bool `yaml:"startTLS"`
UseTLS bool `yaml:"useTLS"`
FromEmail string `yaml:"fromEmail"`
} `yaml:"smtp"`
}
const DEFAULT_CONFIG_FILE = "config.yaml"
const ENV_PREFIX = "MAILSENDER"
var Config configStruct
var log = logrus.New()
func processError(err error) {
log.Error("Config file error: " + err.Error())
os.Exit(2)
}
func readFile(cfgFile string, cfg *configStruct) {
f, err := os.Open(cfgFile)
if err != nil {
processError(err)
} else {
decoder := yaml.NewDecoder(f)
err = decoder.Decode(cfg)
if err != nil {
processError(err)
}
}
defer f.Close()
}
func readEnv(cfg *configStruct) {
err := envconfig.Process(ENV_PREFIX, cfg)
if err != nil {
processError(err)
}
}
func InitCfg() {
cfgFile := os.Getenv("MAILSENDER_CONFIG")
if cfgFile == "" {
cfgFile = DEFAULT_CONFIG_FILE
}
readFile(cfgFile, &Config)
readEnv(&Config)
maskedCfg := Config
maskedCfg.Database.Password = "**password hidden**"
maskedCfg.Redis.Password = "**password hidden**"
maskedCfg.SMTP.Password = "**password hidden**"
maskedCfg.Application.SecretKey = "**secret key hidden**"
fmt.Println("--- CONFIG -------------------------------")
fmt.Println(maskedCfg)
fmt.Println("------------------------------------------")
}