/
config.go
128 lines (118 loc) · 3.6 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright 2019 InfAI (CC SES)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package config
import (
"encoding/json"
"fmt"
"log"
"os"
"reflect"
"regexp"
"strconv"
"strings"
)
type Config struct {
MongoUrl string
MongoTable string
DeviceStateCollection string
HubStateCollection string
InfluxdbUrl string
InfluxdbDb string
InfluxdbUser string
InfluxdbPw string
InfluxdbTimeout int64
DeviceLogTopic string
HubLogTopic string
DeviceTopic string
HubTopic string
KafkaUrl string
KafkaGroupId string
Debug bool
}
//loads config from json in location and used environment variables (e.g KafkaUrl --> ZOOKEEPER_URL)
func Load(location string) (config Config, err error) {
file, error := os.Open(location)
if error != nil {
log.Println("error on config load: ", error)
return config, error
}
decoder := json.NewDecoder(file)
error = decoder.Decode(&config)
if error != nil {
log.Println("invalid config json: ", error)
return config, error
}
handleEnvironmentVars(&config)
return config, nil
}
var camel = regexp.MustCompile("(^[^A-Z]*|[A-Z]*)([A-Z][^A-Z]+|$)")
func fieldNameToEnvName(s string) string {
var a []string
for _, sub := range camel.FindAllStringSubmatch(s, -1) {
if sub[1] != "" {
a = append(a, sub[1])
}
if sub[2] != "" {
a = append(a, sub[2])
}
}
return strings.ToUpper(strings.Join(a, "_"))
}
// preparations for docker
func handleEnvironmentVars(config *Config) {
configValue := reflect.Indirect(reflect.ValueOf(config))
configType := configValue.Type()
for index := 0; index < configType.NumField(); index++ {
fieldName := configType.Field(index).Name
envName := fieldNameToEnvName(fieldName)
envValue := os.Getenv(envName)
if envValue != "" {
fmt.Println("use environment variable: ", envName, " = ", envValue)
if configValue.FieldByName(fieldName).Kind() == reflect.Int64 {
i, _ := strconv.ParseInt(envValue, 10, 64)
configValue.FieldByName(fieldName).SetInt(i)
}
if configValue.FieldByName(fieldName).Kind() == reflect.String {
configValue.FieldByName(fieldName).SetString(envValue)
}
if configValue.FieldByName(fieldName).Kind() == reflect.Bool {
b, _ := strconv.ParseBool(envValue)
configValue.FieldByName(fieldName).SetBool(b)
}
if configValue.FieldByName(fieldName).Kind() == reflect.Float64 {
f, _ := strconv.ParseFloat(envValue, 64)
configValue.FieldByName(fieldName).SetFloat(f)
}
if configValue.FieldByName(fieldName).Kind() == reflect.Slice {
val := []string{}
for _, element := range strings.Split(envValue, ",") {
val = append(val, strings.TrimSpace(element))
}
configValue.FieldByName(fieldName).Set(reflect.ValueOf(val))
}
if configValue.FieldByName(fieldName).Kind() == reflect.Map {
value := map[string]string{}
for _, element := range strings.Split(envValue, ",") {
keyVal := strings.Split(element, ":")
key := strings.TrimSpace(keyVal[0])
val := strings.TrimSpace(keyVal[1])
value[key] = val
}
configValue.FieldByName(fieldName).Set(reflect.ValueOf(value))
}
}
}
}