dhcp_handlers.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "proxmox-router/internal/dhcp"
  6. )
  7. func (s *Server) handleListDHCPClients(w http.ResponseWriter, r *http.Request) {
  8. clients := s.dhcpMgr.ListClients()
  9. writeJSON(w, http.StatusOK, clients)
  10. }
  11. func (s *Server) handleGetDHCPSummary(w http.ResponseWriter, r *http.Request) {
  12. summary := s.dhcpMgr.GetSummary()
  13. writeJSON(w, http.StatusOK, summary)
  14. }
  15. func (s *Server) handleCreateDHCPClient(w http.ResponseWriter, r *http.Request) {
  16. var req dhcp.CreateDHCPClientRequest
  17. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  18. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  19. return
  20. }
  21. client, err := s.dhcpMgr.AddClient(req)
  22. if err != nil {
  23. writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
  24. return
  25. }
  26. writeJSON(w, http.StatusCreated, client)
  27. }
  28. func (s *Server) handleUpdateDHCPClient(w http.ResponseWriter, r *http.Request) {
  29. id := r.PathValue("id")
  30. if id == "" {
  31. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Client ID is required"})
  32. return
  33. }
  34. var req dhcp.UpdateDHCPClientRequest
  35. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  36. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  37. return
  38. }
  39. client, err := s.dhcpMgr.UpdateClient(id, req)
  40. if err != nil {
  41. writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
  42. return
  43. }
  44. writeJSON(w, http.StatusOK, client)
  45. }
  46. func (s *Server) handleDeleteDHCPClient(w http.ResponseWriter, r *http.Request) {
  47. id := r.PathValue("id")
  48. if id == "" {
  49. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Client ID is required"})
  50. return
  51. }
  52. if err := s.dhcpMgr.DeleteClient(id); err != nil {
  53. writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
  54. return
  55. }
  56. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  57. }
  58. func (s *Server) handleToggleDHCPClientState(w http.ResponseWriter, r *http.Request) {
  59. id := r.PathValue("id")
  60. if id == "" {
  61. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Client ID is required"})
  62. return
  63. }
  64. var payload struct {
  65. Enabled bool `json:"enabled"`
  66. }
  67. if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
  68. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  69. return
  70. }
  71. if err := s.dhcpMgr.ToggleState(id, payload.Enabled); err != nil {
  72. writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
  73. return
  74. }
  75. writeJSON(w, http.StatusOK, map[string]string{"status": "updated"})
  76. }
  77. func (s *Server) handleRenewDHCPClient(w http.ResponseWriter, r *http.Request) {
  78. id := r.PathValue("id")
  79. if id == "" {
  80. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Client ID is required"})
  81. return
  82. }
  83. if err := s.dhcpMgr.RenewLease(id); err != nil {
  84. writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
  85. return
  86. }
  87. writeJSON(w, http.StatusOK, map[string]string{"status": "renewed"})
  88. }
  89. func (s *Server) handleReleaseDHCPClient(w http.ResponseWriter, r *http.Request) {
  90. id := r.PathValue("id")
  91. if id == "" {
  92. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Client ID is required"})
  93. return
  94. }
  95. if err := s.dhcpMgr.ReleaseLease(id); err != nil {
  96. writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
  97. return
  98. }
  99. writeJSON(w, http.StatusOK, map[string]string{"status": "released"})
  100. }