-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.go
67 lines (56 loc) · 1.32 KB
/
init.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
package main
import (
"encoding/json"
"io/ioutil"
"os"
)
// configuration is a structure that specifies the contents of config.json.
type configuration struct {
ServiceKey string `json:"serviceKey"`
ServerKey string `json:"serverKey"`
Database Database `json:"database"`
}
type Database struct {
User string `json:"user"`
Passwd string `json:"passwd"`
IPAddr string `json:"ip_addr"`
Port string `json:"port"`
DBname string `json:"dbname"`
}
// config is a variable that stores configuration information.
var config configuration
// init is an initialization function.
func init() {
setUpConfig()
setUpUsingEnv()
}
func setUpConfig() {
if _, err := os.Stat("./configs"); err != nil {
return
}
file, err := ioutil.ReadFile("./configs/config.json") // read config.json
if err != nil {
return
}
err = json.Unmarshal(file, &config) // store loaded json at config variable
if err != nil {
return
}
}
func setUpUsingEnv() {
if config.ServiceKey == "" {
config.ServiceKey = os.Getenv("serviceKey")
}
if config.ServerKey == "" {
config.ServerKey = os.Getenv("serverKey")
}
if config.Database == (Database{}) {
config.Database = Database{
os.Getenv("databaseUser"),
os.Getenv("databasePasswd"),
os.Getenv("databaseIpAddr"),
os.Getenv("databasePort"),
os.Getenv("databaseDbname"),
}
}
}