2017-11-09 15:12:04 +00:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/containous/mux"
|
|
|
|
"github.com/containous/traefik/log"
|
|
|
|
"github.com/containous/traefik/safe"
|
|
|
|
"github.com/containous/traefik/types"
|
|
|
|
"github.com/unrolled/render"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Provider is a provider.Provider implementation that provides a Rest API
|
|
|
|
type Provider struct {
|
2018-01-29 13:58:03 +00:00
|
|
|
configurationChan chan<- types.ConfigMessage
|
|
|
|
EntryPoint string `description:"EntryPoint" export:"true"`
|
2017-11-09 15:12:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var templatesRenderer = render.New(render.Options{Directory: "nowhere"})
|
|
|
|
|
2018-07-11 07:08:03 +00:00
|
|
|
// Init the provider
|
|
|
|
func (p *Provider) Init(_ types.Constraints) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-09 15:12:04 +00:00
|
|
|
// AddRoutes add rest provider routes on a router
|
|
|
|
func (p *Provider) AddRoutes(systemRouter *mux.Router) {
|
2017-11-20 08:40:03 +00:00
|
|
|
systemRouter.
|
|
|
|
Methods(http.MethodPut).
|
|
|
|
Path("/api/providers/{provider}").
|
|
|
|
HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
2017-11-09 15:12:04 +00:00
|
|
|
|
2017-11-20 08:40:03 +00:00
|
|
|
vars := mux.Vars(request)
|
|
|
|
// TODO: Deprecated configuration - Need to be removed in the future
|
|
|
|
if vars["provider"] != "web" && vars["provider"] != "rest" {
|
|
|
|
response.WriteHeader(http.StatusBadRequest)
|
|
|
|
fmt.Fprint(response, "Only 'rest' provider can be updated through the REST API")
|
|
|
|
return
|
|
|
|
} else if vars["provider"] == "web" {
|
|
|
|
log.Warn("The provider web is deprecated. Please use /rest instead")
|
|
|
|
}
|
|
|
|
|
|
|
|
configuration := new(types.Configuration)
|
|
|
|
body, _ := ioutil.ReadAll(request.Body)
|
|
|
|
err := json.Unmarshal(body, configuration)
|
|
|
|
if err == nil {
|
|
|
|
// TODO: Deprecated configuration - Change to `rest` in the future
|
|
|
|
p.configurationChan <- types.ConfigMessage{ProviderName: "web", Configuration: configuration}
|
2018-01-29 13:58:03 +00:00
|
|
|
err := templatesRenderer.JSON(response, http.StatusOK, configuration)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2017-11-20 08:40:03 +00:00
|
|
|
} else {
|
|
|
|
log.Errorf("Error parsing configuration %+v", err)
|
|
|
|
http.Error(response, fmt.Sprintf("%+v", err), http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
})
|
2017-11-09 15:12:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Provide allows the provider to provide configurations to traefik
|
|
|
|
// using the given configuration channel.
|
2018-07-11 07:08:03 +00:00
|
|
|
func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool) error {
|
2017-11-09 15:12:04 +00:00
|
|
|
p.configurationChan = configurationChan
|
|
|
|
return nil
|
|
|
|
}
|