Global configration toml

This commit is contained in:
emile 2015-09-07 17:39:22 +02:00
commit 0a09a78cac
5 changed files with 75 additions and 38 deletions

View file

@ -8,12 +8,12 @@ import(
) )
type DockerProvider struct { type DockerProvider struct {
Endpoint string
dockerClient *docker.Client dockerClient *docker.Client
} }
func (provider *DockerProvider) Provide(serviceChan chan<- *Service){ func (provider *DockerProvider) Provide(serviceChan chan<- *Service){
endpoint := "unix:///var/run/docker.sock" provider.dockerClient, _ = docker.NewClient(provider.Endpoint)
provider.dockerClient, _ = docker.NewClient(endpoint)
dockerEvents := make(chan *docker.APIEvents) dockerEvents := make(chan *docker.APIEvents)
provider.dockerClient.AddEventListener(dockerEvents) provider.dockerClient.AddEventListener(dockerEvents)
go func() { go func() {

32
file.go
View file

@ -4,23 +4,29 @@ import (
"log" "log"
"gopkg.in/fsnotify.v1" "gopkg.in/fsnotify.v1"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"os"
"path/filepath"
"strings"
) )
type FileProvider struct { type FileProvider struct {
Filename string
} }
func (provider *FileProvider) Provide(serviceChan chan<- *Service){ func (provider *FileProvider) Provide(serviceChan chan<- *Service){
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
if err != nil { if err != nil {
log.Fatal(err) log.Println(err)
return
} }
defer watcher.Close() defer watcher.Close()
file, err := os.Open(provider.Filename)
err = watcher.Add(".")
if err != nil { if err != nil {
log.Fatal(err) log.Println(err)
return
} }
defer file.Close()
done := make(chan bool) done := make(chan bool)
// Process events // Process events
@ -28,9 +34,9 @@ func (provider *FileProvider) Provide(serviceChan chan<- *Service){
for { for {
select { select {
case event := <-watcher.Events: case event := <-watcher.Events:
if(event.Name == "./tortuous.toml"){ if(strings.Contains(event.Name,file.Name())){
log.Println("event:", event) log.Println("event:", event)
service := provider.LoadFileConfig() service := provider.LoadFileConfig(file.Name())
serviceChan <- service serviceChan <- service
} }
case error := <-watcher.Errors: 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() if err != nil {
log.Println(err)
return
}
service:= provider.LoadFileConfig(file.Name())
serviceChan <- service serviceChan <- service
<-done <-done
log.Println("DONE")
} }
func (provider *FileProvider) LoadFileConfig() *Service { func (provider *FileProvider) LoadFileConfig(filename string) *Service {
service := new(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) log.Println("Error reading file:", err)
return nil return nil
} }

18
file.toml Normal file
View 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"

View file

@ -15,18 +15,30 @@ import (
"syscall" "syscall"
"time" "time"
"log" "log"
"github.com/BurntSushi/toml"
) )
type Configuration struct {
Docker *DockerProvider
File *FileProvider
}
var srv *graceful.Server var srv *graceful.Server
var systemRouter *mux.Router var serviceRouter *mux.Router
var renderer = render.New() var renderer = render.New()
var currentService = new(Service) var currentService = new(Service)
var serviceChan = make(chan *Service) var serviceChan = make(chan *Service)
var providers = []Provider{} var providers = []Provider{}
func main() { func main() {
//providers = append(providers, new(DockerProvider)) configuration := LoadFileConfig()
providers = append(providers, new(FileProvider)) if(configuration.Docker != nil){
providers = append(providers, configuration.Docker)
}
if(configuration.File != nil){
providers = append(providers, configuration.File)
}
sigs := make(chan os.Signal, 1) sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
@ -46,7 +58,7 @@ func main() {
log.Println("Skipping same service") log.Println("Skipping same service")
} else{ } else{
currentService = service currentService = service
systemRouter = LoadConfig(service) serviceRouter = LoadConfig(service)
srv.Stop(10 * time.Second) srv.Stop(10 * time.Second)
time.Sleep(3 * time.Second) time.Sleep(3 * time.Second)
} }
@ -81,7 +93,7 @@ func main() {
Server: &http.Server{ Server: &http.Server{
Addr: ":8001", Addr: ":8001",
Handler: systemRouter, Handler: serviceRouter,
}, },
} }
@ -122,7 +134,7 @@ func LoadConfig(service *Service) *mux.Router {
} }
func DeployService() { func DeployService() {
systemRouter = LoadConfig(currentService) serviceRouter = LoadConfig(currentService)
} }
func ReloadConfigHandler(rw http.ResponseWriter, r *http.Request) { 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) 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
}

View file

@ -1,18 +1,5 @@
[backends] #[docker]
[backends.backend1] #endpoint = "unix:///var/run/docker.sock"
[backends.backend1.servers.server1]
url = "http://172.17.0.2:80"
[backends.backend1.servers.server2]
url = "http://172.17.0.3:80"
[routes] [file]
[routes.route1] filename = "file.toml"
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"