Dynamic Configuration Refactoring
This commit is contained in:
parent
d3ae88f108
commit
a09dfa3ce1
452 changed files with 21023 additions and 9419 deletions
|
@ -13,10 +13,10 @@ type DashboardHandler struct {
|
|||
Assets *assetfs.AssetFS
|
||||
}
|
||||
|
||||
// AddRoutes add dashboard routes on a router
|
||||
func (g DashboardHandler) AddRoutes(router *mux.Router) {
|
||||
// Append add dashboard routes on a router
|
||||
func (g DashboardHandler) Append(router *mux.Router) {
|
||||
if g.Assets == nil {
|
||||
log.Error("No assets for dashboard")
|
||||
log.WithoutContext().Error("No assets for dashboard")
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -11,7 +11,8 @@ import (
|
|||
)
|
||||
|
||||
func init() {
|
||||
expvar.Publish("Goroutines", expvar.Func(goroutines))
|
||||
// FIXME Goroutines2 -> Goroutines
|
||||
expvar.Publish("Goroutines2", expvar.Func(goroutines))
|
||||
}
|
||||
|
||||
func goroutines() interface{} {
|
||||
|
@ -21,8 +22,8 @@ func goroutines() interface{} {
|
|||
// DebugHandler expose debug routes
|
||||
type DebugHandler struct{}
|
||||
|
||||
// AddRoutes add debug routes on a router
|
||||
func (g DebugHandler) AddRoutes(router *mux.Router) {
|
||||
// Append add debug routes on a router
|
||||
func (g DebugHandler) Append(router *mux.Router) {
|
||||
router.Methods(http.MethodGet).Path("/debug/vars").
|
||||
HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
|
|
462
api/handler.go
462
api/handler.go
|
@ -1,252 +1,304 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/containous/mux"
|
||||
"github.com/containous/traefik/config"
|
||||
"github.com/containous/traefik/log"
|
||||
"github.com/containous/traefik/middlewares"
|
||||
"github.com/containous/traefik/safe"
|
||||
"github.com/containous/traefik/types"
|
||||
"github.com/containous/traefik/version"
|
||||
"github.com/elazarl/go-bindata-assetfs"
|
||||
thoas_stats "github.com/thoas/stats"
|
||||
thoasstats "github.com/thoas/stats"
|
||||
"github.com/unrolled/render"
|
||||
)
|
||||
|
||||
// ResourceIdentifier a resource identifier
|
||||
type ResourceIdentifier struct {
|
||||
ID string `json:"id"`
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
// ProviderRepresentation a provider with resource identifiers
|
||||
type ProviderRepresentation struct {
|
||||
Routers []ResourceIdentifier `json:"routers,omitempty"`
|
||||
Middlewares []ResourceIdentifier `json:"middlewares,omitempty"`
|
||||
Services []ResourceIdentifier `json:"services,omitempty"`
|
||||
}
|
||||
|
||||
// RouterRepresentation extended version of a router configuration with an ID
|
||||
type RouterRepresentation struct {
|
||||
*config.Router
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
// MiddlewareRepresentation extended version of a middleware configuration with an ID
|
||||
type MiddlewareRepresentation struct {
|
||||
*config.Middleware
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
// ServiceRepresentation extended version of a service configuration with an ID
|
||||
type ServiceRepresentation struct {
|
||||
*config.Service
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
// Handler expose api routes
|
||||
type Handler struct {
|
||||
EntryPoint string `description:"EntryPoint" export:"true"`
|
||||
Dashboard bool `description:"Activate dashboard" export:"true"`
|
||||
Debug bool `export:"true"`
|
||||
EntryPoint string
|
||||
Dashboard bool
|
||||
Debug bool
|
||||
CurrentConfigurations *safe.Safe
|
||||
Statistics *types.Statistics `description:"Enable more detailed statistics" export:"true"`
|
||||
Stats *thoas_stats.Stats `json:"-"`
|
||||
StatsRecorder *middlewares.StatsRecorder `json:"-"`
|
||||
DashboardAssets *assetfs.AssetFS `json:"-"`
|
||||
Statistics *types.Statistics
|
||||
Stats *thoasstats.Stats
|
||||
// StatsRecorder *middlewares.StatsRecorder // FIXME stats
|
||||
DashboardAssets *assetfs.AssetFS
|
||||
}
|
||||
|
||||
var (
|
||||
templatesRenderer = render.New(render.Options{
|
||||
Directory: "nowhere",
|
||||
})
|
||||
)
|
||||
var templateRenderer jsonRenderer = render.New(render.Options{Directory: "nowhere"})
|
||||
|
||||
// AddRoutes add api routes on a router
|
||||
func (p Handler) AddRoutes(router *mux.Router) {
|
||||
type jsonRenderer interface {
|
||||
JSON(w io.Writer, status int, v interface{}) error
|
||||
}
|
||||
|
||||
// Append add api routes on a router
|
||||
func (p Handler) Append(router *mux.Router) {
|
||||
if p.Debug {
|
||||
DebugHandler{}.AddRoutes(router)
|
||||
DebugHandler{}.Append(router)
|
||||
}
|
||||
|
||||
router.Methods(http.MethodGet).Path("/api").HandlerFunc(p.getConfigHandler)
|
||||
router.Methods(http.MethodGet).Path("/api/providers").HandlerFunc(p.getConfigHandler)
|
||||
router.Methods(http.MethodGet).Path("/api/providers").HandlerFunc(p.getProvidersHandler)
|
||||
router.Methods(http.MethodGet).Path("/api/providers/{provider}").HandlerFunc(p.getProviderHandler)
|
||||
router.Methods(http.MethodGet).Path("/api/providers/{provider}/backends").HandlerFunc(p.getBackendsHandler)
|
||||
router.Methods(http.MethodGet).Path("/api/providers/{provider}/backends/{backend}").HandlerFunc(p.getBackendHandler)
|
||||
router.Methods(http.MethodGet).Path("/api/providers/{provider}/backends/{backend}/servers").HandlerFunc(p.getServersHandler)
|
||||
router.Methods(http.MethodGet).Path("/api/providers/{provider}/backends/{backend}/servers/{server}").HandlerFunc(p.getServerHandler)
|
||||
router.Methods(http.MethodGet).Path("/api/providers/{provider}/frontends").HandlerFunc(p.getFrontendsHandler)
|
||||
router.Methods(http.MethodGet).Path("/api/providers/{provider}/frontends/{frontend}").HandlerFunc(p.getFrontendHandler)
|
||||
router.Methods(http.MethodGet).Path("/api/providers/{provider}/frontends/{frontend}/routes").HandlerFunc(p.getRoutesHandler)
|
||||
router.Methods(http.MethodGet).Path("/api/providers/{provider}/frontends/{frontend}/routes/{route}").HandlerFunc(p.getRouteHandler)
|
||||
router.Methods(http.MethodGet).Path("/api/providers/{provider}/routers").HandlerFunc(p.getRoutersHandler)
|
||||
router.Methods(http.MethodGet).Path("/api/providers/{provider}/routers/{router}").HandlerFunc(p.getRouterHandler)
|
||||
router.Methods(http.MethodGet).Path("/api/providers/{provider}/middlewares").HandlerFunc(p.getMiddlewaresHandler)
|
||||
router.Methods(http.MethodGet).Path("/api/providers/{provider}/middlewares/{middleware}").HandlerFunc(p.getMiddlewareHandler)
|
||||
router.Methods(http.MethodGet).Path("/api/providers/{provider}/services").HandlerFunc(p.getServicesHandler)
|
||||
router.Methods(http.MethodGet).Path("/api/providers/{provider}/services/{service}").HandlerFunc(p.getServiceHandler)
|
||||
|
||||
// FIXME stats
|
||||
// health route
|
||||
router.Methods(http.MethodGet).Path("/health").HandlerFunc(p.getHealthHandler)
|
||||
//router.Methods(http.MethodGet).Path("/health").HandlerFunc(p.getHealthHandler)
|
||||
|
||||
version.Handler{}.AddRoutes(router)
|
||||
version.Handler{}.Append(router)
|
||||
|
||||
if p.Dashboard {
|
||||
DashboardHandler{Assets: p.DashboardAssets}.AddRoutes(router)
|
||||
DashboardHandler{Assets: p.DashboardAssets}.Append(router)
|
||||
}
|
||||
}
|
||||
|
||||
func getProviderIDFromVars(vars map[string]string) string {
|
||||
providerID := vars["provider"]
|
||||
// TODO: Deprecated
|
||||
if providerID == "rest" {
|
||||
providerID = "web"
|
||||
func (p Handler) getProvidersHandler(rw http.ResponseWriter, request *http.Request) {
|
||||
// FIXME handle currentConfiguration
|
||||
if p.CurrentConfigurations != nil {
|
||||
currentConfigurations, ok := p.CurrentConfigurations.Get().(config.Configurations)
|
||||
if !ok {
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
var providers []ResourceIdentifier
|
||||
for name := range currentConfigurations {
|
||||
providers = append(providers, ResourceIdentifier{
|
||||
ID: name,
|
||||
Path: "/api/providers/" + name,
|
||||
})
|
||||
}
|
||||
|
||||
err := templateRenderer.JSON(rw, http.StatusOK, providers)
|
||||
if err != nil {
|
||||
log.FromContext(request.Context()).Error(err)
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
return providerID
|
||||
}
|
||||
|
||||
func (p Handler) getConfigHandler(response http.ResponseWriter, request *http.Request) {
|
||||
currentConfigurations := p.CurrentConfigurations.Get().(types.Configurations)
|
||||
err := templatesRenderer.JSON(response, http.StatusOK, currentConfigurations)
|
||||
func (p Handler) getProviderHandler(rw http.ResponseWriter, request *http.Request) {
|
||||
providerID := mux.Vars(request)["provider"]
|
||||
|
||||
currentConfigurations := p.CurrentConfigurations.Get().(config.Configurations)
|
||||
|
||||
provider, ok := currentConfigurations[providerID]
|
||||
if !ok {
|
||||
http.NotFound(rw, request)
|
||||
return
|
||||
}
|
||||
|
||||
var routers []ResourceIdentifier
|
||||
for name := range provider.Routers {
|
||||
routers = append(routers, ResourceIdentifier{
|
||||
ID: name,
|
||||
Path: "/api/providers/" + providerID + "/routers",
|
||||
})
|
||||
}
|
||||
|
||||
var services []ResourceIdentifier
|
||||
for name := range provider.Services {
|
||||
services = append(services, ResourceIdentifier{
|
||||
ID: name,
|
||||
Path: "/api/providers/" + providerID + "/services",
|
||||
})
|
||||
}
|
||||
|
||||
var middlewares []ResourceIdentifier
|
||||
for name := range provider.Middlewares {
|
||||
middlewares = append(middlewares, ResourceIdentifier{
|
||||
ID: name,
|
||||
Path: "/api/providers/" + providerID + "/middlewares",
|
||||
})
|
||||
}
|
||||
|
||||
providers := ProviderRepresentation{Routers: routers, Middlewares: middlewares, Services: services}
|
||||
|
||||
err := templateRenderer.JSON(rw, http.StatusOK, providers)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
log.FromContext(request.Context()).Error(err)
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (p Handler) getProviderHandler(response http.ResponseWriter, request *http.Request) {
|
||||
providerID := getProviderIDFromVars(mux.Vars(request))
|
||||
func (p Handler) getRoutersHandler(rw http.ResponseWriter, request *http.Request) {
|
||||
providerID := mux.Vars(request)["provider"]
|
||||
|
||||
currentConfigurations := p.CurrentConfigurations.Get().(types.Configurations)
|
||||
if provider, ok := currentConfigurations[providerID]; ok {
|
||||
err := templatesRenderer.JSON(response, http.StatusOK, provider)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
} else {
|
||||
http.NotFound(response, request)
|
||||
currentConfigurations := p.CurrentConfigurations.Get().(config.Configurations)
|
||||
|
||||
provider, ok := currentConfigurations[providerID]
|
||||
if !ok {
|
||||
http.NotFound(rw, request)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (p Handler) getBackendsHandler(response http.ResponseWriter, request *http.Request) {
|
||||
providerID := getProviderIDFromVars(mux.Vars(request))
|
||||
|
||||
currentConfigurations := p.CurrentConfigurations.Get().(types.Configurations)
|
||||
if provider, ok := currentConfigurations[providerID]; ok {
|
||||
err := templatesRenderer.JSON(response, http.StatusOK, provider.Backends)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
} else {
|
||||
http.NotFound(response, request)
|
||||
var routers []RouterRepresentation
|
||||
for name, router := range provider.Routers {
|
||||
routers = append(routers, RouterRepresentation{Router: router, ID: name})
|
||||
}
|
||||
}
|
||||
|
||||
func (p Handler) getBackendHandler(response http.ResponseWriter, request *http.Request) {
|
||||
vars := mux.Vars(request)
|
||||
providerID := getProviderIDFromVars(vars)
|
||||
backendID := vars["backend"]
|
||||
|
||||
currentConfigurations := p.CurrentConfigurations.Get().(types.Configurations)
|
||||
if provider, ok := currentConfigurations[providerID]; ok {
|
||||
if backend, ok := provider.Backends[backendID]; ok {
|
||||
err := templatesRenderer.JSON(response, http.StatusOK, backend)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
http.NotFound(response, request)
|
||||
}
|
||||
|
||||
func (p Handler) getServersHandler(response http.ResponseWriter, request *http.Request) {
|
||||
vars := mux.Vars(request)
|
||||
providerID := getProviderIDFromVars(vars)
|
||||
backendID := vars["backend"]
|
||||
|
||||
currentConfigurations := p.CurrentConfigurations.Get().(types.Configurations)
|
||||
if provider, ok := currentConfigurations[providerID]; ok {
|
||||
if backend, ok := provider.Backends[backendID]; ok {
|
||||
err := templatesRenderer.JSON(response, http.StatusOK, backend.Servers)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
http.NotFound(response, request)
|
||||
}
|
||||
|
||||
func (p Handler) getServerHandler(response http.ResponseWriter, request *http.Request) {
|
||||
vars := mux.Vars(request)
|
||||
providerID := getProviderIDFromVars(vars)
|
||||
backendID := vars["backend"]
|
||||
serverID := vars["server"]
|
||||
|
||||
currentConfigurations := p.CurrentConfigurations.Get().(types.Configurations)
|
||||
if provider, ok := currentConfigurations[providerID]; ok {
|
||||
if backend, ok := provider.Backends[backendID]; ok {
|
||||
if server, ok := backend.Servers[serverID]; ok {
|
||||
err := templatesRenderer.JSON(response, http.StatusOK, server)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
http.NotFound(response, request)
|
||||
}
|
||||
|
||||
func (p Handler) getFrontendsHandler(response http.ResponseWriter, request *http.Request) {
|
||||
providerID := getProviderIDFromVars(mux.Vars(request))
|
||||
|
||||
currentConfigurations := p.CurrentConfigurations.Get().(types.Configurations)
|
||||
if provider, ok := currentConfigurations[providerID]; ok {
|
||||
err := templatesRenderer.JSON(response, http.StatusOK, provider.Frontends)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
} else {
|
||||
http.NotFound(response, request)
|
||||
}
|
||||
}
|
||||
|
||||
func (p Handler) getFrontendHandler(response http.ResponseWriter, request *http.Request) {
|
||||
vars := mux.Vars(request)
|
||||
providerID := getProviderIDFromVars(vars)
|
||||
frontendID := vars["frontend"]
|
||||
|
||||
currentConfigurations := p.CurrentConfigurations.Get().(types.Configurations)
|
||||
if provider, ok := currentConfigurations[providerID]; ok {
|
||||
if frontend, ok := provider.Frontends[frontendID]; ok {
|
||||
err := templatesRenderer.JSON(response, http.StatusOK, frontend)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
http.NotFound(response, request)
|
||||
}
|
||||
|
||||
func (p Handler) getRoutesHandler(response http.ResponseWriter, request *http.Request) {
|
||||
vars := mux.Vars(request)
|
||||
providerID := getProviderIDFromVars(vars)
|
||||
frontendID := vars["frontend"]
|
||||
|
||||
currentConfigurations := p.CurrentConfigurations.Get().(types.Configurations)
|
||||
if provider, ok := currentConfigurations[providerID]; ok {
|
||||
if frontend, ok := provider.Frontends[frontendID]; ok {
|
||||
err := templatesRenderer.JSON(response, http.StatusOK, frontend.Routes)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
http.NotFound(response, request)
|
||||
}
|
||||
|
||||
func (p Handler) getRouteHandler(response http.ResponseWriter, request *http.Request) {
|
||||
vars := mux.Vars(request)
|
||||
providerID := getProviderIDFromVars(vars)
|
||||
frontendID := vars["frontend"]
|
||||
routeID := vars["route"]
|
||||
|
||||
currentConfigurations := p.CurrentConfigurations.Get().(types.Configurations)
|
||||
if provider, ok := currentConfigurations[providerID]; ok {
|
||||
if frontend, ok := provider.Frontends[frontendID]; ok {
|
||||
if route, ok := frontend.Routes[routeID]; ok {
|
||||
err := templatesRenderer.JSON(response, http.StatusOK, route)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
http.NotFound(response, request)
|
||||
}
|
||||
|
||||
// healthResponse combines data returned by thoas/stats with statistics (if
|
||||
// they are enabled).
|
||||
type healthResponse struct {
|
||||
*thoas_stats.Data
|
||||
*middlewares.Stats
|
||||
}
|
||||
|
||||
func (p *Handler) getHealthHandler(response http.ResponseWriter, request *http.Request) {
|
||||
health := &healthResponse{Data: p.Stats.Data()}
|
||||
if p.StatsRecorder != nil {
|
||||
health.Stats = p.StatsRecorder.Data()
|
||||
}
|
||||
err := templatesRenderer.JSON(response, http.StatusOK, health)
|
||||
err := templateRenderer.JSON(rw, http.StatusOK, routers)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
log.FromContext(request.Context()).Error(err)
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (p Handler) getRouterHandler(rw http.ResponseWriter, request *http.Request) {
|
||||
providerID := mux.Vars(request)["provider"]
|
||||
routerID := mux.Vars(request)["router"]
|
||||
|
||||
currentConfigurations := p.CurrentConfigurations.Get().(config.Configurations)
|
||||
|
||||
provider, ok := currentConfigurations[providerID]
|
||||
if !ok {
|
||||
http.NotFound(rw, request)
|
||||
return
|
||||
}
|
||||
|
||||
router, ok := provider.Routers[routerID]
|
||||
if !ok {
|
||||
http.NotFound(rw, request)
|
||||
return
|
||||
}
|
||||
|
||||
err := templateRenderer.JSON(rw, http.StatusOK, router)
|
||||
if err != nil {
|
||||
log.FromContext(request.Context()).Error(err)
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (p Handler) getMiddlewaresHandler(rw http.ResponseWriter, request *http.Request) {
|
||||
providerID := mux.Vars(request)["provider"]
|
||||
|
||||
currentConfigurations := p.CurrentConfigurations.Get().(config.Configurations)
|
||||
|
||||
provider, ok := currentConfigurations[providerID]
|
||||
if !ok {
|
||||
http.NotFound(rw, request)
|
||||
return
|
||||
}
|
||||
|
||||
var middlewares []MiddlewareRepresentation
|
||||
for name, middleware := range provider.Middlewares {
|
||||
middlewares = append(middlewares, MiddlewareRepresentation{Middleware: middleware, ID: name})
|
||||
}
|
||||
|
||||
err := templateRenderer.JSON(rw, http.StatusOK, middlewares)
|
||||
if err != nil {
|
||||
log.FromContext(request.Context()).Error(err)
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (p Handler) getMiddlewareHandler(rw http.ResponseWriter, request *http.Request) {
|
||||
providerID := mux.Vars(request)["provider"]
|
||||
middlewareID := mux.Vars(request)["middleware"]
|
||||
|
||||
currentConfigurations := p.CurrentConfigurations.Get().(config.Configurations)
|
||||
|
||||
provider, ok := currentConfigurations[providerID]
|
||||
if !ok {
|
||||
http.NotFound(rw, request)
|
||||
return
|
||||
}
|
||||
|
||||
middleware, ok := provider.Middlewares[middlewareID]
|
||||
if !ok {
|
||||
http.NotFound(rw, request)
|
||||
return
|
||||
}
|
||||
|
||||
err := templateRenderer.JSON(rw, http.StatusOK, middleware)
|
||||
if err != nil {
|
||||
log.FromContext(request.Context()).Error(err)
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (p Handler) getServicesHandler(rw http.ResponseWriter, request *http.Request) {
|
||||
providerID := mux.Vars(request)["provider"]
|
||||
|
||||
currentConfigurations := p.CurrentConfigurations.Get().(config.Configurations)
|
||||
|
||||
provider, ok := currentConfigurations[providerID]
|
||||
if !ok {
|
||||
http.NotFound(rw, request)
|
||||
return
|
||||
}
|
||||
|
||||
var services []ServiceRepresentation
|
||||
for name, service := range provider.Services {
|
||||
services = append(services, ServiceRepresentation{Service: service, ID: name})
|
||||
}
|
||||
|
||||
err := templateRenderer.JSON(rw, http.StatusOK, services)
|
||||
if err != nil {
|
||||
log.FromContext(request.Context()).Error(err)
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (p Handler) getServiceHandler(rw http.ResponseWriter, request *http.Request) {
|
||||
providerID := mux.Vars(request)["provider"]
|
||||
serviceID := mux.Vars(request)["service"]
|
||||
|
||||
currentConfigurations := p.CurrentConfigurations.Get().(config.Configurations)
|
||||
|
||||
provider, ok := currentConfigurations[providerID]
|
||||
if !ok {
|
||||
http.NotFound(rw, request)
|
||||
return
|
||||
}
|
||||
|
||||
service, ok := provider.Services[serviceID]
|
||||
if !ok {
|
||||
http.NotFound(rw, request)
|
||||
return
|
||||
}
|
||||
|
||||
err := templateRenderer.JSON(rw, http.StatusOK, service)
|
||||
if err != nil {
|
||||
log.FromContext(request.Context()).Error(err)
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
|
210
api/handler_test.go
Normal file
210
api/handler_test.go
Normal file
|
@ -0,0 +1,210 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/containous/mux"
|
||||
"github.com/containous/traefik/config"
|
||||
"github.com/containous/traefik/safe"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestHandler_Configuration(t *testing.T) {
|
||||
type expected struct {
|
||||
statusCode int
|
||||
body string
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
desc string
|
||||
path string
|
||||
configuration config.Configurations
|
||||
expected expected
|
||||
}{
|
||||
{
|
||||
desc: "Get all the providers",
|
||||
path: "/api/providers",
|
||||
configuration: config.Configurations{
|
||||
"foo": {
|
||||
Routers: map[string]*config.Router{
|
||||
"bar": {EntryPoints: []string{"foo", "bar"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: expected{statusCode: http.StatusOK, body: `[{"id":"foo","path":"/api/providers/foo"}]`},
|
||||
},
|
||||
{
|
||||
desc: "Get a provider",
|
||||
path: "/api/providers/foo",
|
||||
configuration: config.Configurations{
|
||||
"foo": {
|
||||
Routers: map[string]*config.Router{
|
||||
"bar": {EntryPoints: []string{"foo", "bar"}},
|
||||
},
|
||||
Middlewares: map[string]*config.Middleware{
|
||||
"bar": {
|
||||
AddPrefix: &config.AddPrefix{Prefix: "bar"},
|
||||
},
|
||||
},
|
||||
Services: map[string]*config.Service{
|
||||
"foo": {
|
||||
LoadBalancer: &config.LoadBalancerService{
|
||||
Method: "wrr",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: expected{statusCode: http.StatusOK, body: `{"routers":[{"id":"bar","path":"/api/providers/foo/routers"}],"middlewares":[{"id":"bar","path":"/api/providers/foo/middlewares"}],"services":[{"id":"foo","path":"/api/providers/foo/services"}]}`},
|
||||
},
|
||||
{
|
||||
desc: "Provider not found",
|
||||
path: "/api/providers/foo",
|
||||
configuration: config.Configurations{},
|
||||
expected: expected{statusCode: http.StatusNotFound, body: "404 page not found\n"},
|
||||
},
|
||||
{
|
||||
desc: "Get all routers",
|
||||
path: "/api/providers/foo/routers",
|
||||
configuration: config.Configurations{
|
||||
"foo": {
|
||||
Routers: map[string]*config.Router{
|
||||
"bar": {EntryPoints: []string{"foo", "bar"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: expected{statusCode: http.StatusOK, body: `[{"entryPoints":["foo","bar"],"id":"bar"}]`},
|
||||
},
|
||||
{
|
||||
desc: "Get a router",
|
||||
path: "/api/providers/foo/routers/bar",
|
||||
configuration: config.Configurations{
|
||||
"foo": {
|
||||
Routers: map[string]*config.Router{
|
||||
"bar": {EntryPoints: []string{"foo", "bar"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: expected{statusCode: http.StatusOK, body: `{"entryPoints":["foo","bar"]}`},
|
||||
},
|
||||
{
|
||||
desc: "Router not found",
|
||||
path: "/api/providers/foo/routers/bar",
|
||||
configuration: config.Configurations{
|
||||
"foo": {},
|
||||
},
|
||||
expected: expected{statusCode: http.StatusNotFound, body: "404 page not found\n"},
|
||||
},
|
||||
{
|
||||
desc: "Get all services",
|
||||
path: "/api/providers/foo/services",
|
||||
configuration: config.Configurations{
|
||||
"foo": {
|
||||
Services: map[string]*config.Service{
|
||||
"foo": {
|
||||
LoadBalancer: &config.LoadBalancerService{
|
||||
Method: "wrr",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: expected{statusCode: http.StatusOK, body: `[{"loadbalancer":{"method":"wrr","passHostHeader":false},"id":"foo"}]`},
|
||||
},
|
||||
{
|
||||
desc: "Get a service",
|
||||
path: "/api/providers/foo/services/foo",
|
||||
configuration: config.Configurations{
|
||||
"foo": {
|
||||
Services: map[string]*config.Service{
|
||||
"foo": {
|
||||
LoadBalancer: &config.LoadBalancerService{
|
||||
Method: "wrr",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: expected{statusCode: http.StatusOK, body: `{"loadbalancer":{"method":"wrr","passHostHeader":false}}`},
|
||||
},
|
||||
{
|
||||
desc: "Service not found",
|
||||
path: "/api/providers/foo/services/bar",
|
||||
configuration: config.Configurations{
|
||||
"foo": {},
|
||||
},
|
||||
expected: expected{statusCode: http.StatusNotFound, body: "404 page not found\n"},
|
||||
},
|
||||
{
|
||||
desc: "Get all middlewares",
|
||||
path: "/api/providers/foo/middlewares",
|
||||
configuration: config.Configurations{
|
||||
"foo": {
|
||||
Middlewares: map[string]*config.Middleware{
|
||||
"bar": {
|
||||
AddPrefix: &config.AddPrefix{Prefix: "bar"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: expected{statusCode: http.StatusOK, body: `[{"addPrefix":{"prefix":"bar"},"id":"bar"}]`},
|
||||
},
|
||||
{
|
||||
desc: "Get a middleware",
|
||||
path: "/api/providers/foo/middlewares/bar",
|
||||
configuration: config.Configurations{
|
||||
"foo": {
|
||||
Middlewares: map[string]*config.Middleware{
|
||||
"bar": {
|
||||
AddPrefix: &config.AddPrefix{Prefix: "bar"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: expected{statusCode: http.StatusOK, body: `{"addPrefix":{"prefix":"bar"}}`},
|
||||
},
|
||||
{
|
||||
desc: "Middleware not found",
|
||||
path: "/api/providers/foo/middlewares/bar",
|
||||
configuration: config.Configurations{
|
||||
"foo": {},
|
||||
},
|
||||
expected: expected{statusCode: http.StatusNotFound, body: "404 page not found\n"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
currentConfiguration := &safe.Safe{}
|
||||
currentConfiguration.Set(test.configuration)
|
||||
|
||||
handler := Handler{
|
||||
CurrentConfigurations: currentConfiguration,
|
||||
}
|
||||
|
||||
router := mux.NewRouter()
|
||||
handler.Append(router)
|
||||
|
||||
server := httptest.NewServer(router)
|
||||
|
||||
resp, err := http.DefaultClient.Get(server.URL + test.path)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, test.expected.statusCode, resp.StatusCode)
|
||||
|
||||
content, err := ioutil.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
err = resp.Body.Close()
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, test.expected.body, string(content))
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue