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

View file

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

View file

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

View file

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