refactor rules -> routes, routes -> frontends

This commit is contained in:
emile 2015-09-15 16:09:21 +02:00
parent 74ae3eaa01
commit a4f542c02f
7 changed files with 62 additions and 62 deletions

View file

@ -34,17 +34,17 @@ type Server struct {
Weight int Weight int
} }
type Rule struct { type Route struct {
Category string Category string
Value string Value string
} }
type Route struct { type Frontend struct {
Backend string Backend string
Rules map[string]Rule Routes map[string]Route
} }
type Configuration struct { type Configuration struct {
Backends map[string]Backend Backends map[string]Backend
Routes map[string]Route Frontends map[string]Frontend
} }

View file

@ -4,11 +4,11 @@
weight = {{getWeight .}} weight = {{getWeight .}}
{{end}} {{end}}
[routes]{{range $host, $containers := .Hosts}} [frontends]{{range $host, $containers := .Hosts}}
[routes.route-{{$host}}] [frontends.frontend-{{$host}}]
{{$container := index $containers 0}} {{$container := index $containers 0}}
backend = "backend-{{getBackend $container}}" backend = "backend-{{getBackend $container}}"
[routes.route-{{$host}}.rules.rule-host-{{$host}}] [frontends.frontend-{{$host}}.routes.route-host-{{$host}}]
category = "Host" category = "Host"
value = "{{$host}}.{{$.Domain}}" value = "{{$host}}.{{$.Domain}}"
{{end}} {{end}}

View file

@ -10,10 +10,10 @@
{{end}} {{end}}
{{end}} {{end}}
[routes]{{range .Applications}} [frontends]{{range .Applications}}
[routes.route{{.ID | replace "/" "-"}}] [frontends.frontend{{.ID | replace "/" "-"}}]
backend = "backend{{.ID | replace "/" "-"}}" backend = "backend{{.ID | replace "/" "-"}}"
[routes.route-{{getHost . | replace "/" "-"}}.rules.rule-host-{{getHost . | replace "/" "-"}}] [frontends.frontend-{{getHost . | replace "/" "-"}}.routes.route-host-{{getHost . | replace "/" "-"}}]
category = "Host" category = "Host"
value = "{{getHost . | replace "/" "-"}}.{{$.Domain}}" value = "{{getHost . | replace "/" "-"}}.{{$.Domain}}"
{{end}} {{end}}

View file

@ -23,6 +23,37 @@
</div> </div>
<div class="row"> <div class="row">
<div class="col-md-6">
<!-- <div class="panel-heading">Frontends</div>
<div class="panel-body"> -->
{{range $keyFrontends, $valueFrontends := .Configuration.Frontends}}
<div class="panel panel-primary">
<div class="panel-heading">{{$keyFrontends}}</div>
<div class="panel-body">
<!--<button type="button" class="btn btn-info">{{$valueFrontends.Backend}}</button>-->
<a class="btn btn-info" role="button" data-toggle="collapse" href="#{{$valueFrontends.Backend}}" aria-expanded="false">
{{$valueFrontends.Backend}}
</a>
</div>
<table class="table table-striped table-hover">
<tr>
<td><em>Route</em></td>
<td><em>Category</em></td>
<td><em>Value</em></td>
</tr>
{{range $keyRoutes, $valueRoutes := $valueFrontends.Routes}}
<tr>
<td>{{$keyRoutes}}</td>
<td>{{$valueRoutes.Category}}</td>
<td>{{$valueRoutes.Value}}</td>
</tr>
{{end}}
</table>
</div>
{{end}}
<!-- </div> -->
</div>
<div class="col-md-6"> <div class="col-md-6">
<!-- <div class="panel-heading">Backends</div> <!-- <div class="panel-heading">Backends</div>
<div class="panel-body"> --> <div class="panel-body"> -->
@ -48,37 +79,6 @@
<!-- </div> --> <!-- </div> -->
</div> </div>
<div class="col-md-6">
<!-- <div class="panel-heading">Routes</div>
<div class="panel-body"> -->
{{range $keyRoutes, $valueRoutes := .Configuration.Routes}}
<div class="panel panel-primary">
<div class="panel-heading">{{$keyRoutes}}</div>
<div class="panel-body">
<!--<button type="button" class="btn btn-info">{{$valueRoutes.Backend}}</button>-->
<a class="btn btn-info" role="button" data-toggle="collapse" href="#{{$valueRoutes.Backend}}" aria-expanded="false">
{{$valueRoutes.Backend}}
</a>
</div>
<table class="table table-striped table-hover">
<tr>
<td><em>Rule</em></td>
<td><em>Category</em></td>
<td><em>Value</em></td>
</tr>
{{range $keyRules, $valueRules := $valueRoutes.Rules}}
<tr>
<td>{{$keyRules}}</td>
<td>{{$valueRules.Category}}</td>
<td>{{$valueRules.Value}}</td>
</tr>
{{end}}
</table>
</div>
{{end}}
<!-- </div> -->
</div>
</div> </div>
</div> </div>

