-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
68 lines (57 loc) · 1.56 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
64
65
66
67
68
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
)
var (
currentPath, _ = os.Getwd()
)
type Config struct {
Port int `json:"port,omitempty"`
MQ *MQConfig `json:"mq,omitempty"`
}
type MQConfig struct {
Host string `json:"host"`
Port int `json:"port"`
Username string `json:"username"`
Password string `json:"password"`
InputQueue string `json:"input_queue"`
OutputExchange string `json:"output_exchange"`
}
func loadConfig() (*Config, error) {
// Config object to set defaults
config := &Config{
Port: StringToIntUnsafe(os.Getenv("PORT"), 8080),
MQ: &MQConfig{
Host: os.Getenv("MQ_HOST"),
Port: StringToIntUnsafe(os.Getenv("MQ_PORT"), 0),
InputQueue: os.Getenv("MQ_INPUT_QUEUE"),
OutputExchange: os.Getenv("MQ_OUTPUT_EXCHANGE"),
Username: os.Getenv("MQ_USERNAME"),
Password: os.Getenv("MQ_PASSWORD"),
},
}
configPath := currentPath + string(os.PathSeparator) + "config" + string(os.PathSeparator) + "config.json"
file, err := os.Open(configPath)
if err != nil {
return config, fmt.Errorf("couldn't open configfile %s with err: %s", configPath, err.Error())
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
return config, fmt.Errorf("couldn't decode configfile %s with err: %s", configPath, err.Error())
}
return config, nil
}
func StringToIntUnsafe(input string, defaultVal int) int {
if input == "" {
return defaultVal
}
output, err := strconv.Atoi(input)
if err != nil {
panic(err)
}
return output
}