traefik/pkg/api/handler_entrypoint.go

80 lines
2 KiB
Go
Raw 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
"sort"
"strconv"
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/static"
2019-07-12 09:10:03 +00:00
)
type entryPointRepresentation struct {
*static.EntryPoint
Name string `json:"name,omitempty"`
}
func (h Handler) getEntryPoints(rw http.ResponseWriter, request *http.Request) {
results := make([]entryPointRepresentation, 0, len(h.staticConfig.EntryPoints))
for name, ep := range h.staticConfig.EntryPoints {
results = append(results, entryPointRepresentation{
EntryPoint: ep,
Name: name,
})
}
sort.Slice(results, func(i, j int) bool {
return results[i].Name < results[j].Name
})
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) getEntryPoint(rw http.ResponseWriter, request *http.Request) {
scapedEntryPointID := mux.Vars(request)["entryPointID"]
entryPointID, err := url.PathUnescape(scapedEntryPointID)
if err != nil {
writeError(rw, fmt.Sprintf("unable to decode entryPointID %q: %s", scapedEntryPointID, 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
ep, ok := h.staticConfig.EntryPoints[entryPointID]
if !ok {
2019-09-02 09:38:04 +00:00
writeError(rw, fmt.Sprintf("entry point not found: %s", entryPointID), http.StatusNotFound)
2019-07-12 09:10:03 +00:00
return
}
result := entryPointRepresentation{
EntryPoint: ep,
Name: entryPointID,
}
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
}
}