traefik/pkg/api/handler_http.go

266 lines
7.4 KiB
Go
Raw Permalink Normal View History

2019-07-12 09:10:03 +00:00
package api
import (
"encoding/json"
2019-09-02 09:38:04 +00:00
"fmt"
2019-07-12 09:10:03 +00:00
"net/http"
"net/url"
2019-07-12 09:10:03 +00:00
"strconv"
2019-09-02 09:38:04 +00:00
"strings"
2019-07-12 09:10:03 +00:00
2019-08-03 01:58:23 +00:00
"github.com/gorilla/mux"
2022-11-21 17:36:05 +00:00
"github.com/rs/zerolog/log"
2023-02-03 14:24:05 +00:00
"github.com/traefik/traefik/v3/pkg/config/runtime"
"github.com/traefik/traefik/v3/pkg/tls"
2019-07-12 09:10:03 +00:00
)
type routerRepresentation struct {
*runtime.RouterInfo
2019-07-12 09:10:03 +00:00
Name string `json:"name,omitempty"`
Provider string `json:"provider,omitempty"`
}
2019-09-02 09:38:04 +00:00
func newRouterRepresentation(name string, rt *runtime.RouterInfo) routerRepresentation {
if rt.TLS != nil && rt.TLS.Options == "" {
rt.TLS.Options = tls.DefaultTLSConfigName
}
2019-09-02 09:38:04 +00:00
return routerRepresentation{
RouterInfo: rt,
Name: name,
Provider: getProviderName(name),
}
}
2019-07-12 09:10:03 +00:00
type serviceRepresentation struct {
*runtime.ServiceInfo
2019-07-12 09:10:03 +00:00
ServerStatus map[string]string `json:"serverStatus,omitempty"`
Name string `json:"name,omitempty"`
Provider string `json:"provider,omitempty"`
2019-09-02 09:38:04 +00:00
Type string `json:"type,omitempty"`
}
func newServiceRepresentation(name string, si *runtime.ServiceInfo) serviceRepresentation {
return serviceRepresentation{
ServiceInfo: si,
Name: name,
Provider: getProviderName(name),
ServerStatus: si.GetAllStatus(),
Type: strings.ToLower(extractType(si.Service)),
}
2019-07-12 09:10:03 +00:00
}
type middlewareRepresentation struct {
*runtime.MiddlewareInfo
2019-07-12 09:10:03 +00:00
Name string `json:"name,omitempty"`
Provider string `json:"provider,omitempty"`
2019-09-02 09:38:04 +00:00
Type string `json:"type,omitempty"`
}
func newMiddlewareRepresentation(name string, mi *runtime.MiddlewareInfo) middlewareRepresentation {
return middlewareRepresentation{
MiddlewareInfo: mi,
Name: name,
Provider: getProviderName(name),
Type: strings.ToLower(extractType(mi.Middleware)),
}
2019-07-12 09:10:03 +00:00
}
func (h Handler) getRouters(rw http.ResponseWriter, request *http.Request) {
results := make([]routerRepresentation, 0, len(h.runtimeConfiguration.Routers))
query := request.URL.Query()
criterion := newSearchCriterion(query)
2019-09-02 09:38:04 +00:00
2019-07-12 09:10:03 +00:00
for name, rt := range h.runtimeConfiguration.Routers {
2019-09-02 09:38:04 +00:00
if keepRouter(name, rt, criterion) {
results = append(results, newRouterRepresentation(name, rt))
}
2019-07-12 09:10:03 +00:00
}
sortRouters(query, results)
2019-07-12 09:10:03 +00:00
2019-09-02 09:38:04 +00:00
rw.Header().Set("Content-Type", "application/json")
2019-07-12 09:10:03 +00:00
pageInfo, err := pagination(request, len(results))
if err != nil {
2019-09-02 09:38:04 +00:00
writeError(rw, err.Error(), http.StatusBadRequest)
2019-07-12 09:10:03 +00:00
return
}
rw.Header().Set(nextPageHeader, strconv.Itoa(pageInfo.nextPage))
err = json.NewEncoder(rw).Encode(results[pageInfo.startIndex:pageInfo.endIndex])
if err != nil {
2022-11-21 17:36:05 +00:00
log.Ctx(request.Context()).Error().Err(err).Send()
2019-09-02 09:38:04 +00:00
writeError(rw, err.Error(), http.StatusInternalServerError)
2019-07-12 09:10:03 +00:00
}
}
func (h Handler) getRouter(rw http.ResponseWriter, request *http.Request) {
scapedRouterID := mux.Vars(request)["routerID"]
routerID, err := url.PathUnescape(scapedRouterID)
if err != nil {
writeError(rw, fmt.Sprintf("unable to decode routerID %q: %s", scapedRouterID, err), http.StatusBadRequest)
return
}
2019-07-12 09:10:03 +00:00
2019-09-02 09:38:04 +00:00
rw.Header().Set("Content-Type", "application/json")
2019-07-12 09:10:03 +00:00
router, ok := h.runtimeConfiguration.Routers[routerID]
if !ok {
2019-09-02 09:38:04 +00:00
writeError(rw, fmt.Sprintf("router not found: %s", routerID), http.StatusNotFound)
2019-07-12 09:10:03 +00:00
return
}
2019-09-02 09:38:04 +00:00
result := newRouterRepresentation(routerID, router)
2019-07-12 09:10:03 +00:00
err = json.NewEncoder(rw).Encode(result)
2019-07-12 09:10:03 +00:00
if err != nil {
2022-11-21 17:36:05 +00:00
log.Ctx(request.Context()).Error().Err(err).Send()
2019-09-02 09:38:04 +00:00
writeError(rw, err.Error(), http.StatusInternalServerError)
2019-07-12 09:10:03 +00:00
}
}
func (h Handler) getServices(rw http.ResponseWriter, request *http.Request) {
results := make([]serviceRepresentation, 0, len(h.runtimeConfiguration.Services))
query := request.URL.Query()
criterion := newSearchCriterion(query)
2019-09-02 09:38:04 +00:00
2019-07-12 09:10:03 +00:00
for name, si := range h.runtimeConfiguration.Services {
2019-09-02 09:38:04 +00:00
if keepService(name, si, criterion) {
results = append(results, newServiceRepresentation(name, si))
}
2019-07-12 09:10:03 +00:00
}
sortServices(query, results)
2019-07-12 09:10:03 +00:00
2019-09-02 09:38:04 +00:00
rw.Header().Set("Content-Type", "application/json")
2019-07-12 09:10:03 +00:00
pageInfo, err := pagination(request, len(results))
if err != nil {
2019-09-02 09:38:04 +00:00
writeError(rw, err.Error(), http.StatusBadRequest)
2019-07-12 09:10:03 +00:00
return
}
rw.Header().Set(nextPageHeader, strconv.Itoa(pageInfo.nextPage))
err = json.NewEncoder(rw).Encode(results[pageInfo.startIndex:pageInfo.endIndex])
if err != nil {
2022-11-21 17:36:05 +00:00
log.Ctx(request.Context()).Error().Err(err).Send()
2019-09-02 09:38:04 +00:00
writeError(rw, err.Error(), http.StatusInternalServerError)
2019-07-12 09:10:03 +00:00
}
}
func (h Handler) getService(rw http.ResponseWriter, request *http.Request) {
scapedServiceID := mux.Vars(request)["serviceID"]
serviceID, err := url.PathUnescape(scapedServiceID)
if err != nil {
writeError(rw, fmt.Sprintf("unable to decode serviceID %q: %s", scapedServiceID, err), http.StatusBadRequest)
return
}
2019-07-12 09:10:03 +00:00
2019-09-02 09:38:04 +00:00
rw.Header().Add("Content-Type", "application/json")
2019-07-12 09:10:03 +00:00
service, ok := h.runtimeConfiguration.Services[serviceID]
if !ok {
2019-09-02 09:38:04 +00:00
writeError(rw, fmt.Sprintf("service not found: %s", serviceID), http.StatusNotFound)
2019-07-12 09:10:03 +00:00
return
}
2019-09-02 09:38:04 +00:00
result := newServiceRepresentation(serviceID, service)
2019-07-12 09:10:03 +00:00
err = json.NewEncoder(rw).Encode(result)
2019-07-12 09:10:03 +00:00
if err != nil {
2022-11-21 17:36:05 +00:00
log.Ctx(request.Context()).Error().Err(err).Send()
2019-09-02 09:38:04 +00:00
writeError(rw, err.Error(), http.StatusInternalServerError)
2019-07-12 09:10:03 +00:00
}
}
func (h Handler) getMiddlewares(rw http.ResponseWriter, request *http.Request) {
results := make([]middlewareRepresentation, 0, len(h.runtimeConfiguration.Middlewares))
query := request.URL.Query()
criterion := newSearchCriterion(query)
2019-09-02 09:38:04 +00:00
2019-07-12 09:10:03 +00:00
for name, mi := range h.runtimeConfiguration.Middlewares {
2019-09-02 09:38:04 +00:00
if keepMiddleware(name, mi, criterion) {
results = append(results, newMiddlewareRepresentation(name, mi))
}
2019-07-12 09:10:03 +00:00
}
sortMiddlewares(query, results)
2019-07-12 09:10:03 +00:00
2019-09-02 09:38:04 +00:00
rw.Header().Set("Content-Type", "application/json")
2019-07-12 09:10:03 +00:00
pageInfo, err := pagination(request, len(results))
if err != nil {
2019-09-02 09:38:04 +00:00
writeError(rw, err.Error(), http.StatusBadRequest)
2019-07-12 09:10:03 +00:00
return
}
rw.Header().Set(nextPageHeader, strconv.Itoa(pageInfo.nextPage))
err = json.NewEncoder(rw).Encode(results[pageInfo.startIndex:pageInfo.endIndex])
if err != nil {
2022-11-21 17:36:05 +00:00
log.Ctx(request.Context()).Error().Err(err).Send()
2019-09-02 09:38:04 +00:00
writeError(rw, err.Error(), http.StatusInternalServerError)
2019-07-12 09:10:03 +00:00
}
}
func (h Handler) getMiddleware(rw http.ResponseWriter, request *http.Request) {
scapedMiddlewareID := mux.Vars(request)["middlewareID"]
middlewareID, err := url.PathUnescape(scapedMiddlewareID)
if err != nil {
writeError(rw, fmt.Sprintf("unable to decode middlewareID %q: %s", scapedMiddlewareID, err), http.StatusBadRequest)
return
}
2019-07-12 09:10:03 +00:00
2019-09-02 09:38:04 +00:00
rw.Header().Set("Content-Type", "application/json")
2019-07-12 09:10:03 +00:00
middleware, ok := h.runtimeConfiguration.Middlewares[middlewareID]
if !ok {
2019-09-02 09:38:04 +00:00
writeError(rw, fmt.Sprintf("middleware not found: %s", middlewareID), http.StatusNotFound)
2019-07-12 09:10:03 +00:00
return
}
2019-09-02 09:38:04 +00:00
result := newMiddlewareRepresentation(middlewareID, middleware)
2019-07-12 09:10:03 +00:00
err = json.NewEncoder(rw).Encode(result)
2019-07-12 09:10:03 +00:00
if err != nil {
2022-11-21 17:36:05 +00:00
log.Ctx(request.Context()).Error().Err(err).Send()
2019-09-02 09:38:04 +00:00
writeError(rw, err.Error(), http.StatusInternalServerError)
2019-07-12 09:10:03 +00:00
}
}
2019-09-02 09:38:04 +00:00
func keepRouter(name string, item *runtime.RouterInfo, criterion *searchCriterion) bool {
if criterion == nil {
return true
}
return criterion.withStatus(item.Status) &&
criterion.searchIn(item.Rule, name) &&
criterion.filterService(item.Service) &&
criterion.filterMiddleware(item.Middlewares)
2019-09-02 09:38:04 +00:00
}
func keepService(name string, item *runtime.ServiceInfo, criterion *searchCriterion) bool {
if criterion == nil {
return true
}
return criterion.withStatus(item.Status) && criterion.searchIn(name)
}
func keepMiddleware(name string, item *runtime.MiddlewareInfo, criterion *searchCriterion) bool {
if criterion == nil {
return true
}
return criterion.withStatus(item.Status) && criterion.searchIn(name)
}