Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add static redirect #4090

Merged
merged 4 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 5 additions & 19 deletions server/server_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))

Expand Down Expand Up @@ -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()

Expand All @@ -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)
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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)
}
Expand Down
17 changes: 6 additions & 11 deletions server/server_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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.")
}
}
Expand Down Expand Up @@ -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{}
Expand Down
8 changes: 8 additions & 0 deletions server/server_middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions server/server_middlewares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
5 changes: 1 addition & 4 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down