-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
109 lines (90 loc) · 2.25 KB
/
configuration.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
package configs
import (
"fmt"
"reflect"
"strings"
"unicode"
"go.uber.org/zap"
)
const (
//API the base path for the services
API = "/api"
// APIHealth health check path
APIHealth = API + "/health"
// APITemperature weather api path
APITemperature = API + "/temperature"
)
const (
secret = "secret"
pass = "pass"
apikey = "apikey"
)
type Config struct {
Log struct {
Level string `env:"SUNNY_LOG_LEVEL"`
}
Server struct {
Port int `env:"SUNNY_SERVER_PORT"`
Swagger struct {
Enabled bool `env:"SUNNY_SWAGGER_ENABLED"`
Path string `env:"SUNNY_SWAGGER_PATH"`
}
Geolocation struct {
Internal bool `env:"SUNNY_GEOLOCATION_INTERNAL"`
Path string `env:"SUNNY_GEOLOCATION_PATH"`
}
}
External struct {
Geolocation struct {
Host string `env:"SUNNY_EXTERNAL_GEOLOCATION_HOST"`
ApiKey string `env:"SUNNY_EXTERNAL_GEOLOCATION_APIKEY"`
}
Weather struct {
Host string `env:"SUNNY_EXTERNAL_WEATHER_HOST"`
ApiKey string `env:"SUNNY_EXTERNAL_WEATHER_APIKEY"`
}
}
}
func (c *Config) PrintConfigs(l *zap.Logger, version string) {
l.Info("starting Sunshine monitor with the following configurations",
zap.String("version", version))
printConfigs(l, *c, "")
}
func printConfigs(l *zap.Logger, obj interface{}, prefix string) {
var cfgs []reflect.StructField
var cfgtype = reflect.TypeOf(obj)
getPublicFields(&cfgs, &cfgtype)
for i, c := range cfgs {
if shouldOmmit(c.Name) {
l.Info(fmt.Sprintf("%s: <ommited>", c.Name))
} else {
if c.Type.Kind() == reflect.Struct {
printConfigs(l, reflect.ValueOf(obj).Field(i).Interface(), c.Name)
} else {
l.Info(fmt.Sprintf("%s.%s: %v",
prefix, c.Name, reflect.ValueOf(obj).Field(i).Interface()))
}
}
}
}
func getPublicFields(arr *[]reflect.StructField, typ *reflect.Type) {
for i := 0; i < (*typ).NumField(); i++ {
isPublic := unicode.IsUpper([]rune((*typ).Field(i).Name)[0])
if isPublic {
*arr = append(*arr, (*typ).Field(i))
}
}
}
// ommit passwords and secrets in log
func shouldOmmit(cfg string) bool {
if strings.Contains(strings.ToLower(cfg), secret) {
return true
}
if strings.Contains(strings.ToLower(cfg), pass) {
return true
}
if strings.Contains(strings.ToLower(cfg), apikey) {
return true
}
return false
}