Files
repo-pattern/app/lib/cfg/cfg.go
Eden Kirin 21dcabe180 Initial
2024-06-18 22:01:31 +02:00

107 lines
2.5 KiB
Go

package cfg
import (
"fmt"
"os"
"github.com/kelseyhightower/envconfig"
"gopkg.in/yaml.v3"
)
type ServiceConfig struct {
Address string `yaml:"address"`
Port int `yaml:"port"`
}
type DatabaseConfig struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
Name string `yaml:"name"`
Username string `yaml:"username"`
Password string `yaml:"password" json:"-"`
DebugSQL bool `yaml:"debugSQL"`
}
type ApplicationConfig struct {
LogLevel string `yaml:"logLevel"`
LogFile string `yaml:"logFile"`
Debug bool `yaml:"debug"`
DebugPrint bool `yaml:"debugPrint"`
DisableSendMail bool `yaml:"disableSendMail"`
IsProduction bool `yaml:"isProduction"`
MetricsPrefix string `yaml:"metricsPrefix"`
}
type S3StorageConfig struct {
AccessKey string `yaml:"accessKey"`
SecretKey string `yaml:"secretKey"`
RegionName string `yaml:"regionName"`
EndpointUrl string `yaml:"endpointUrl"`
BucketName string `yaml:"bucketName"`
CertPath string `yaml:"certPath"`
}
type CertConfig struct {
FinaDemoCaCert string `yaml:"finaDemoCaCert"`
FinaProdCaCert string `yaml:"finaProdCaCert"`
DemoCertOib string `yaml:"demoCertOib"`
}
type configStruct struct {
Service ServiceConfig `yaml:"service"`
Database DatabaseConfig `yaml:"database"`
Application ApplicationConfig `yaml:"application"`
S3Storage S3StorageConfig `yaml:"s3storage"`
Cert CertConfig `yaml:"cert"`
}
const ENV_PREFIX = "FISKALATOR"
const ENV_CONFIG = ENV_PREFIX + "_CONFIG"
const DEFAULT_CONFIG_FILE = "config.yaml"
var Config configStruct
func panicWithError(err error) {
panic("Config file error: " + err.Error())
}
func readFile(cfgFile string, cfg *configStruct) {
f, err := os.Open(cfgFile)
if err != nil {
panicWithError(err)
}
defer f.Close()
decoder := yaml.NewDecoder(f)
err = decoder.Decode(cfg)
if err != nil {
panicWithError(err)
}
}
func readEnv(cfg *configStruct) {
err := envconfig.Process(ENV_PREFIX, cfg)
if err != nil {
panicWithError(err)
}
}
func Init() {
cfgFile := os.Getenv(ENV_CONFIG)
if cfgFile == "" {
cfgFile = DEFAULT_CONFIG_FILE
}
readFile(cfgFile, &Config)
readEnv(&Config)
maskedCfg := Config
maskedCfg.Database.Password = "**password hidden**"
maskedCfg.S3Storage.AccessKey = "**access key hidden**"
maskedCfg.S3Storage.SecretKey = "**secret key hidden**"
fmt.Println("--- CONFIG -------------------------------")
fmt.Printf("%+v\n", maskedCfg)
fmt.Println("------------------------------------------")
}