-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
115 lines (99 loc) · 2.46 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
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 []string
}
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
}
return nil
}
func newConfig() (Config, error) {
cnf := defaultConfig()
if err := cnf.loadConfig(); err != nil {
return cnf, err
}
return cnf, nil
}