100 lines
2.4 KiB
Go
100 lines
2.4 KiB
Go
package common
|
|
|
|
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("------------------------------------------")
|
|
}
|