mirror of
https://github.com/containous/traefik.git
synced 2025-01-25 06:03:49 +03:00
feat(kv): add error pages configuration.
This commit is contained in:
parent
cfa1f47226
commit
51390aa874
@ -81,6 +81,18 @@ func withPair(key string, value string) func(map[string]string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func withErrorPage(name string, backend, query, status string) func(map[string]string) {
|
||||||
|
return func(pairs map[string]string) {
|
||||||
|
if len(name) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
withPair(pathFrontendErrorPages+name+pathFrontendErrorPagesBackend, backend)(pairs)
|
||||||
|
withPair(pathFrontendErrorPages+name+pathFrontendErrorPagesQuery, query)(pairs)
|
||||||
|
withPair(pathFrontendErrorPages+name+pathFrontendErrorPagesStatus, status)(pairs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFiller(t *testing.T) {
|
func TestFiller(t *testing.T) {
|
||||||
expected := []*store.KVPair{
|
expected := []*store.KVPair{
|
||||||
{Key: "traefik/backends/backend.with.dot.too", Value: []byte("")},
|
{Key: "traefik/backends/backend.with.dot.too", Value: []byte("")},
|
||||||
|
@ -35,6 +35,8 @@ func (p *Provider) buildConfiguration() *types.Configuration {
|
|||||||
|
|
||||||
// Frontend functions
|
// Frontend functions
|
||||||
"getRedirect": p.getRedirect,
|
"getRedirect": p.getRedirect,
|
||||||
|
"getErrorPages": p.getErrorPages,
|
||||||
|
|
||||||
// Backend functions
|
// Backend functions
|
||||||
"getSticky": p.getSticky,
|
"getSticky": p.getSticky,
|
||||||
"hasStickinessLabel": p.hasStickinessLabel,
|
"hasStickinessLabel": p.hasStickinessLabel,
|
||||||
@ -96,6 +98,28 @@ func (p *Provider) getRedirect(rootPath string) *types.Redirect {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Provider) getErrorPages(rootPath string) map[string]*types.ErrorPage {
|
||||||
|
var errorPages map[string]*types.ErrorPage
|
||||||
|
|
||||||
|
pathErrors := p.list(rootPath, pathFrontendErrorPages)
|
||||||
|
|
||||||
|
for _, pathPage := range pathErrors {
|
||||||
|
if errorPages == nil {
|
||||||
|
errorPages = make(map[string]*types.ErrorPage)
|
||||||
|
}
|
||||||
|
|
||||||
|
pageName := p.last(pathPage)
|
||||||
|
|
||||||
|
errorPages[pageName] = &types.ErrorPage{
|
||||||
|
Backend: p.get("", pathPage, pathFrontendErrorPagesBackend),
|
||||||
|
Query: p.get("", pathPage, pathFrontendErrorPagesQuery),
|
||||||
|
Status: p.splitGet(pathPage, pathFrontendErrorPagesStatus),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return errorPages
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Provider) listServers(backend string) []string {
|
func (p *Provider) listServers(backend string) []string {
|
||||||
serverNames := p.list(backend, pathBackendServers)
|
serverNames := p.list(backend, pathBackendServers)
|
||||||
return fun.Filter(p.serverFilter, serverNames).([]string)
|
return fun.Filter(p.serverFilter, serverNames).([]string)
|
||||||
|
@ -770,3 +770,52 @@ func TestProviderGetRedirect(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestProviderGetErrorPages(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
desc string
|
||||||
|
rootPath string
|
||||||
|
kvPairs []*store.KVPair
|
||||||
|
expected map[string]*types.ErrorPage
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
desc: "2 errors pages",
|
||||||
|
rootPath: "traefik/frontends/foo",
|
||||||
|
kvPairs: filler("traefik",
|
||||||
|
frontend("foo",
|
||||||
|
withErrorPage("foo", "error", "/test1", "500-501, 503-599"),
|
||||||
|
withErrorPage("bar", "error", "/test2", "400-405"))),
|
||||||
|
expected: map[string]*types.ErrorPage{
|
||||||
|
"foo": {
|
||||||
|
Backend: "error",
|
||||||
|
Query: "/test1",
|
||||||
|
Status: []string{"500-501", "503-599"},
|
||||||
|
},
|
||||||
|
"bar": {
|
||||||
|
Backend: "error",
|
||||||
|
Query: "/test2",
|
||||||
|
Status: []string{"400-405"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "return nil when no errors pages",
|
||||||
|
rootPath: "traefik/frontends/foo",
|
||||||
|
kvPairs: filler("traefik", frontend("foo")),
|
||||||
|
expected: nil,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range testCases {
|
||||||
|
test := test
|
||||||
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
p := newProviderMock(test.kvPairs)
|
||||||
|
|
||||||
|
actual := p.getErrorPages(test.rootPath)
|
||||||
|
|
||||||
|
assert.Equal(t, test.expected, actual)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -78,6 +78,19 @@
|
|||||||
replacement = "{{ $redirect.Replacement }}"
|
replacement = "{{ $redirect.Replacement }}"
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
|
{{ $errorPages := getErrorPages $frontend }}
|
||||||
|
{{ if $errorPages }}
|
||||||
|
[frontends."{{$frontendName}}".errors]
|
||||||
|
{{ range $pageName, $page := $errorPages }}
|
||||||
|
[frontends."{{$frontendName}}".errors.{{ $pageName }}]
|
||||||
|
status = [{{range $page.Status}}
|
||||||
|
"{{.}}",
|
||||||
|
{{end}}]
|
||||||
|
backend = "{{$page.Backend}}"
|
||||||
|
query = "{{$page.Query}}"
|
||||||
|
{{end}}
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{range $route := List $frontend "/routes/"}}
|
{{range $route := List $frontend "/routes/"}}
|
||||||
[frontends."{{$frontendName}}".routes."{{Last $route}}"]
|
[frontends."{{$frontendName}}".routes."{{Last $route}}"]
|
||||||
rule = "{{Get "" $route "/rule"}}"
|
rule = "{{Get "" $route "/rule"}}"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user