22 Star 72 Fork 1

Gitee 极速下载 / traefik

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/containous/traefik
克隆/下载
rest.go 2.11 KB
一键复制 编辑 原始数据 按行查看 历史
Romain 提交于 2020-09-21 10:20 . chore: move to Traefik organization.
package rest
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/containous/mux"
"github.com/traefik/traefik/log"
"github.com/traefik/traefik/safe"
"github.com/traefik/traefik/types"
"github.com/unrolled/render"
)
// Provider is a provider.Provider implementation that provides a Rest API
type Provider struct {
configurationChan chan<- types.ConfigMessage
EntryPoint string `description:"EntryPoint" export:"true"`
}
var templatesRenderer = render.New(render.Options{Directory: "nowhere"})
// Init the provider
func (p *Provider) Init(_ types.Constraints) error {
return nil
}
// AddRoutes add rest provider routes on a router
func (p *Provider) AddRoutes(systemRouter *mux.Router) {
systemRouter.
Methods(http.MethodPut).
Path("/api/providers/{provider}").
HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
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}
err := templatesRenderer.JSON(response, http.StatusOK, configuration)
if err != nil {
log.Error(err)
}
} else {
log.Errorf("Error parsing configuration %+v", err)
http.Error(response, fmt.Sprintf("%+v", err), http.StatusBadRequest)
}
})
}
// Provide allows the provider to provide configurations to traefik
// using the given configuration channel.
func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool) error {
p.configurationChan = configurationChan
return nil
}
Go
1
https://gitee.com/mirrors/traefik.git
git@gitee.com:mirrors/traefik.git
mirrors
traefik
traefik
v1.7.28

搜索帮助

53164aa7 5694891 3bd8fe86 5694891