-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
148 lines (137 loc) · 5.09 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* 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"
"net/http"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
type ConfigStruct struct {
ApiPort string `json:"api_port"`
KafkaUrl string `json:"kafka_url"`
DeviceRepoUrl string `json:"device_repo_url"`
ProcessRepoUrl string `json:"process_repo_url"`
PermSearchUrl string `json:"perm_search_url"`
DeviceSelectionUrl string `json:"device_selection_url"`
MongoUrl string `json:"mongo_url"`
MongoTable string `json:"mongo_table"`
MongoDeploymentCollection string `json:"mongo_deployment_collection"`
MongoDependenciesCollection string `json:"mongo_dependencies_collection"`
ConsumerGroup string `json:"consumer_group"`
Debug bool `json:"debug"`
ConnectivityTest bool `json:"connectivity_test"`
DeploymentTopic string `json:"deployment_topic"`
UsersTopic string `json:"users_topic"`
NotificationUrl string `json:"notification_url"`
ImportDeployUrl string `json:"import_deploy_url"`
CacheInvalidationKafkaTopics []string `json:"cache_invalidation_kafka_topics"`
EnableDeviceGroupsForTasks bool `json:"enable_device_groups_for_tasks"`
EnableDeviceGroupsForEvents bool `json:"enable_device_groups_for_events"`
EnableImportsForEvents bool `json:"enable_imports_for_events"`
EnableModifiedDevicesForDeploymentOptions bool `json:"enable_modified_devices_for_deployment_options"`
HttpClientTimeout string `json:"http_client_timeout"`
HttpServerTimeout string `json:"http_server_timeout"`
HttpServerReadTimeout string `json:"http_server_read_timeout"`
}
type Config = *ConfigStruct
func LoadConfig(location string) (config Config, err error) {
file, err := os.Open(location)
if err != nil {
log.Println("error on config load: ", err)
return config, err
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
log.Println("invalid config json: ", err)
return config, err
}
HandleEnvironmentVars(config)
setDefaultHttpClient(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.Float64 {
f, _ := strconv.ParseFloat(envValue, 64)
configValue.FieldByName(fieldName).SetFloat(f)
}
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.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))
}
}
}
}
func setDefaultHttpClient(config Config) {
var err error
http.DefaultClient.Timeout, err = time.ParseDuration(config.HttpClientTimeout)
if err != nil {
log.Println("WARNING: invalid http timeout --> no timeouts\n", err)
}
}