2026-06-25 14:27:41 +07:00

53 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package api
import (
"net/http"
"github.com/pulse-lets-go/internal/models"
)
// handleGetNodes — GET /api/nodes — список всех нод с текущими score.
func (a *API) handleGetNodes(w http.ResponseWriter, r *http.Request) {
nodes := a.engine.GetAllNodes()
writeJSON(w, http.StatusOK, nodes)
}
// handleGetNodeMetrics — GET /api/nodes/{id}/metrics — история метрик ноды из ring buffer.
func (a *API) handleGetNodeMetrics(w http.ResponseWriter, r *http.Request) {
nodeID := r.PathValue("id")
if nodeID == "" {
writeError(w, http.StatusBadRequest, "не указан id ноды")
return
}
snapshots := a.engine.GetNodeHistory(nodeID)
writeJSON(w, http.StatusOK, map[string]interface{}{
"node_id": nodeID,
"snapshots": snapshots,
"count": len(snapshots),
})
}
// handleToggleNode — PUT /api/nodes/{id}/toggle — включение/выключение ноды.
func (a *API) handleToggleNode(w http.ResponseWriter, r *http.Request) {
nodeID := r.PathValue("id")
if nodeID == "" {
writeError(w, http.StatusBadRequest, "не указан id ноды")
return
}
var req models.ToggleNodeRequest
if err := decodeJSON(r, &req); err != nil {
writeError(w, http.StatusBadRequest, "некорректный JSON")
return
}
if err := a.engine.ToggleNode(nodeID, req.Disabled, req.Reason); err != nil {
writeError(w, http.StatusNotFound, err.Error())
return
}
info, _ := a.engine.GetNodeInfo(nodeID)
writeJSON(w, http.StatusOK, info)
}