add static redirect

This commit is contained in:
Manuel Zapf 2018-10-29 16:48:06 +01:00 committed by Traefiker Bot
parent 993caf5058
commit c6dd1dccc3
5 changed files with 21 additions and 36 deletions

View file

@ -42,13 +42,7 @@ func (s *Server) loadConfiguration(configMsg types.ConfigMessage) {
s.metricsRegistry.ConfigReloadsCounter().Add(1)
newServerEntryPoints, err := s.loadConfig(newConfigurations, s.globalConfiguration)
if err != nil {
s.metricsRegistry.ConfigReloadsFailureCounter().Add(1)
s.metricsRegistry.LastConfigReloadFailureGauge().Set(float64(time.Now().Unix()))
log.Error("Error loading new configuration, aborted ", err)
return
}
newServerEntryPoints := s.loadConfig(newConfigurations, s.globalConfiguration)
s.metricsRegistry.LastConfigReloadSuccessGauge().Set(float64(time.Now().Unix()))
@ -77,11 +71,7 @@ func (s *Server) loadConfiguration(configMsg types.ConfigMessage) {
// loadConfig returns a new gorilla.mux Route from the specified global configuration and the dynamic
// provider configurations.
func (s *Server) loadConfig(configurations types.Configurations, globalConfiguration configuration.GlobalConfiguration) (map[string]*serverEntryPoint, error) {
redirectHandlers, err := s.buildEntryPointRedirect()
if err != nil {
return nil, err
}
func (s *Server) loadConfig(configurations types.Configurations, globalConfiguration configuration.GlobalConfiguration) map[string]*serverEntryPoint {
serverEntryPoints := s.buildServerEntryPoints()
@ -95,7 +85,7 @@ func (s *Server) loadConfig(configurations types.Configurations, globalConfigura
for _, frontendName := range frontendNames {
frontendPostConfigs, err := s.loadFrontendConfig(providerName, frontendName, config,
redirectHandlers, serverEntryPoints,
serverEntryPoints,
backendsHandlers, backendsHealthCheck)
if err != nil {
log.Errorf("%v. Skipping frontend %s...", err, frontendName)
@ -128,12 +118,12 @@ func (s *Server) loadConfig(configurations types.Configurations, globalConfigura
}
}
return serverEntryPoints, err
return serverEntryPoints
}
func (s *Server) loadFrontendConfig(
providerName string, frontendName string, config *types.Configuration,
redirectHandlers map[string]negroni.Handler, serverEntryPoints map[string]*serverEntryPoint,
serverEntryPoints map[string]*serverEntryPoint,
backendsHandlers map[string]http.Handler, backendsHealthCheck map[string]*healthcheck.BackendConfig,
) ([]handlerPostConfig, error) {
@ -194,10 +184,6 @@ func (s *Server) loadFrontendConfig(
n := negroni.New()
if _, exist := redirectHandlers[entryPointName]; exist {
n.Use(redirectHandlers[entryPointName])
}
for _, handler := range handlers {
n.Use(handler)
}

View file

@ -135,8 +135,7 @@ func TestServerLoadConfigHealthCheckOptions(t *testing.T) {
srv := NewServer(globalConfig, nil, entryPoints)
_, err := srv.loadConfig(dynamicConfigs, globalConfig)
require.NoError(t, err)
_ = srv.loadConfig(dynamicConfigs, globalConfig)
expectedNumHealthCheckBackends := 0
if healthCheck != nil {
@ -187,8 +186,7 @@ func TestServerLoadConfigEmptyBasicAuth(t *testing.T) {
}
srv := NewServer(globalConfig, nil, entryPoints)
_, err := srv.loadConfig(dynamicConfigs, globalConfig)
require.NoError(t, err)
_ = srv.loadConfig(dynamicConfigs, globalConfig)
}
func TestServerLoadCertificateWithDefaultEntryPoint(t *testing.T) {
@ -214,9 +212,9 @@ func TestServerLoadCertificateWithDefaultEntryPoint(t *testing.T) {
}
srv := NewServer(globalConfig, nil, entryPoints)
if mapEntryPoints, err := srv.loadConfig(dynamicConfigs, globalConfig); err != nil {
t.Fatalf("got error: %s", err)
} else if !mapEntryPoints["https"].certs.ContainsCertificates() {
mapEntryPoints := srv.loadConfig(dynamicConfigs, globalConfig)
if !mapEntryPoints["https"].certs.ContainsCertificates() {
t.Fatal("got error: https entryPoint must have TLS certificates.")
}
}
@ -259,10 +257,7 @@ func TestReuseBackend(t *testing.T) {
srv := NewServer(globalConfig, nil, entryPoints)
serverEntryPoints, err := srv.loadConfig(dynamicConfigs, globalConfig)
if err != nil {
t.Fatalf("error loading config: %s", err)
}
serverEntryPoints := srv.loadConfig(dynamicConfigs, globalConfig)
// Test that the /ok path returns a status 200.
responseRecorderOk := &httptest.ResponseRecorder{}

View file

@ -163,6 +163,14 @@ func (s *Server) buildServerEntryPointMiddlewares(serverEntryPointName string, s
}
}
if s.entryPoints[serverEntryPointName].Configuration.Redirect != nil {
redirectHandlers, err := s.buildEntryPointRedirect()
if err != nil {
return nil, fmt.Errorf("failed to create redirect middleware: %v", err)
}
serverMiddlewares = append(serverMiddlewares, redirectHandlers[serverEntryPointName])
}
if s.entryPoints[serverEntryPointName].Configuration.Auth != nil {
authMiddleware, err := mauth.NewAuthenticator(s.entryPoints[serverEntryPointName].Configuration.Auth, s.tracingMiddleware)
if err != nil {

View file

@ -285,6 +285,5 @@ func TestServerGenericFrontendAuthFail(t *testing.T) {
srv := NewServer(globalConfig, nil, nil)
_, err := srv.loadConfig(dynamicConfigs, globalConfig)
require.NoError(t, err)
_ = srv.loadConfig(dynamicConfigs, globalConfig)
}

View file

@ -333,10 +333,7 @@ func TestServerResponseEmptyBackend(t *testing.T) {
dynamicConfigs := types.Configurations{"config": test.config(testServer.URL)}
srv := NewServer(globalConfig, nil, entryPointsConfig)
entryPoints, err := srv.loadConfig(dynamicConfigs, globalConfig)
if err != nil {
t.Fatalf("error loading config: %s", err)
}
entryPoints := srv.loadConfig(dynamicConfigs, globalConfig)
responseRecorder := &httptest.ResponseRecorder{}
request := httptest.NewRequest(http.MethodGet, testServer.URL+requestPath, nil)