refactoring unique backend by route

This commit is contained in:
emile 2015-09-09 17:41:33 +02:00
parent c5a91e7d22
commit 6f8f3e449d
6 changed files with 30 additions and 46 deletions

View file

@ -15,7 +15,7 @@ type Rule struct {
} }
type Route struct { type Route struct {
Backends []string Backend string
Rules map[string]Rule Rules map[string]Rule
} }

View file

@ -5,7 +5,8 @@
[routes]{{range $host, $containers := .Hosts}} [routes]{{range $host, $containers := .Hosts}}
[routes.route-{{$host}}] [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}}] [routes.route-{{$host}}.rules.rule-host-{{$host}}]
category = "Host" category = "Host"
value = "{{$host}}.{{$.Domain}}" value = "{{$host}}.{{$.Domain}}"

View file

@ -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"

View file

@ -60,12 +60,10 @@
<div class="panel panel-primary"> <div class="panel panel-primary">
<div class="panel-heading">{{$keyRoutes}}</div> <div class="panel-heading">{{$keyRoutes}}</div>
<div class="panel-body"> <div class="panel-body">
{{range $backend := $valueRoutes.Backends}} <!--<button type="button" class="btn btn-info">{{$valueRoutes.Backend}}</button>-->
<!--<button type="button" class="btn btn-info">{{$backend}}</button>--> <a class="btn btn-info" role="button" data-toggle="collapse" href="#{{$valueRoutes.Backend}}" aria-expanded="false">
<a class="btn btn-info" role="button" data-toggle="collapse" href="#{{$backend}}" aria-expanded="false"> {{$valueRoutes.Backend}}
{{$backend}}
</a> </a>
{{end}}
</div> </div>
<table class="table table-striped table-hover"> <table class="table table-striped table-hover">
<tr> <tr>

View file

@ -33,6 +33,7 @@ var providers = []Provider{}
func main() { func main() {
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)
globalConfigFile := "træfik.toml"
go func() { go func() {
for { for {
@ -51,12 +52,16 @@ func main() {
} }
}() }()
configuration := LoadFileConfig() configuration := LoadFileConfig(globalConfigFile)
if (configuration.Docker != nil) { if (configuration.Docker != nil) {
providers = append(providers, configuration.Docker) providers = append(providers, configuration.Docker)
} }
if (configuration.File != nil) { 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) providers = append(providers, configuration.File)
} }
@ -118,23 +123,21 @@ func LoadConfig(configuration *Configuration) *mux.Router {
newRoute := newRouteReflect[0].Interface().(*mux.Route) newRoute := newRouteReflect[0].Interface().(*mux.Route)
newRoutes = append(newRoutes, newRoute) newRoutes = append(newRoutes, newRoute)
} }
for _, backendName := range route.Backends { if (backends[route.Backend] ==nil) {
if (backends[backendName] ==nil) { log.Println("Creating backend", route.Backend)
log.Println("Creating backend", backendName)
lb, _ := roundrobin.New(fwd) lb, _ := roundrobin.New(fwd)
rb, _ := roundrobin.NewRebalancer(lb) rb, _ := roundrobin.NewRebalancer(lb)
for serverName, server := range configuration.Backends[backendName].Servers { for serverName, server := range configuration.Backends[route.Backend].Servers {
log.Println("Creating server", serverName) log.Println("Creating server", serverName)
url, _ := url.Parse(server.Url) url, _ := url.Parse(server.Url)
rb.UpsertServer(url) rb.UpsertServer(url)
} }
backends[backendName]=lb backends[route.Backend]=lb
}else { }else {
log.Println("Reusing backend", backendName) log.Println("Reusing backend", route.Backend)
}
for _, route := range newRoutes {
route.Handler(handlers.CombinedLoggingHandler(os.Stdout, backends[backendName]))
} }
for _, muxRoute := range newRoutes {
muxRoute.Handler(handlers.CombinedLoggingHandler(os.Stdout, backends[route.Backend]))
} }
} }
return router return router
@ -148,9 +151,9 @@ 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() *FileConfiguration { func LoadFileConfig(file string) *FileConfiguration {
configuration := new(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) log.Fatal("Error reading file:", err)
} }
return configuration return configuration

View file

@ -22,12 +22,12 @@ address = ":8010"
[routes] [routes]
[routes.route1] [routes.route1]
backends = ["backend1", "backend2"] backend = "backend2"
[routes.route1.rules.test_zenika_1] [routes.route1.rules.test_zenika_1]
category = "Host" category = "Host"
value = "test.zenika.fr" value = "test.zenika.fr"
[routes.route2] [routes.route2]
backends = ["backend1"] backend = "backend1"
[routes.route2.rules.test_zenika_2] [routes.route2.rules.test_zenika_2]
category = "Path" category = "Path"
value = "/test" value = "/test"