-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
129 lines (113 loc) · 3.21 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
129
/*
* Copyright 2018 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 main
import (
"encoding/json"
"fmt"
"log"
"os"
"reflect"
"regexp"
"strconv"
"strings"
)
type ConfigStruct struct {
MongoUrl string
MongoTable string
ConnectorGatewayCollection string
ConnectorDeviceCollection string
DeviceStateCollection string
GatewayStateCollection string
ServerPort string
LogLevel string
PermissionsUrl string
AmqpUrl string
AmqpReconnectTimeout int64
AmqpConsumerName string
AmqpMsgLogging string
InfluxdbUrl string
InfluxdbDb string
InfluxdbUser string
InfluxdbPw string
InfluxdbTimeout int64
ConnectorLogTopic string
DeviceLogTopic string
GatewayLogTopic string
ForceUser string
ForceAuth string
}
type ConfigType *ConfigStruct
var Config ConfigType
func LoadConfig(location string) error {
file, error := os.Open(location)
if error != nil {
log.Println("error on config load: ", error)
return error
}
decoder := json.NewDecoder(file)
configuration := ConfigStruct{}
error = decoder.Decode(&configuration)
if error != nil {
log.Println("invalid config json: ", error)
return error
}
HandleEnvironmentVars(&configuration)
HandleDefaultValues(&configuration)
Config = &configuration
return nil
}
func HandleDefaultValues(config ConfigType) {
}
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 ConfigType) {
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.Slice {
val := []string{}
for _, element := range strings.Split(envValue, ",") {
val = append(val, strings.TrimSpace(element))
}
configValue.FieldByName(fieldName).Set(reflect.ValueOf(val))
}
}
}
}