Restructure app

This commit is contained in:
Eden Kirin
2023-10-23 23:05:18 +02:00
parent f0e323f2ce
commit 002f6a3d06
11 changed files with 250 additions and 10 deletions

88
app/cfg/cfg.go Normal file
View File

@ -0,0 +1,88 @@
package cfg
import (
"fmt"
"os"
"github.com/kelseyhightower/envconfig"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
type configStruct struct {
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"`
Application struct {
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"`
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 Init() {
cfgFile := os.Getenv("MAILSENDER_CONFIG")
if cfgFile == "" {
cfgFile = DEFAULT_CONFIG_FILE
}
readFile(cfgFile, &Config)
readEnv(&Config)
maskedCfg := Config
maskedCfg.Database.Password = "**password hidden**"
maskedCfg.SMTP.Password = "**password hidden**"
fmt.Println("--- CONFIG -------------------------------")
fmt.Println(maskedCfg)
fmt.Println("------------------------------------------")
}