-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
135 lines (117 loc) · 3.02 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
package gott
import (
"io/ioutil"
"log"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/yaml.v2"
)
type tlsConfig struct {
Listen, Cert, Key string
}
func (t tlsConfig) Enabled() bool {
return t.Listen != "" && t.Cert != "" && t.Key != ""
}
type wssConfig struct {
Listen, Cert, Key string
}
func (t wssConfig) Enabled() bool {
return t.Listen != "" && t.Cert != "" && t.Key != ""
}
type loggingConfig struct {
LogLevel string `yaml:"log_level"`
Filename string
MaxSize int `yaml:"max_size"`
MaxBackups int `yaml:"max_backups"`
MaxAge int `yaml:"max_age"`
EnableCompression bool `yaml:"enable_compression"`
logLevel zapcore.Level
}
type webSocketsConfig struct {
Listen string
Path string
RejectEmptyOrigin bool `yaml:"reject_empty_origin"`
Origins []string
WSS wssConfig `yaml:"wss"`
}
// Config holds the parsed config file
type Config struct {
ConfigPath string
Listen string
Tls tlsConfig
WebSockets webSocketsConfig `yaml:"websockets"`
Logging loggingConfig
Plugins []interface{}
pluginNames []string
pluginConfig map[string]map[interface{}]interface{}
}
func defaultConfig() Config {
return Config{
Listen: ":1883",
Tls: tlsConfig{Listen: ":8883", Cert: "", Key: ""},
WebSockets: webSocketsConfig{
Listen: "",
Path: "/ws",
},
Logging: loggingConfig{
LogLevel: "error",
Filename: "gott.log",
MaxSize: 5,
MaxBackups: 20,
MaxAge: 30,
EnableCompression: true,
},
ConfigPath: "config.yml",
}
}
func (c *Config) loadConfig() error {
file, err := ioutil.ReadFile(c.ConfigPath)
if err != nil {
log.Println("Error opening config file:", err)
log.Println("Creating default config.yml file")
if err = ioutil.WriteFile("config.yml", []byte(defaultConfigContent), 0664); err != nil {
log.Fatalln("Error creating default config.yml file:", err)
}
file = []byte(defaultConfigContent)
}
if err = yaml.Unmarshal(file, c); err != nil {
return err
}
switch c.Logging.LogLevel {
case "debug":
c.Logging.logLevel = zap.DebugLevel
case "info":
c.Logging.logLevel = zap.InfoLevel
case "error":
c.Logging.logLevel = zap.ErrorLevel
case "fatal":
c.Logging.logLevel = zap.FatalLevel
default:
c.Logging.LogLevel = "error"
c.Logging.logLevel = zap.ErrorLevel
}
c.pluginConfig = make(map[string]map[interface{}]interface{})
for _, item := range c.Plugins {
if str, ok := item.(string); ok {
c.pluginNames = append(c.pluginNames, str)
} else if m, ok := item.(map[interface{}]interface{}); ok {
for k, v := range m {
if kstr, ok := k.(string); ok {
if vm, ok := v.(map[interface{}]interface{}); ok {
c.pluginNames = append(c.pluginNames, kstr)
c.pluginConfig[kstr] = vm
}
}
break
}
}
}
return nil
}
func newConfig() (Config, error) {
cnf := defaultConfig()
if err := cnf.loadConfig(); err != nil {
return cnf, err
}
return cnf, nil
}