| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- package api
- import (
- "encoding/json"
- "net/http"
- "debtik/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})
- }
|