-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
95 lines (76 loc) · 2.05 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
// Copyright (c) 2021 Andres More
// config
package main
import (
"io"
"os"
"strings"
"github.com/fsnotify/fsnotify"
log "github.com/sirupsen/logrus"
cfg "github.com/spf13/viper"
_ "github.com/spf13/viper/remote"
)
// https://github.com/dgrijalva/jwt-go
// SetConfig sets configuration
func SetConfig() {
cfg.SetDefault("LogLevel", "Info")
cfg.SetDefault("SchemaName", "objects.json")
cfg.SetDefault("DbUri", "file::memory:?cache=shared")
cfg.SetDefault("HttpPort", "8080")
cfg.SetDefault("UseSSL", false)
cfg.SetDefault("HttpsPort", "8443")
cfg.SetDefault("CertFile", "api.cer")
cfg.SetDefault("KeyFile", "api.key")
cfg.SetDefault("URL", "https://localhost.com")
cfg.SetDefault("QueryLimit", "512")
cfg.SetDefault("JwtSecret", "password")
cfg.SetDefault("MaxAllowed", 20)
cfg.SetDefault("AccessCidr", "0.0.0.0/0")
cfg.SetConfigName("api")
cfg.AddConfigPath(".")
cfg.AutomaticEnv()
cfg.SetEnvPrefix("api")
log.SetReportCaller(true)
log.SetFormatter(&log.JSONFormatter{})
file, err := os.OpenFile("api.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.WithFields(log.Fields{
"event": "config",
"topic": "logging",
"key": err.Error(),
}).Fatal("Could not open logging file")
}
log.SetOutput(io.MultiWriter(os.Stdout, file))
err2 := cfg.ReadInConfig()
if err2 != nil {
log.WithFields(log.Fields{
"event": "config",
"topic": "read",
"key": err2.Error(),
}).Warn("could not read config")
}
setLogLevel()
cfg.WatchConfig()
cfg.OnConfigChange(handleConfigChange)
}
func setLogLevel() {
levels := map[string]log.Level{
"debug": log.DebugLevel,
"info": log.InfoLevel,
"warn": log.WarnLevel,
"error": log.ErrorLevel,
"fatal": log.FatalLevel,
"panic": log.PanicLevel,
}
level := levels[strings.ToLower(cfg.GetString("LogLevel"))]
log.SetLevel(level)
}
// handleConfigChange handle configuration changes
func handleConfigChange(e fsnotify.Event) {
log.WithFields(log.Fields{
"event": "config",
"topic": "change",
"key": e.Name,
}).Info("configuration has changed")
setLogLevel()
}