-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
63 lines (50 loc) · 1.1 KB
/
config.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
package core
import (
"errors"
"os"
"strings"
"github.com/spf13/viper"
)
type WebServerConfig struct {
Host string `mapstructure:"host"`
Port string `mapstructure:"port"`
}
var mainConfig = new(MainConfig)
func readConfigFromFile(filePath string) (err error) {
v := viper.New()
filePart := strings.Split(filePath, ".")
if len(filePart) != 2 {
return errors.New("Unacceptable file path format. Require *.*")
}
v.SetConfigName(filePart[0])
v.SetConfigType(filePart[1])
// add config path
v.AddConfigPath(".")
v.AddConfigPath("../")
v.AddConfigPath("../../")
if err := v.ReadInConfig(); err != nil {
return err
}
if err := v.Unmarshal(mainConfig); err != nil {
return err
}
return nil
}
func readConfigFromENV() {
dbHost := os.Getenv("DBHOST")
if len(dbHost) > 0 {
mainConfig.DB.Host = dbHost
}
webHost := os.Getenv("WEBHOST")
if len(webHost) > 0 {
mainConfig.Web.Host = webHost
}
webPort := os.Getenv("WEBPORT")
if len(webPort) > 0 {
mainConfig.Web.Port = webPort
}
rabbitMQURL := os.Getenv("RABBITMQURL")
if len(rabbitMQURL) > 0 {
mainConfig.RabbitMQURL = rabbitMQURL
}
}