mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-17 22:19:42 +02:00
allow multiple docker providers, added file provider support
This commit is contained in:
74
src/go-proxy/config.go
Normal file
74
src/go-proxy/config.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/golang/glog"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Providers map[string]*Provider `yaml:",flow"`
|
||||
}
|
||||
|
||||
var config *Config
|
||||
|
||||
func ReadConfig() (*Config, error) {
|
||||
config := Config{}
|
||||
data, err := os.ReadFile(configPath)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to read config file: %v", err)
|
||||
}
|
||||
|
||||
err = yaml.Unmarshal(data, &config)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to parse config file: %v", err)
|
||||
}
|
||||
|
||||
for name, p := range config.Providers {
|
||||
p.name = name
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
func ListenConfigChanges() {
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
glog.Errorf("[Config] unable to create file watcher: %v", err)
|
||||
}
|
||||
defer watcher.Close()
|
||||
|
||||
if err = watcher.Add(configPath); err != nil {
|
||||
glog.Errorf("[Config] unable to watch file: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case event, ok := <-watcher.Events:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
switch {
|
||||
case event.Has(fsnotify.Write):
|
||||
glog.Infof("[Config] file change detected")
|
||||
for _, p := range config.Providers {
|
||||
p.StopAllRoutes()
|
||||
}
|
||||
config, err = ReadConfig()
|
||||
if err != nil {
|
||||
glog.Fatalf("[Config] unable to read config: %v", err)
|
||||
}
|
||||
case event.Has(fsnotify.Remove), event.Has(fsnotify.Rename):
|
||||
glog.Fatalf("[Config] file renamed / deleted")
|
||||
}
|
||||
case err := <-watcher.Errors:
|
||||
glog.Errorf("[Config] File watcher error: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user