/
configwrapper.go
97 lines (85 loc) · 2.62 KB
/
configwrapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package config
import (
//Standard library packages
"path/filepath"
"io/ioutil"
"log"
"flag"
//Third party packages
"gopkg.in/yaml.v2"
)
var configWrapper *ConfigWrapper
func InitConfig(env string, configPath string) {
if configWrapper == nil {
filename, _ := filepath.Abs(configPath)
yamlFile, err := ioutil.ReadFile(filename)
var config Config
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
log.Println(err)
panic(err)
}
configWrapper = &ConfigWrapper{}
switch env {
case "development":
configWrapper.currentConfig = config.Development
case "test":
configWrapper.currentConfig = config.Test
case "production":
configWrapper.currentConfig = config.Production
}
}
}
func GetConfigWrapper() *ConfigWrapper {
if configWrapper == nil {
configPath := flag.String("config", "empty", "configuration file (.yml) full path")
env := flag.String("env", "wrong", "environment: development | test | production")
flag.Parse()
filename, _ := filepath.Abs(*configPath)
log.Println("trying to read file ", filename)
yamlFile, err := ioutil.ReadFile(filename)
var config Config
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
log.Println(err)
panic(err)
}
configWrapper = &ConfigWrapper{}
switch *env {
case "development":
configWrapper.currentConfig = config.Development
case "test":
configWrapper.currentConfig = config.Test
case "production":
configWrapper.currentConfig = config.Production
}
}
return configWrapper
}
type ConfigWrapper struct {
Config Config
currentConfig EnvConfig
}
type Config struct {
Development EnvConfig `yaml:"development"`
Test EnvConfig `yaml:"test"`
Production EnvConfig `yaml:"production"`
}
type EnvConfig struct {
MongoHost string `yaml:"mongo_host"`
Env string `yaml:"env"`
EmailServerAddress string `yaml:"email_server_address"`
EmailServerPort int `yaml:"email_server_port"`
EmailServerUsername string `yaml:"email_server_username"`
EmailServerPassword string `yaml:"email_server_password"`
EmailServerFrom string `yaml:"email_server_from"`
EmailServerBcc string `yaml:"email_server_bcc"`
LogPath string `yaml:"log_path"`
AdminAuth string `yaml:"admin_auth"`
DashboardBaseUrl string `yaml:"dashboard_base_url"`
SessionKey string `yaml:"session_key"`
GuestWebsiteUrl string `yaml:"guest_website_url"`
}
func (configWrapper *ConfigWrapper) GetCurrent() *EnvConfig {
return &configWrapper.currentConfig
}