View file

@ -182,29 +182,29 @@ func LoadConfig(configuration *Configuration, gloablConfiguration *GlobalConfigu
router := mux.NewRouter() router := mux.NewRouter()
router.NotFoundHandler = http.HandlerFunc(notFoundHandler) router.NotFoundHandler = http.HandlerFunc(notFoundHandler)
backends := map[string]http.Handler{} backends := map[string]http.Handler{}
for routeName, route := range configuration.Routes { for frontendName, frontend := range configuration.Frontends {
log.Debug("Creating route %s", routeName) log.Debug("Creating frontend %s", frontendName)
fwd, _ := forward.New() fwd, _ := forward.New()
newRoute := router.NewRoute() newRoute := router.NewRoute()
for ruleName, rule := range route.Rules { for routeName, route := range frontend.Routes {
log.Debug("Creating rule %s", ruleName) log.Debug("Creating route %s", routeName)
newRouteReflect := Invoke(newRoute, rule.Category, rule.Value) newRouteReflect := Invoke(newRoute, route.Category, route.Value)
newRoute = newRouteReflect[0].Interface().(*mux.Route) newRoute = newRouteReflect[0].Interface().(*mux.Route)
} }
if backends[route.Backend] == nil { if backends[frontend.Backend] == nil {
log.Debug("Creating backend %s", route.Backend) log.Debug("Creating backend %s", frontend.Backend)
lb, _ := roundrobin.New(fwd) lb, _ := roundrobin.New(fwd)
rb, _ := roundrobin.NewRebalancer(lb) rb, _ := roundrobin.NewRebalancer(lb)
for serverName, server := range configuration.Backends[route.Backend].Servers { for serverName, server := range configuration.Backends[frontend.Backend].Servers {
log.Debug("Creating server %s", serverName) log.Debug("Creating server %s", serverName)
url, _ := url.Parse(server.Url) url, _ := url.Parse(server.Url)
rb.UpsertServer(url, roundrobin.Weight(server.Weight)) rb.UpsertServer(url, roundrobin.Weight(server.Weight))
} }
backends[route.Backend] = lb backends[frontend.Backend] = lb
} else { } else {
log.Debug("Reusing backend %s", route.Backend) log.Debug("Reusing backend %s", frontend.Backend)
} }
newRoute.Handler(backends[route.Backend]) newRoute.Handler(backends[frontend.Backend])
err := newRoute.GetError() err := newRoute.GetError()
if err != nil { if err != nil {
log.Error("Error building route ", err) log.Error("Error building route ", err)

View file

@ -192,14 +192,14 @@
# url = "http://172.17.0.5:80" # url = "http://172.17.0.5:80"
# weight = 2 # weight = 2
# #
# [routes] # [frontends]
# [routes.route1] # [frontends.frontend1]
# backend = "backend2" # backend = "backend2"
# [routes.route1.rules.test_1] # [frontends.frontend1.routes.test_1]
# category = "Host" # category = "Host"
# value = "test.localhost" # value = "test.localhost"
# [routes.route2] # [frontends.frontend2]
# backend = "backend1" # backend = "backend1"
# [routes.route2.rules.test_2] # [frontends.frontend2.routes.test_2]
# category = "Path" # category = "Path"
# value = "/test" # value = "/test"

View file

@ -192,14 +192,14 @@ domain = "docker.localhost"
# url = "http://172.17.0.5:80" # url = "http://172.17.0.5:80"
# weight = 2 # weight = 2
# #
# [routes] # [frontends]
# [routes.route1] # [frontends.frontend1]
# backend = "backend2" # backend = "backend2"
# [routes.route1.rules.test_1] # [frontends.frontend1.routes.test_1]
# category = "Host" # category = "Host"
# value = "test.localhost" # value = "test.localhost"
# [routes.route2] # [frontends.frontend2]
# backend = "backend1" # backend = "backend1"
# [routes.route2.rules.test_2] # [frontends.frontend2.routes.test_2]
# category = "Path" # category = "Path"
# value = "/test" # value = "/test"