vrrp_handlers.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "debtik/internal/vrrp"
  6. )
  7. func (s *Server) handleListVRRPInstances(w http.ResponseWriter, r *http.Request) {
  8. list := s.vrrpMgr.List()
  9. w.Header().Set("Content-Type", "application/json")
  10. _ = json.NewEncoder(w).Encode(list)
  11. }
  12. func (s *Server) handleSaveVRRPInstance(w http.ResponseWriter, r *http.Request) {
  13. var inst vrrp.VRRPInstance
  14. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  15. http.Error(w, "invalid request body", http.StatusBadRequest)
  16. return
  17. }
  18. saved, err := s.vrrpMgr.Save(inst)
  19. if err != nil {
  20. http.Error(w, err.Error(), http.StatusInternalServerError)
  21. return
  22. }
  23. w.Header().Set("Content-Type", "application/json")
  24. _ = json.NewEncoder(w).Encode(saved)
  25. }
  26. func (s *Server) handleDeleteVRRPInstance(w http.ResponseWriter, r *http.Request) {
  27. id := r.PathValue("id")
  28. if id == "" {
  29. http.Error(w, "missing instance ID", http.StatusBadRequest)
  30. return
  31. }
  32. if err := s.vrrpMgr.Delete(id); err != nil {
  33. http.Error(w, err.Error(), http.StatusInternalServerError)
  34. return
  35. }
  36. w.Header().Set("Content-Type", "application/json")
  37. _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
  38. }
  39. func (s *Server) handleToggleVRRPState(w http.ResponseWriter, r *http.Request) {
  40. id := r.PathValue("id")
  41. if id == "" {
  42. http.Error(w, "missing instance ID", http.StatusBadRequest)
  43. return
  44. }
  45. var req struct {
  46. Disabled bool `json:"disabled"`
  47. }
  48. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  49. http.Error(w, "invalid JSON", http.StatusBadRequest)
  50. return
  51. }
  52. if err := s.vrrpMgr.ToggleState(id, req.Disabled); err != nil {
  53. http.Error(w, err.Error(), http.StatusInternalServerError)
  54. return
  55. }
  56. w.Header().Set("Content-Type", "application/json")
  57. _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
  58. }
  59. func (s *Server) handleGetVRRPSummary(w http.ResponseWriter, r *http.Request) {
  60. summary := s.vrrpMgr.GetSummary()
  61. w.Header().Set("Content-Type", "application/json")
  62. _ = json.NewEncoder(w).Encode(summary)
  63. }