forked from security/hakurei
cmd/pkgserver: satisfy handler signature in method
This is somewhat cleaner. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
@@ -27,8 +27,8 @@ var (
|
||||
infoPayloadOnce sync.Once
|
||||
)
|
||||
|
||||
// serveInfo returns constant system information.
|
||||
func serveInfo(w http.ResponseWriter, _ *http.Request) {
|
||||
// handleInfo writes constant system information.
|
||||
func handleInfo(w http.ResponseWriter, _ *http.Request) {
|
||||
infoPayloadOnce.Do(func() {
|
||||
infoPayload.Count = int(rosa.PresetUnexportedStart)
|
||||
infoPayload.HakureiVersion = info.Version()
|
||||
@@ -37,8 +37,7 @@ func serveInfo(w http.ResponseWriter, _ *http.Request) {
|
||||
writeAPIPayload(w, infoPayload)
|
||||
}
|
||||
|
||||
func (index *packageIndex) serveStatus() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
func (index *packageIndex) handleStatus(w http.ResponseWriter, r *http.Request) {
|
||||
download := path.Dir(r.URL.Path) == "/status"
|
||||
if index == nil {
|
||||
http.Error(w, "index is nil", http.StatusInternalServerError)
|
||||
@@ -81,10 +80,8 @@ func (index *packageIndex) serveStatus() http.HandlerFunc {
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (index *packageIndex) serveGet() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
func (index *packageIndex) handleGet(w http.ResponseWriter, r *http.Request) {
|
||||
q := r.URL.Query()
|
||||
limit, err := strconv.Atoi(q.Get("limit"))
|
||||
if err != nil || limit > 100 || limit < 1 {
|
||||
@@ -108,15 +105,14 @@ func (index *packageIndex) serveGet() http.HandlerFunc {
|
||||
Values []*metadata `json:"values"`
|
||||
}{len(values), values})
|
||||
}
|
||||
}
|
||||
|
||||
const ApiVersion = "v1"
|
||||
|
||||
func apiRoutes(mux *http.ServeMux, index *packageIndex) {
|
||||
mux.HandleFunc(fmt.Sprintf("GET /api/%s/info", ApiVersion), serveInfo)
|
||||
mux.HandleFunc(fmt.Sprintf("GET /api/%s/get", ApiVersion), index.serveGet())
|
||||
mux.HandleFunc(fmt.Sprintf("GET /api/%s/status/", ApiVersion), index.serveStatus())
|
||||
mux.HandleFunc("GET /status/", index.serveStatus())
|
||||
mux.HandleFunc(fmt.Sprintf("GET /api/%s/info", ApiVersion), handleInfo)
|
||||
mux.HandleFunc(fmt.Sprintf("GET /api/%s/get", ApiVersion), index.handleGet)
|
||||
mux.HandleFunc(fmt.Sprintf("GET /api/%s/status/", ApiVersion), index.handleStatus)
|
||||
mux.HandleFunc("GET /status/", index.handleStatus)
|
||||
}
|
||||
|
||||
// writeAPIPayload sets headers common to API responses and encodes payload as
|
||||
|
||||
@@ -18,7 +18,7 @@ func TestAPIInfo(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
serveInfo(w, httptest.NewRequestWithContext(
|
||||
handleInfo(w, httptest.NewRequestWithContext(
|
||||
t.Context(),
|
||||
http.MethodGet,
|
||||
prefix+"info",
|
||||
@@ -42,7 +42,7 @@ func TestAPIGet(t *testing.T) {
|
||||
index := newIndex(t)
|
||||
newRequest := func(suffix string) *httptest.ResponseRecorder {
|
||||
w := httptest.NewRecorder()
|
||||
index.serveGet()(w, httptest.NewRequestWithContext(
|
||||
index.handleGet(w, httptest.NewRequestWithContext(
|
||||
t.Context(),
|
||||
http.MethodGet,
|
||||
target+suffix,
|
||||
|
||||
Reference in New Issue
Block a user