2015-11-01 15:35:01 +00:00
|
|
|
package provider
|
|
|
|
|
2015-11-13 10:50:32 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
"github.com/emilevauge/traefik/autogen"
|
|
|
|
"github.com/emilevauge/traefik/types"
|
|
|
|
)
|
2015-11-01 15:35:01 +00:00
|
|
|
|
2015-11-01 18:29:47 +00:00
|
|
|
// Provider defines methods of a provider.
|
2015-11-01 15:35:01 +00:00
|
|
|
type Provider interface {
|
2015-11-01 18:29:47 +00:00
|
|
|
// Provide allows the provider to provide configurations to traefik
|
|
|
|
// using the given configuration channel.
|
2015-11-01 15:35:01 +00:00
|
|
|
Provide(configurationChan chan<- types.ConfigMessage) error
|
|
|
|
}
|
2015-11-13 10:50:32 +00:00
|
|
|
|
2016-01-13 21:46:44 +00:00
|
|
|
// BaseProvider should be inherited by providers
|
|
|
|
type BaseProvider struct {
|
2015-11-13 10:50:32 +00:00
|
|
|
Watch bool
|
|
|
|
Filename string
|
|
|
|
}
|
|
|
|
|
2016-01-13 21:46:44 +00:00
|
|
|
func (p *BaseProvider) getConfiguration(defaultTemplateFile string, funcMap template.FuncMap, templateObjects interface{}) (*types.Configuration, error) {
|
2015-11-13 10:50:32 +00:00
|
|
|
var (
|
|
|
|
buf []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
configuration := new(types.Configuration)
|
|
|
|
tmpl := template.New(p.Filename).Funcs(funcMap)
|
|
|
|
if len(p.Filename) > 0 {
|
|
|
|
buf, err = ioutil.ReadFile(p.Filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buf, err = autogen.Asset(defaultTemplateFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err = tmpl.Parse(string(buf))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
err = tmpl.Execute(&buffer, templateObjects)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := toml.Decode(buffer.String(), configuration); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return configuration, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func replace(s1 string, s2 string, s3 string) string {
|
|
|
|
return strings.Replace(s3, s1, s2, -1)
|
|
|
|
}
|
|
|
|
|
2016-02-03 02:02:28 +00:00
|
|
|
// Escape beginning slash "/", convert all others to dash "-"
|
2015-11-13 10:50:32 +00:00
|
|
|
func getEscapedName(name string) string {
|
2016-02-03 02:02:28 +00:00
|
|
|
return strings.Replace(strings.TrimPrefix(name, "/"), "/", "-", -1)
|
2015-11-13 10:50:32 +00:00
|
|
|
}
|