| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- package api
- import (
- "encoding/json"
- "net/http"
- "debtik/internal/vrrp"
- )
- func (s *Server) handleListVRRPInstances(w http.ResponseWriter, r *http.Request) {
- list := s.vrrpMgr.List()
- w.Header().Set("Content-Type", "application/json")
- _ = json.NewEncoder(w).Encode(list)
- }
- func (s *Server) handleSaveVRRPInstance(w http.ResponseWriter, r *http.Request) {
- var inst vrrp.VRRPInstance
- if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
- http.Error(w, "invalid request body", http.StatusBadRequest)
- return
- }
- saved, err := s.vrrpMgr.Save(inst)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- w.Header().Set("Content-Type", "application/json")
- _ = json.NewEncoder(w).Encode(saved)
- }
- func (s *Server) handleDeleteVRRPInstance(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- http.Error(w, "missing instance ID", http.StatusBadRequest)
- return
- }
- if err := s.vrrpMgr.Delete(id); err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- w.Header().Set("Content-Type", "application/json")
- _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
- }
- func (s *Server) handleToggleVRRPState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if id == "" {
- http.Error(w, "missing instance ID", http.StatusBadRequest)
- return
- }
- var req struct {
- Disabled bool `json:"disabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- http.Error(w, "invalid JSON", http.StatusBadRequest)
- return
- }
- if err := s.vrrpMgr.ToggleState(id, req.Disabled); err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- w.Header().Set("Content-Type", "application/json")
- _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
- }
- func (s *Server) handleGetVRRPSummary(w http.ResponseWriter, r *http.Request) {
- summary := s.vrrpMgr.GetSummary()
- w.Header().Set("Content-Type", "application/json")
- _ = json.NewEncoder(w).Encode(summary)
- }
|