mirror of
https://github.com/containous/traefik.git
synced 2024-12-23 17:34:13 +03:00
feat: Add defaultFuncMap to template
This commit is contained in:
parent
686faf0556
commit
ec05fbcf19
@ -58,7 +58,19 @@ func (p *BaseProvider) getConfiguration(defaultTemplateFile string, funcMap temp
|
||||
err error
|
||||
)
|
||||
configuration := new(types.Configuration)
|
||||
tmpl := template.New(p.Filename).Funcs(funcMap)
|
||||
var defaultFuncMap = template.FuncMap{
|
||||
"replace": replace,
|
||||
"tolower": strings.ToLower,
|
||||
"normalize": normalize,
|
||||
"split": split,
|
||||
"contains": contains,
|
||||
}
|
||||
|
||||
for funcID, funcElement := range funcMap {
|
||||
defaultFuncMap[funcID] = funcElement
|
||||
}
|
||||
|
||||
tmpl := template.New(p.Filename).Funcs(defaultFuncMap)
|
||||
if len(p.Filename) > 0 {
|
||||
buf, err = ioutil.ReadFile(p.Filename)
|
||||
if err != nil {
|
||||
@ -93,6 +105,14 @@ func replace(s1 string, s2 string, s3 string) string {
|
||||
return strings.Replace(s3, s1, s2, -1)
|
||||
}
|
||||
|
||||
func contains(substr, s string) bool {
|
||||
return strings.Contains(s, substr)
|
||||
}
|
||||
|
||||
func split(sep, s string) []string {
|
||||
return strings.Split(s, sep)
|
||||
}
|
||||
|
||||
func normalize(name string) string {
|
||||
fargs := func(c rune) bool {
|
||||
return !unicode.IsLetter(c) && !unicode.IsNumber(c)
|
||||
|
@ -325,3 +325,58 @@ func TestMatchingConstraints(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultFuncMap(t *testing.T) {
|
||||
templateFile, err := ioutil.TempFile("", "provider-configuration")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(templateFile.Name())
|
||||
data := []byte(`
|
||||
[backends]
|
||||
[backends.{{ "backend-1" | replace "-" "" }}]
|
||||
[backends.{{ "BACKEND1" | tolower }}.circuitbreaker]
|
||||
expression = "NetworkErrorRatio() > 0.5"
|
||||
[backends.servers.server1]
|
||||
url = "http://172.17.0.2:80"
|
||||
weight = 10
|
||||
[backends.backend1.servers.server2]
|
||||
url = "http://172.17.0.3:80"
|
||||
weight = 1
|
||||
|
||||
[frontends]
|
||||
[frontends.{{normalize "frontend/1"}}]
|
||||
{{ $backend := "backend1/test/value" | split "/" }}
|
||||
{{ $backendid := index $backend 1 }}
|
||||
{{ if "backend1" | contains "backend" }}
|
||||
backend = "backend1"
|
||||
{{end}}
|
||||
passHostHeader = true
|
||||
[frontends.frontend-1.routes.test_2]
|
||||
rule = "Path"
|
||||
value = "/test"`)
|
||||
err = ioutil.WriteFile(templateFile.Name(), data, 0700)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
provider := &myProvider{
|
||||
BaseProvider{
|
||||
Filename: templateFile.Name(),
|
||||
},
|
||||
nil,
|
||||
}
|
||||
configuration, err := provider.getConfiguration(templateFile.Name(), nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Shouldn't have error out, got %v", err)
|
||||
}
|
||||
if configuration == nil {
|
||||
t.Fatalf("Configuration should not be nil, but was")
|
||||
}
|
||||
if _, ok := configuration.Backends["backend1"]; !ok {
|
||||
t.Fatalf("backend1 should exists, but it not")
|
||||
}
|
||||
if _, ok := configuration.Frontends["frontend-1"]; !ok {
|
||||
t.Fatalf("Frontend frontend-1 should exists, but it not")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user