package api import ( "encoding/json" "net/http" "proxmox-router/internal/dhcp" ) func (s *Server) handleGetDHCPServerSummary(w http.ResponseWriter, r *http.Request) { summary := s.dhcpServerMgr.GetSummary() w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(summary) } func (s *Server) handleListDHCPServers(w http.ResponseWriter, r *http.Request) { servers := s.dhcpServerMgr.ListServers() w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(servers) } func (s *Server) handleSaveDHCPServer(w http.ResponseWriter, r *http.Request) { var inst dhcp.DHCPServerInstance if err := json.NewDecoder(r.Body).Decode(&inst); err != nil { http.Error(w, "invalid request body", http.StatusBadRequest) return } saved, err := s.dhcpServerMgr.SaveServer(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) handleDeleteDHCPServer(w http.ResponseWriter, r *http.Request) { id := r.PathValue("id") if id == "" { http.Error(w, "missing server ID", http.StatusBadRequest) return } if err := s.dhcpServerMgr.DeleteServer(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) handleSetDHCPServerState(w http.ResponseWriter, r *http.Request) { id := r.PathValue("id") if id == "" { http.Error(w, "missing server 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.dhcpServerMgr.ToggleServerState(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}) } // ======================== NETWORKS ======================== func (s *Server) handleListDHCPNetworks(w http.ResponseWriter, r *http.Request) { networks := s.dhcpServerMgr.ListNetworks() w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(networks) } func (s *Server) handleSaveDHCPNetwork(w http.ResponseWriter, r *http.Request) { var netw dhcp.DHCPNetwork if err := json.NewDecoder(r.Body).Decode(&netw); err != nil { http.Error(w, "invalid request body", http.StatusBadRequest) return } saved, err := s.dhcpServerMgr.SaveNetwork(netw) 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) handleDeleteDHCPNetwork(w http.ResponseWriter, r *http.Request) { id := r.PathValue("id") if id == "" { http.Error(w, "missing network ID", http.StatusBadRequest) return } if err := s.dhcpServerMgr.DeleteNetwork(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}) } // ======================== LEASES ======================== func (s *Server) handleListDHCPLeases(w http.ResponseWriter, r *http.Request) { leases := s.dhcpServerMgr.ListLeases() w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(leases) } func (s *Server) handleSaveDHCPStaticLease(w http.ResponseWriter, r *http.Request) { var lease dhcp.DHCPLease if err := json.NewDecoder(r.Body).Decode(&lease); err != nil { http.Error(w, "invalid request body", http.StatusBadRequest) return } saved, err := s.dhcpServerMgr.SaveStaticLease(lease) 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) handleDeleteDHCPStaticLease(w http.ResponseWriter, r *http.Request) { id := r.PathValue("id") if id == "" { http.Error(w, "missing lease ID", http.StatusBadRequest) return } if err := s.dhcpServerMgr.DeleteStaticLease(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) handleMakeDHCPStaticLease(w http.ResponseWriter, r *http.Request) { var req struct { MACAddress string `json:"mac_address"` Address string `json:"address"` HostName string `json:"hostname"` Comment string `json:"comment"` } if err := json.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, "invalid JSON", http.StatusBadRequest) return } st, err := s.dhcpServerMgr.MakeStaticLease(req.MACAddress, req.Address, req.HostName, req.Comment) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(st) } // ======================== OPTIONS ======================== func (s *Server) handleListDHCPOptions(w http.ResponseWriter, r *http.Request) { options := s.dhcpServerMgr.ListOptions() w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(options) } func (s *Server) handleSaveDHCPOption(w http.ResponseWriter, r *http.Request) { var opt dhcp.DHCPOption if err := json.NewDecoder(r.Body).Decode(&opt); err != nil { http.Error(w, "invalid request body", http.StatusBadRequest) return } saved, err := s.dhcpServerMgr.SaveOption(opt) 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) handleDeleteDHCPOption(w http.ResponseWriter, r *http.Request) { id := r.PathValue("id") if id == "" { http.Error(w, "missing option ID", http.StatusBadRequest) return } if err := s.dhcpServerMgr.DeleteOption(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}) } // ======================== WIZARD ======================== func (s *Server) handleRunDHCPSetup(w http.ResponseWriter, r *http.Request) { var req dhcp.DHCPServerSetupRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, "invalid request body", http.StatusBadRequest) return } if err := s.dhcpServerMgr.RunSetupWizard(req); 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}) }