1
0
mirror of https://github.com/containous/traefik.git synced 2024-12-22 13:34:03 +03:00
traefik/pkg/api/handler_entrypoint.go

80 lines
2.0 KiB
Go
Raw Normal View History

2019-07-12 12:10:03 +03:00
package api
import (
"encoding/json"
2019-09-02 12:38:04 +03:00
"fmt"
2019-07-12 12:10:03 +03:00
"net/http"
"net/url"
2019-07-12 12:10:03 +03:00
"sort"
"strconv"
2019-08-03 04:58:23 +03:00
"github.com/gorilla/mux"
"github.com/traefik/traefik/v2/pkg/config/static"
"github.com/traefik/traefik/v2/pkg/log"
2019-07-12 12:10:03 +03: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 12:38:04 +03:00
rw.Header().Set("Content-Type", "application/json")
2019-07-12 12:10:03 +03:00
pageInfo, err := pagination(request, len(results))
if err != nil {
2019-09-02 12:38:04 +03:00
writeError(rw, err.Error(), http.StatusBadRequest)
2019-07-12 12:10:03 +03:00
return
}
rw.Header().Set(nextPageHeader, strconv.Itoa(pageInfo.nextPage))
err = json.NewEncoder(rw).Encode(results[pageInfo.startIndex:pageInfo.endIndex])
if err != nil {
log.FromContext(request.Context()).Error(err)
2019-09-02 12:38:04 +03:00
writeError(rw, err.Error(), http.StatusInternalServerError)
2019-07-12 12:10:03 +03: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 12:10:03 +03:00
2019-09-02 12:38:04 +03:00
rw.Header().Set("Content-Type", "application/json")
2019-07-12 12:10:03 +03:00
ep, ok := h.staticConfig.EntryPoints[entryPointID]
if !ok {
2019-09-02 12:38:04 +03:00
writeError(rw, fmt.Sprintf("entry point not found: %s", entryPointID), http.StatusNotFound)
2019-07-12 12:10:03 +03:00
return
}
result := entryPointRepresentation{
EntryPoint: ep,
Name: entryPointID,
}
err = json.NewEncoder(rw).Encode(result)
2019-07-12 12:10:03 +03:00
if err != nil {
log.FromContext(request.Context()).Error(err)
2019-09-02 12:38:04 +03:00
writeError(rw, err.Error(), http.StatusInternalServerError)
2019-07-12 12:10:03 +03:00
}
}