From 208998972ac6fa710c48cc399bf9abfd6d9d4008 Mon Sep 17 00:00:00 2001 From: Emile Vauge Date: Fri, 3 Jun 2016 17:58:33 +0200 Subject: [PATCH] Add routes priorities Signed-off-by: Emile Vauge --- glide.lock | 16 ++++++++-------- glide.yaml | 2 +- middlewares/handlerSwitcher.go | 2 +- middlewares/routes.go | 2 +- rules.go | 2 +- server.go | 10 +++++++++- traefik.go | 2 +- types/types.go | 1 + web.go | 2 +- 9 files changed, 24 insertions(+), 15 deletions(-) diff --git a/glide.lock b/glide.lock index 05374f993..8c5a483df 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: dc59755b72e71945a21135c5a37e4a5c11ae511ac7404d1440166ea0aed736c4 -updated: 2016-06-02T15:11:52.77657652+02:00 +hash: 5a6dbc30a69abd002736bd5113e0f783c448faee20a0791c724ec2c3c1cfb8bb +updated: 2016-06-03T18:11:43.839017153+02:00 imports: - name: github.com/boltdb/bolt version: dfb21201d9270c1082d5fb0f07f500311ff72f18 @@ -16,9 +16,11 @@ imports: - name: github.com/codegangsta/cli version: bf4a526f48af7badd25d2cb02d587e1b01be3b50 - name: github.com/codegangsta/negroni - version: fb7b7c045dfb05dc81a5c3688c568550b5bd6e36 + version: feacfc52d357c844f524c794947493483ed881b3 - name: github.com/containous/flaeg version: b98687da5c323650f4513fda6b6203fcbdec9313 +- name: github.com/containous/mux + version: a819b77bba13f0c0cbe36e437bc2e948411b3996 - name: github.com/containous/oxy version: 183212964e13e7b8afe01a08b193d04300554a68 subpackages: @@ -43,7 +45,7 @@ imports: subpackages: - spew - name: github.com/docker/distribution - version: bb330cd684eb4afab9cc4f2453d7c8918099d7ee + version: feddf6cd4e439577ab270d8e3ba63a5d7c5c0d55 subpackages: - reference - digest @@ -100,10 +102,8 @@ imports: - query - name: github.com/gorilla/context version: aed02d124ae4a0e94fea4541c8effd05bf0c8296 -- name: github.com/gorilla/mux - version: bd09be08ed4377796d312df0a45314e11b8f5dc1 - name: github.com/hashicorp/consul - version: ebf7ea1d759184c02a5bb5263a7c52d29838ffc3 + version: 802b29ab948dedb7f7b1b903f535bdf250388c50 subpackages: - api - name: github.com/hashicorp/go-cleanhttp @@ -136,7 +136,7 @@ imports: - name: github.com/ogier/pflag version: 45c278ab3607870051a2ea9040bb85fcb8557481 - name: github.com/opencontainers/runc - version: 6c485e6902bb9dd77b8234042b8f00e20ef87a18 + version: 3211c9f721237f55a16da9c111e3d7e8777e53b5 subpackages: - libcontainer/user - name: github.com/parnurzeal/gorequest diff --git a/glide.yaml b/glide.yaml index 9a1e4385c..7668dac10 100644 --- a/glide.yaml +++ b/glide.yaml @@ -40,7 +40,7 @@ import: - package: github.com/elazarl/go-bindata-assetfs - package: github.com/gambol99/go-marathon version: ade11d1dc2884ee1f387078fc28509559b6235d1 -- package: github.com/gorilla/mux +- package: github.com/containous/mux - package: github.com/hashicorp/consul subpackages: - api diff --git a/middlewares/handlerSwitcher.go b/middlewares/handlerSwitcher.go index 81dfacd58..b9a0903e2 100644 --- a/middlewares/handlerSwitcher.go +++ b/middlewares/handlerSwitcher.go @@ -1,8 +1,8 @@ package middlewares import ( + "github.com/containous/mux" "github.com/containous/traefik/safe" - "github.com/gorilla/mux" "net/http" ) diff --git a/middlewares/routes.go b/middlewares/routes.go index 991d3085d..4f017fce0 100644 --- a/middlewares/routes.go +++ b/middlewares/routes.go @@ -5,7 +5,7 @@ import ( "log" "net/http" - "github.com/gorilla/mux" + "github.com/containous/mux" ) // Routes holds the gorilla mux routes (for the API & co). diff --git a/rules.go b/rules.go index 94f0b17f4..b14243b26 100644 --- a/rules.go +++ b/rules.go @@ -2,7 +2,7 @@ package main import ( "errors" - "github.com/gorilla/mux" + "github.com/containous/mux" "net" "net/http" "reflect" diff --git a/server.go b/server.go index 14694efbe..ecac8c4db 100644 --- a/server.go +++ b/server.go @@ -20,6 +20,7 @@ import ( log "github.com/Sirupsen/logrus" "github.com/codegangsta/negroni" + "github.com/containous/mux" "github.com/containous/oxy/cbreaker" "github.com/containous/oxy/connlimit" "github.com/containous/oxy/forward" @@ -30,7 +31,6 @@ import ( "github.com/containous/traefik/provider" "github.com/containous/traefik/safe" "github.com/containous/traefik/types" - "github.com/gorilla/mux" "github.com/mailgun/manners" "github.com/streamrail/concurrent-map" ) @@ -501,6 +501,9 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo } else { log.Debugf("Reusing backend %s", frontend.Backend) } + if frontend.Priority > 0 { + newServerRoute.route.Priority(frontend.Priority) + } server.wireFrontendBackend(newServerRoute, backends[frontend.Backend]) } err := newServerRoute.route.GetError() @@ -511,6 +514,10 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo } } middlewares.SetBackend2FrontendMap(&backend2FrontendMap) + //sort routes + for _, serverEntryPoint := range serverEntryPoints { + serverEntryPoint.httpRouter.GetHandler().SortRoutes() + } return serverEntryPoints, nil } @@ -576,6 +583,7 @@ func getRoute(serverRoute *serverRoute, route *types.Route) error { if err != nil { return err } + newRoute.Priority(serverRoute.route.GetPriority() + len(route.Rule)) serverRoute.route = newRoute return nil } diff --git a/traefik.go b/traefik.go index a8be2edd1..f870cffd4 100644 --- a/traefik.go +++ b/traefik.go @@ -103,7 +103,7 @@ Complete documentation is available at https://traefik.io`, s.AddSource(toml) s.AddSource(f) if _, err := s.LoadConfig(); err != nil { - fmtlog.Println(err) + fmtlog.Println(fmt.Errorf("Error reading TOML config file %s : %s", toml.ConfigFileUsed(), err)) } traefikConfiguration.ConfigFile = toml.ConfigFileUsed() diff --git a/types/types.go b/types/types.go index dcac6bd46..7d521ed23 100644 --- a/types/types.go +++ b/types/types.go @@ -52,6 +52,7 @@ type Frontend struct { Backend string `json:"backend,omitempty"` Routes map[string]Route `json:"routes,omitempty"` PassHostHeader bool `json:"passHostHeader,omitempty"` + Priority int `json:"priority,omitempty"` } // LoadBalancerMethod holds the method of load balancing to use. diff --git a/web.go b/web.go index 690c2ecfc..048ffa121 100644 --- a/web.go +++ b/web.go @@ -9,11 +9,11 @@ import ( "runtime" log "github.com/Sirupsen/logrus" + "github.com/containous/mux" "github.com/containous/traefik/autogen" "github.com/containous/traefik/safe" "github.com/containous/traefik/types" "github.com/elazarl/go-bindata-assetfs" - "github.com/gorilla/mux" "github.com/thoas/stats" "github.com/unrolled/render" )