Global configration toml
This commit is contained in:
commit
0a09a78cac
5 changed files with 75 additions and 38 deletions
|
@ -8,12 +8,12 @@ import(
|
|||
)
|
||||
|
||||
type DockerProvider struct {
|
||||
Endpoint string
|
||||
dockerClient *docker.Client
|
||||
}
|
||||
|
||||
func (provider *DockerProvider) Provide(serviceChan chan<- *Service){
|
||||
endpoint := "unix:///var/run/docker.sock"
|
||||
provider.dockerClient, _ = docker.NewClient(endpoint)
|
||||
provider.dockerClient, _ = docker.NewClient(provider.Endpoint)
|
||||
dockerEvents := make(chan *docker.APIEvents)
|
||||
provider.dockerClient.AddEventListener(dockerEvents)
|
||||
go func() {
|
||||
|
|
36
file.go
36
file.go
|
@ -4,23 +4,29 @@ import (
|
|||
"log"
|
||||
"gopkg.in/fsnotify.v1"
|
||||
"github.com/BurntSushi/toml"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type FileProvider struct {
|
||||
Filename string
|
||||
}
|
||||
|
||||
func (provider *FileProvider) Provide(serviceChan chan<- *Service){
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
defer watcher.Close()
|
||||
|
||||
|
||||
err = watcher.Add(".")
|
||||
file, err := os.Open(provider.Filename)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
done := make(chan bool)
|
||||
// Process events
|
||||
|
@ -28,9 +34,9 @@ func (provider *FileProvider) Provide(serviceChan chan<- *Service){
|
|||
for {
|
||||
select {
|
||||
case event := <-watcher.Events:
|
||||
if(event.Name == "./tortuous.toml"){
|
||||
if(strings.Contains(event.Name,file.Name())){
|
||||
log.Println("event:", event)
|
||||
service := provider.LoadFileConfig()
|
||||
service := provider.LoadFileConfig(file.Name())
|
||||
serviceChan <- service
|
||||
}
|
||||
case error := <-watcher.Errors:
|
||||
|
@ -39,17 +45,23 @@ func (provider *FileProvider) Provide(serviceChan chan<- *Service){
|
|||
}
|
||||
}()
|
||||
|
||||
err = watcher.Add(filepath.Dir(file.Name()))
|
||||
|
||||
service:= provider.LoadFileConfig()
|
||||
serviceChan <- service
|
||||
<-done
|
||||
log.Println("DONE")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
func (provider *FileProvider) LoadFileConfig() *Service {
|
||||
service:= provider.LoadFileConfig(file.Name())
|
||||
serviceChan <- service
|
||||
<-done
|
||||
}
|
||||
|
||||
|
||||
func (provider *FileProvider) LoadFileConfig(filename string) *Service {
|
||||
service := new(Service)
|
||||
if _, err := toml.DecodeFile("tortuous.toml", service); err != nil {
|
||||
if _, err := toml.DecodeFile(filename, service); err != nil {
|
||||
log.Println("Error reading file:", err)
|
||||
return nil
|
||||
}
|
||||
|
|
18
file.toml
Normal file
18
file.toml
Normal file
|
@ -0,0 +1,18 @@
|
|||
[backends]
|
||||
[backends.backend1]
|
||||
[backends.backend1.servers.server1]
|
||||
url = "http://172.17.0.2:80"
|
||||
[backends.backend1.servers.server2]
|
||||
url = "http://172.17.0.3:80"
|
||||
|
||||
[routes]
|
||||
[routes.route1]
|
||||
backends = ["backend1"]
|
||||
[routes.route1.rules.test_zenika_1]
|
||||
category = "Host"
|
||||
value = "test.zenika.fr"
|
||||
[routes.route2]
|
||||
backends = ["backend1"]
|
||||
[routes.route2.rules.test_zenika_2]
|
||||
category = "Path"
|
||||
value = "/test"
|
32
tortuous.go
32
tortuous.go
|
@ -15,18 +15,30 @@ import (
|
|||
"syscall"
|
||||
"time"
|
||||
"log"
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type Configuration struct {
|
||||
Docker *DockerProvider
|
||||
File *FileProvider
|
||||
}
|
||||
|
||||
var srv *graceful.Server
|
||||
var systemRouter *mux.Router
|
||||
var serviceRouter *mux.Router
|
||||
var renderer = render.New()
|
||||
var currentService = new(Service)
|
||||
var serviceChan = make(chan *Service)
|
||||
var providers = []Provider{}
|
||||
|
||||
func main() {
|
||||
//providers = append(providers, new(DockerProvider))
|
||||
providers = append(providers, new(FileProvider))
|
||||
configuration := LoadFileConfig()
|
||||
if(configuration.Docker != nil){
|
||||
providers = append(providers, configuration.Docker)
|
||||
}
|
||||
|
||||
if(configuration.File != nil){
|
||||
providers = append(providers, configuration.File)
|
||||
}
|
||||
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
@ -46,7 +58,7 @@ func main() {
|
|||
log.Println("Skipping same service")
|
||||
} else{
|
||||
currentService = service
|
||||
systemRouter = LoadConfig(service)
|
||||
serviceRouter = LoadConfig(service)
|
||||
srv.Stop(10 * time.Second)
|
||||
time.Sleep(3 * time.Second)
|
||||
}
|
||||
|
@ -81,7 +93,7 @@ func main() {
|
|||
|
||||
Server: &http.Server{
|
||||
Addr: ":8001",
|
||||
Handler: systemRouter,
|
||||
Handler: serviceRouter,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -122,7 +134,7 @@ func LoadConfig(service *Service) *mux.Router {
|
|||
}
|
||||
|
||||
func DeployService() {
|
||||
systemRouter = LoadConfig(currentService)
|
||||
serviceRouter = LoadConfig(currentService)
|
||||
}
|
||||
|
||||
func ReloadConfigHandler(rw http.ResponseWriter, r *http.Request) {
|
||||
|
@ -146,3 +158,11 @@ func Invoke(any interface{}, name string, args ...interface{}) []reflect.Value {
|
|||
}
|
||||
return reflect.ValueOf(any).MethodByName(name).Call(inputs)
|
||||
}
|
||||
|
||||
func LoadFileConfig() *Configuration {
|
||||
configuration := new(Configuration)
|
||||
if _, err := toml.DecodeFile("tortuous.toml", configuration); err != nil {
|
||||
log.Fatal("Error reading file:", err)
|
||||
}
|
||||
return configuration
|
||||
}
|
|
@ -1,18 +1,5 @@
|
|||
[backends]
|
||||
[backends.backend1]
|
||||
[backends.backend1.servers.server1]
|
||||
url = "http://172.17.0.2:80"
|
||||
[backends.backend1.servers.server2]
|
||||
url = "http://172.17.0.3:80"
|
||||
#[docker]
|
||||
#endpoint = "unix:///var/run/docker.sock"
|
||||
|
||||
[routes]
|
||||
[routes.route1]
|
||||
backends = ["backend1"]
|
||||
[routes.route1.rules.test_zenika_1]
|
||||
category = "Host"
|
||||
value = "test.zenika.fr"
|
||||
[routes.route2]
|
||||
backends = ["backend1"]
|
||||
[routes.route2.rules.test_zenika_2]
|
||||
category = "Path"
|
||||
value = "/test"
|
||||
[file]
|
||||
filename = "file.toml"
|
Loading…
Reference in a new issue