traefik/pkg/api/handler_entrypoint.go

73 lines
1.8 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"
"sort"
"strconv"
2019-08-03 01:58:23 +00:00
"github.com/containous/traefik/v2/pkg/config/static"
"github.com/containous/traefik/v2/pkg/log"
"github.com/gorilla/mux"
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 {
log.FromContext(request.Context()).Error(err)
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) {
entryPointID := mux.Vars(request)["entryPointID"]
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)
if err != nil {
log.FromContext(request.Context()).Error(err)
2019-09-02 09:38:04 +00:00
writeError(rw, err.Error(), http.StatusInternalServerError)
2019-07-12 09:10:03 +00:00
}
}