1
0
Fork 0

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,18 +596,19 @@ 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
@ -716,14 +717,13 @@ 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 {
@ -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 {