Improve API for the web UI

This commit is contained in:
Ludovic Fernandez 2019-09-02 11:38:04 +02:00 committed by Traefiker Bot
parent 17554202f6
commit af9762cf32
41 changed files with 1200 additions and 199 deletions

View file

@ -2,6 +2,7 @@ package api
import (
"encoding/json"
"fmt"
"net/http"
"sort"
"strconv"
@ -30,28 +31,31 @@ func (h Handler) getEntryPoints(rw http.ResponseWriter, request *http.Request) {
return results[i].Name < results[j].Name
})
rw.Header().Set("Content-Type", "application/json")
pageInfo, err := pagination(request, len(results))
if err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
writeError(rw, err.Error(), http.StatusBadRequest)
return
}
rw.Header().Set("Content-Type", "application/json")
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)
http.Error(rw, err.Error(), http.StatusInternalServerError)
writeError(rw, err.Error(), http.StatusInternalServerError)
}
}
func (h Handler) getEntryPoint(rw http.ResponseWriter, request *http.Request) {
entryPointID := mux.Vars(request)["entryPointID"]
rw.Header().Set("Content-Type", "application/json")
ep, ok := h.staticConfig.EntryPoints[entryPointID]
if !ok {
http.NotFound(rw, request)
writeError(rw, fmt.Sprintf("entry point not found: %s", entryPointID), http.StatusNotFound)
return
}
@ -60,11 +64,9 @@ func (h Handler) getEntryPoint(rw http.ResponseWriter, request *http.Request) {
Name: entryPointID,
}
rw.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(rw).Encode(result)
if err != nil {
log.FromContext(request.Context()).Error(err)
http.Error(rw, err.Error(), http.StatusInternalServerError)
writeError(rw, err.Error(), http.StatusInternalServerError)
}
}