From 6f8f3e449d811e609af2bddf0e9d39db4ddedd44 Mon Sep 17 00:00:00 2001 From: emile Date: Wed, 9 Sep 2015 17:41:33 +0200 Subject: [PATCH] refactoring unique backend by route --- configuration.go | 2 +- docker.tmpl | 3 ++- file.toml | 18 ---------------- templates/configuration.tmpl | 8 +++---- træfik.go | 41 +++++++++++++++++++----------------- træfik.toml | 4 ++-- 6 files changed, 30 insertions(+), 46 deletions(-) delete mode 100644 file.toml diff --git a/configuration.go b/configuration.go index 33e0c18a2..7b4e3ea8a 100644 --- a/configuration.go +++ b/configuration.go @@ -15,7 +15,7 @@ type Rule struct { } type Route struct { - Backends []string + Backend string Rules map[string]Rule } diff --git a/docker.tmpl b/docker.tmpl index 106b9edd7..dcbee33e1 100644 --- a/docker.tmpl +++ b/docker.tmpl @@ -5,7 +5,8 @@ [routes]{{range $host, $containers := .Hosts}} [routes.route-{{$host}}] - backends = [{{range $container := $containers}}"backend-{{getBackend $container}}",{{end}}] + {{$container := index $containers 0}} + backend = "backend-{{getBackend $container}}" [routes.route-{{$host}}.rules.rule-host-{{$host}}] category = "Host" value = "{{$host}}.{{$.Domain}}" diff --git a/file.toml b/file.toml deleted file mode 100644 index 2804cf7bb..000000000 --- a/file.toml +++ /dev/null @@ -1,18 +0,0 @@ -[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" diff --git a/templates/configuration.tmpl b/templates/configuration.tmpl index 9bd54cd53..d66a4cb4a 100644 --- a/templates/configuration.tmpl +++ b/templates/configuration.tmpl @@ -60,12 +60,10 @@
{{$keyRoutes}}
- {{range $backend := $valueRoutes.Backends}} - - - {{end}}
diff --git a/træfik.go b/træfik.go index 28f2688c4..d5ed8b991 100644 --- a/træfik.go +++ b/træfik.go @@ -33,6 +33,7 @@ var providers = []Provider{} func main() { sigs := make(chan os.Signal, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + globalConfigFile := "træfik.toml" go func() { for { @@ -51,12 +52,16 @@ func main() { } }() - configuration := LoadFileConfig() + configuration := LoadFileConfig(globalConfigFile) if (configuration.Docker != nil) { providers = append(providers, configuration.Docker) } if (configuration.File != nil) { + if (len(configuration.File.Filename) == 0) { + // no filename, setting to global config file + configuration.File.Filename = globalConfigFile + } providers = append(providers, configuration.File) } @@ -118,23 +123,21 @@ func LoadConfig(configuration *Configuration) *mux.Router { newRoute := newRouteReflect[0].Interface().(*mux.Route) newRoutes = append(newRoutes, newRoute) } - for _, backendName := range route.Backends { - if (backends[backendName] ==nil) { - log.Println("Creating backend", backendName) - lb, _ := roundrobin.New(fwd) - rb, _ := roundrobin.NewRebalancer(lb) - for serverName, server := range configuration.Backends[backendName].Servers { - log.Println("Creating server", serverName) - url, _ := url.Parse(server.Url) - rb.UpsertServer(url) - } - backends[backendName]=lb - }else { - log.Println("Reusing backend", backendName) - } - for _, route := range newRoutes { - route.Handler(handlers.CombinedLoggingHandler(os.Stdout, backends[backendName])) + if (backends[route.Backend] ==nil) { + log.Println("Creating backend", route.Backend) + lb, _ := roundrobin.New(fwd) + rb, _ := roundrobin.NewRebalancer(lb) + for serverName, server := range configuration.Backends[route.Backend].Servers { + log.Println("Creating server", serverName) + url, _ := url.Parse(server.Url) + rb.UpsertServer(url) } + backends[route.Backend]=lb + }else { + log.Println("Reusing backend", route.Backend) + } + for _, muxRoute := range newRoutes { + muxRoute.Handler(handlers.CombinedLoggingHandler(os.Stdout, backends[route.Backend])) } } return router @@ -148,9 +151,9 @@ func Invoke(any interface{}, name string, args ...interface{}) []reflect.Value { return reflect.ValueOf(any).MethodByName(name).Call(inputs) } -func LoadFileConfig() *FileConfiguration { +func LoadFileConfig(file string) *FileConfiguration { configuration := new(FileConfiguration) - if _, err := toml.DecodeFile("træfik.toml", configuration); err != nil { + if _, err := toml.DecodeFile(file, configuration); err != nil { log.Fatal("Error reading file:", err) } return configuration diff --git a/træfik.toml b/træfik.toml index 9a10264ba..ec0d004dc 100644 --- a/træfik.toml +++ b/træfik.toml @@ -22,12 +22,12 @@ address = ":8010" [routes] [routes.route1] - backends = ["backend1", "backend2"] + backend = "backend2" [routes.route1.rules.test_zenika_1] category = "Host" value = "test.zenika.fr" [routes.route2] - backends = ["backend1"] + backend = "backend1" [routes.route2.rules.test_zenika_2] category = "Path" value = "/test"