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
}
type Rule struct {
type Route struct {
Category string
Value string
}
type Route struct {
type Frontend struct {
Backend string
Rules map[string]Rule
Routes map[string]Route
}
type Configuration struct {
Backends map[string]Backend
Routes map[string]Route
Frontends map[string]Frontend
}

View file

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

View file

@ -10,10 +10,10 @@
{{end}}
{{end}}
[routes]{{range .Applications}}
[routes.route{{.ID | replace "/" "-"}}]
[frontends]{{range .Applications}}
[frontends.frontend{{.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"
value = "{{getHost . | replace "/" "-"}}.{{$.Domain}}"
{{end}}

View file

@ -23,6 +23,37 @@
</div>
<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="panel-heading">Backends</div>
<div class="panel-body"> -->
@ -48,37 +79,6 @@
<!-- </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>

View file

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

View file

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

View file

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