This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
config.go
203 lines (179 loc) · 5.24 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package app
import (
"errors"
"fmt"
"github.com/fsnotify/fsnotify"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"os"
"path/filepath"
)
// Config represents the configuration of the server application.
type Config struct {
Address string
Port string
Prefix string
Dir string
TLS *TLS
Log Logging
Realm string
Users map[string]*UserInfo
Cors Cors
}
// Logging allows definition for logging each CRUD method.
type Logging struct {
Error bool
Create bool
Read bool
Update bool
Delete bool
}
// TLS allows specification of a certificate and private key file.
type TLS struct {
CertFile string
KeyFile string
}
// UserInfo allows storing of a password and user directory.
type UserInfo struct {
Password string
Subdir *string
}
// Cors contains settings related to Cross-Origin Resource Sharing (CORS)
type Cors struct {
Origin string
Credentials bool
}
// ParseConfig parses the application configuration an sets defaults.
func ParseConfig(path string) *Config {
var cfg = &Config{}
setDefaults()
if path != "" {
viper.SetConfigFile(path)
} else {
viper.SetConfigName("config")
viper.AddConfigPath("./config")
viper.AddConfigPath("$HOME/.swd")
viper.AddConfigPath("$HOME/.dave")
viper.AddConfigPath(".")
}
err := viper.ReadInConfig()
if err != nil {
log.Fatal(fmt.Errorf("Fatal error config file: %s", err))
}
err = viper.Unmarshal(&cfg)
if err != nil {
log.Fatal(fmt.Errorf("Fatal error parsing config file: %s", err))
}
if cfg.TLS != nil {
if _, err := os.Stat(cfg.TLS.KeyFile); err != nil {
log.Fatal(fmt.Errorf("TLS keyFile doesn't exist: %s", err))
}
if _, err := os.Stat(cfg.TLS.CertFile); err != nil {
log.Fatal(fmt.Errorf("TLS certFile doesn't exist: %s", err))
}
}
viper.WatchConfig()
viper.OnConfigChange(cfg.handleConfigUpdate)
cfg.ensureUserDirs()
return cfg
}
// setDefaults adds some default values for the configuration
func setDefaults() {
viper.SetDefault("Address", "127.0.0.1")
viper.SetDefault("Port", "8000")
viper.SetDefault("Prefix", "")
viper.SetDefault("Dir", "/tmp")
viper.SetDefault("Users", nil)
viper.SetDefault("TLS", nil)
viper.SetDefault("Realm", "dave")
viper.SetDefault("Log.Error", true)
viper.SetDefault("Log.Create", false)
viper.SetDefault("Log.Read", false)
viper.SetDefault("Log.Update", false)
viper.SetDefault("Log.Delete", false)
viper.SetDefault("Cors.Credentials", false)
}
// AuthenticationNeeded returns whether users are defined and authentication is required
func (cfg *Config) AuthenticationNeeded() bool {
return cfg.Users != nil && len(cfg.Users) != 0
}
func (cfg *Config) handleConfigUpdate(e fsnotify.Event) {
var err error
defer func() {
r := recover()
switch t := r.(type) {
case string:
log.WithError(errors.New(t)).Error("Error updating configuration. Please restart the server...")
case error:
log.WithError(t).Error("Error updating configuration. Please restart the server...")
}
}()
log.WithField("path", e.Name).Info("Config file changed")
file, err := os.Open(e.Name)
if err != nil {
log.WithField("path", e.Name).Warn("Error reloading config")
}
var updatedCfg = &Config{}
viper.ReadConfig(file)
viper.Unmarshal(&updatedCfg)
updateConfig(cfg, updatedCfg)
}
func updateConfig(cfg *Config, updatedCfg *Config) {
for username := range cfg.Users {
if updatedCfg.Users[username] == nil {
log.WithField("user", username).Info("Removed User from configuration")
delete(cfg.Users, username)
}
}
for username, v := range updatedCfg.Users {
if cfg.Users[username] == nil {
log.WithField("user", username).Info("Added User to configuration")
cfg.Users[username] = v
} else {
if cfg.Users[username].Password != v.Password {
log.WithField("user", username).Info("Updated password of user")
cfg.Users[username].Password = v.Password
}
if cfg.Users[username].Subdir != v.Subdir {
log.WithField("user", username).Info("Updated subdir of user")
cfg.Users[username].Subdir = v.Subdir
}
}
}
cfg.ensureUserDirs()
if cfg.Log.Create != updatedCfg.Log.Create {
cfg.Log.Create = updatedCfg.Log.Create
log.WithField("enabled", cfg.Log.Create).Info("Set logging for create operations")
}
if cfg.Log.Read != updatedCfg.Log.Read {
cfg.Log.Read = updatedCfg.Log.Read
log.WithField("enabled", cfg.Log.Read).Info("Set logging for read operations")
}
if cfg.Log.Update != updatedCfg.Log.Update {
cfg.Log.Update = updatedCfg.Log.Update
log.WithField("enabled", cfg.Log.Update).Info("Set logging for update operations")
}
if cfg.Log.Delete != updatedCfg.Log.Delete {
cfg.Log.Delete = updatedCfg.Log.Delete
log.WithField("enabled", cfg.Log.Delete).Info("Set logging for delete operations")
}
}
func (cfg *Config) ensureUserDirs() {
if _, err := os.Stat(cfg.Dir); os.IsNotExist(err) {
mkdirErr := os.Mkdir(cfg.Dir, os.ModePerm)
if mkdirErr != nil {
log.WithField("path", cfg.Dir).WithField("error", err).Warn("Can't create base dir")
return
}
log.WithField("path", cfg.Dir).Info("Created base dir")
}
for _, user := range cfg.Users {
if user.Subdir != nil {
path := filepath.Join(cfg.Dir, *user.Subdir)
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, os.ModePerm)
log.WithField("path", path).Info("Created user dir")
}
}
}
}