Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 03:00 UTC
feature: watch include directory (#5521)
Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
Yehonatan Ezron committed
May 9, 2023 at 01:49 UTC
571fc034d3838d8c2a33ab8424d44eb5b30f4ecd
1 file changed
+14
-39
cmd/main.go
+14
-39
@@ -174,6 +174,8 @@ func LoadConfig(configFile, adapterName string) ([]byte, string, error) {
174
// blocks indefinitely; it only quits if the poller has errors for
175
// long enough time. The filename passed in must be the actual
176
// config file used, not one to be discovered.
177
+// Each second the config files is loaded and parsed into an object
178
+// and is compared to the last config object that was loaded
179
func watchConfigFile(filename, adapterName string) {
180
defer func() {
181
if err := recover(); err != nil {
@@ -189,64 +191,37 @@ func watchConfigFile(filename, adapterName string) {
191
With(zap.String("config_file", filename))
192
}
193
192
- // get the initial timestamp on the config file
193
- info, err := os.Stat(filename)
194
+ // get current config
195
+ lastCfg, _, err := LoadConfig(filename, adapterName)
196
if err != nil {
195
- logger().Error("cannot watch config file", zap.Error(err))
197
+ logger().Error("unable to load latest config", zap.Error(err))
198
return
199
}
198
- lastModified := info.ModTime()
200
201
logger().Info("watching config file for changes")
202
202
- // if the file disappears or something, we can
203
- // stop polling if the error lasts long enough
204
- var lastErr time.Time
205
- finalError := func(err error) bool {
206
- if lastErr.IsZero() {
207
- lastErr = time.Now()
208
- return false
209
- }
210
- if time.Since(lastErr) > 30*time.Second {
211
- logger().Error("giving up watching config file; too many errors",
212
- zap.Error(err))
213
- return true
214
- }
215
- return false
216
- }
217
-
203
// begin poller
204
//nolint:staticcheck
205
for range time.Tick(1 * time.Second) {
221
- // get the file info
222
- info, err := os.Stat(filename)
206
+
207
+ // get current config
208
+ newCfg, _, err := LoadConfig(filename, adapterName)
209
if err != nil {
224
- if finalError(err) {
225
- return
226
- }
227
- continue
210
+ logger().Error("unable to load latest config", zap.Error(err))
211
+ return
212
}
229
- lastErr = time.Time{} // no error, so clear any memory of one
213
214
// if it hasn't changed, nothing to do
232
- if !info.ModTime().After(lastModified) {
215
+ if bytes.Equal(lastCfg, newCfg) {
216
continue
217
}
235
-
218
logger().Info("config file changed; reloading")
219
238
- // remember this timestamp
239
- lastModified = info.ModTime()
240
-
241
- // load the contents of the file
242
- config, _, err := LoadConfig(filename, adapterName)
243
- if err != nil {
244
- logger().Error("unable to load latest config", zap.Error(err))
245
- continue
246
- }
220
+ // remember the current config
221
+ lastCfg = newCfg
222
223
// apply the updated config
249
- err = caddy.Load(config, false)
224
+ err = caddy.Load(lastCfg, false)
225
if err != nil {
226
logger().Error("applying latest config", zap.Error(err))
227
continue