Fix redirect empty backend

Issue-#679
This commit is contained in:
Adam Geiger 2017-03-10 13:43:20 -05:00 committed by Emile Vauge
parent bf3f6e2029
commit 138fea17ed
No known key found for this signature in database
GPG key ID: D808B4C167352E59

View file

@ -548,7 +548,7 @@ func (server *Server) buildEntryPoints(globalConfiguration GlobalConfiguration)
// provider configurations. // provider configurations.
func (server *Server) loadConfig(configurations configs, globalConfiguration GlobalConfiguration) (map[string]*serverEntryPoint, error) { func (server *Server) loadConfig(configurations configs, globalConfiguration GlobalConfiguration) (map[string]*serverEntryPoint, error) {
serverEntryPoints := server.buildEntryPoints(globalConfiguration) serverEntryPoints := server.buildEntryPoints(globalConfiguration)
redirectHandlers := make(map[string]http.Handler) redirectHandlers := make(map[string]negroni.Handler)
backends := map[string]http.Handler{} backends := map[string]http.Handler{}
@ -596,42 +596,43 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
} }
entryPoint := globalConfiguration.EntryPoints[entryPointName] entryPoint := globalConfiguration.EntryPoints[entryPointName]
negroni := negroni.New()
if entryPoint.Redirect != nil { if entryPoint.Redirect != nil {
if redirectHandlers[entryPointName] != nil { if redirectHandlers[entryPointName] != nil {
newServerRoute.route.Handler(redirectHandlers[entryPointName]) negroni.Use(redirectHandlers[entryPointName])
} else if handler, err := server.loadEntryPointConfig(entryPointName, entryPoint); err != nil { } else if handler, err := server.loadEntryPointConfig(entryPointName, entryPoint); err != nil {
log.Errorf("Error loading entrypoint configuration for frontend %s: %v", frontendName, err) log.Errorf("Error loading entrypoint configuration for frontend %s: %v", frontendName, err)
log.Errorf("Skipping frontend %s...", frontendName) log.Errorf("Skipping frontend %s...", frontendName)
continue frontend continue frontend
} else { } else {
newServerRoute.route.Handler(handler) negroni.Use(handler)
redirectHandlers[entryPointName] = handler redirectHandlers[entryPointName] = handler
} }
} else { }
if backends[frontend.Backend] == nil { if backends[frontend.Backend] == nil {
log.Debugf("Creating backend %s", frontend.Backend) log.Debugf("Creating backend %s", frontend.Backend)
var lb http.Handler var lb http.Handler
rr, _ := roundrobin.New(saveBackend) rr, _ := roundrobin.New(saveBackend)
if configuration.Backends[frontend.Backend] == nil { if configuration.Backends[frontend.Backend] == nil {
log.Errorf("Undefined backend '%s' for frontend %s", frontend.Backend, frontendName) log.Errorf("Undefined backend '%s' for frontend %s", frontend.Backend, frontendName)
log.Errorf("Skipping frontend %s...", frontendName) log.Errorf("Skipping frontend %s...", frontendName)
continue frontend continue frontend
} }
lbMethod, err := types.NewLoadBalancerMethod(configuration.Backends[frontend.Backend].LoadBalancer) lbMethod, err := types.NewLoadBalancerMethod(configuration.Backends[frontend.Backend].LoadBalancer)
if err != nil { if err != nil {
log.Errorf("Error loading load balancer method '%+v' for frontend %s: %v", configuration.Backends[frontend.Backend].LoadBalancer, frontendName, err) log.Errorf("Error loading load balancer method '%+v' for frontend %s: %v", configuration.Backends[frontend.Backend].LoadBalancer, frontendName, err)
log.Errorf("Skipping frontend %s...", frontendName) log.Errorf("Skipping frontend %s...", frontendName)
continue frontend continue frontend
} }
stickysession := configuration.Backends[frontend.Backend].LoadBalancer.Sticky stickysession := configuration.Backends[frontend.Backend].LoadBalancer.Sticky
cookiename := "_TRAEFIK_BACKEND" cookiename := "_TRAEFIK_BACKEND"
var sticky *roundrobin.StickySession var sticky *roundrobin.StickySession
if stickysession { if stickysession {
sticky = roundrobin.NewStickySession(cookiename) sticky = roundrobin.NewStickySession(cookiename)
} }
switch lbMethod { switch lbMethod {
case types.Drr: case types.Drr:
@ -656,9 +657,9 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
log.Errorf("Skipping frontend %s...", frontendName) log.Errorf("Skipping frontend %s...", frontendName)
continue frontend continue frontend
} }
hcOpts := parseHealthCheckOptions(rebalancer, frontend.Backend, configuration.Backends[frontend.Backend].HealthCheck, *globalConfiguration.HealthCheck) hcOpts := parseHealthCheckOptions(rebalancer,frontend.Backend, configuration.Backends[frontend.Backend].HealthCheck, *globalConfiguration.HealthCheck)
if hcOpts != nil { if hcOpts != nil {
log.Debugf("Setting up backend health check %s", *hcOpts) log.Debugf("Setting up backend health check %s", *hcOpts)
backendsHealthcheck[frontend.Backend] = healthcheck.NewBackendHealthCheck(*hcOpts) backendsHealthcheck[frontend.Backend] = healthcheck.NewBackendHealthCheck(*hcOpts)
} }
} }
@ -684,9 +685,9 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
continue frontend continue frontend
} }
} }
hcOpts := parseHealthCheckOptions(rr, frontend.Backend, configuration.Backends[frontend.Backend].HealthCheck, *globalConfiguration.HealthCheck) hcOpts := parseHealthCheckOptions(rr,frontend.Backend, configuration.Backends[frontend.Backend].HealthCheck, *globalConfiguration.HealthCheck)
if hcOpts != nil { if hcOpts != nil {
log.Debugf("Setting up backend health check %s", *hcOpts) log.Debugf("Setting up backend health check %s", *hcOpts)
backendsHealthcheck[frontend.Backend] = healthcheck.NewBackendHealthCheck(*hcOpts) backendsHealthcheck[frontend.Backend] = healthcheck.NewBackendHealthCheck(*hcOpts)
} }
} }
@ -716,15 +717,14 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
log.Debugf("Creating retries max attempts %d", retries) log.Debugf("Creating retries max attempts %d", retries)
} }
var negroni = negroni.New()
if server.globalConfiguration.Web != nil && server.globalConfiguration.Web.Metrics != nil { if server.globalConfiguration.Web != nil && server.globalConfiguration.Web.Metrics != nil {
if server.globalConfiguration.Web.Metrics.Prometheus != nil { if server.globalConfiguration.Web.Metrics.Prometheus != nil {
metricsMiddlewareBackend := middlewares.NewMetricsWrapper(middlewares.NewPrometheus(frontend.Backend, server.globalConfiguration.Web.Metrics.Prometheus)) metricsMiddlewareBackend := middlewares.NewMetricsWrapper(middlewares.NewPrometheus(frontend.Backend, server.globalConfiguration.Web.Metrics.Prometheus))
negroni.Use(metricsMiddlewareBackend) negroni.Use(metricsMiddlewareBackend)
} }
} }
if len(frontend.BasicAuth) > 0 {
if len(frontend.BasicAuth) > 0 {
users := types.Users{} users := types.Users{}
for _, user := range frontend.BasicAuth { for _, user := range frontend.BasicAuth {
users = append(users, user) users = append(users, user)
@ -739,9 +739,7 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
log.Fatal("Error creating Auth: ", err) log.Fatal("Error creating Auth: ", err)
} }
negroni.Use(authMiddleware) negroni.Use(authMiddleware)
} } if configuration.Backends[frontend.Backend].CircuitBreaker != nil {
if configuration.Backends[frontend.Backend].CircuitBreaker != nil {
log.Debugf("Creating circuit breaker %s", configuration.Backends[frontend.Backend].CircuitBreaker.Expression) log.Debugf("Creating circuit breaker %s", configuration.Backends[frontend.Backend].CircuitBreaker.Expression)
cbreaker, err := middlewares.NewCircuitBreaker(lb, configuration.Backends[frontend.Backend].CircuitBreaker.Expression, cbreaker.Logger(oxyLogger)) cbreaker, err := middlewares.NewCircuitBreaker(lb, configuration.Backends[frontend.Backend].CircuitBreaker.Expression, cbreaker.Logger(oxyLogger))
if err != nil { if err != nil {
@ -761,7 +759,7 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
newServerRoute.route.Priority(frontend.Priority) newServerRoute.route.Priority(frontend.Priority)
} }
server.wireFrontendBackend(newServerRoute, backends[frontend.Backend]) server.wireFrontendBackend(newServerRoute, backends[frontend.Backend])
}
err := newServerRoute.route.GetError() err := newServerRoute.route.GetError()
if err != nil { if err != nil {
log.Errorf("Error building route: %s", err) log.Errorf("Error building route: %s", err)
@ -811,7 +809,7 @@ func (server *Server) wireFrontendBackend(serverRoute *serverRoute, handler http
serverRoute.route.Handler(handler) serverRoute.route.Handler(handler)
} }
func (server *Server) loadEntryPointConfig(entryPointName string, entryPoint *EntryPoint) (http.Handler, error) { func (server *Server) loadEntryPointConfig(entryPointName string, entryPoint *EntryPoint) (negroni.Handler, error) {
regex := entryPoint.Redirect.Regex regex := entryPoint.Redirect.Regex
replacement := entryPoint.Redirect.Replacement replacement := entryPoint.Redirect.Replacement
if len(entryPoint.Redirect.EntryPoint) > 0 { if len(entryPoint.Redirect.EntryPoint) > 0 {
@ -835,9 +833,8 @@ func (server *Server) loadEntryPointConfig(entryPointName string, entryPoint *En
return nil, err return nil, err
} }
log.Debugf("Creating entryPoint redirect %s -> %s : %s -> %s", entryPointName, entryPoint.Redirect.EntryPoint, regex, replacement) log.Debugf("Creating entryPoint redirect %s -> %s : %s -> %s", entryPointName, entryPoint.Redirect.EntryPoint, regex, replacement)
negroni := negroni.New()
negroni.Use(rewrite) return rewrite, nil
return negroni, nil
} }
func (server *Server) buildDefaultHTTPRouter() *mux.Router { func (server *Server) buildDefaultHTTPRouter() *mux.Router {