Skip to content

Commit

Permalink
Change response of '/nodes/{all,enabled}' HTTP API routes.
Browse files Browse the repository at this point in the history
Now responses include 'specific' nodes.
  • Loading branch information
nickeskov committed Oct 4, 2024
1 parent b2192fe commit 75f1db1
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,26 +263,41 @@ func cleanCRLF(s string) string {
return cleaned
}

type nodesResponse struct {
Regular proto.NonNullableSlice[entities.Node] `json:"regular"`
Specific proto.NonNullableSlice[entities.Node] `json:"specific"`
}

func (a *API) nodes(w http.ResponseWriter, _ *http.Request) {
regularNodes, err := a.nodesStorage.Nodes(false)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to complete request: %v", err), http.StatusInternalServerError)
return
}
err = json.NewEncoder(w).Encode(regularNodes)
specificNodes, err := a.nodesStorage.Nodes(true)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to complete request: %v", err), http.StatusInternalServerError)
return
}
err = json.NewEncoder(w).Encode(nodesResponse{Regular: regularNodes, Specific: specificNodes})
if err != nil {
http.Error(w, fmt.Sprintf("Failed to marshal nodes to JSON: %v", err), http.StatusInternalServerError)
return
}
}

func (a *API) enabled(w http.ResponseWriter, _ *http.Request) {
enabledNodes, err := a.nodesStorage.EnabledNodes()
enabledRegularNodes, err := a.nodesStorage.EnabledNodes()
if err != nil {
http.Error(w, fmt.Sprintf("Failed to complete request: %v", err), http.StatusInternalServerError)
return
}
enabledSpecificNodes, err := a.nodesStorage.EnabledSpecificNodes()
if err != nil {
http.Error(w, fmt.Sprintf("Failed to complete request: %v", err), http.StatusInternalServerError)
return
}
err = json.NewEncoder(w).Encode(enabledNodes)
err = json.NewEncoder(w).Encode(nodesResponse{Regular: enabledRegularNodes, Specific: enabledSpecificNodes})
if err != nil {
http.Error(w, fmt.Sprintf("Failed to marshal nodes to JSON: %v", err), http.StatusInternalServerError)
return
Expand Down

0 comments on commit 75f1db1

Please sign in to comment.