handlers.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package ganserv
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "imuslab.com/zoraxy/mod/utils"
  6. )
  7. func (m *NetworkManager) HandleAddNetwork(w http.ResponseWriter, r *http.Request) {
  8. networkName, err := utils.PostPara(r, "name")
  9. if err != nil {
  10. utils.SendErrorResponse(w, "invalid or empty name given")
  11. return
  12. }
  13. // Generate a new UUID for this network
  14. networkUID := newNetworkID()
  15. // Create a new network object and add it to the list of networks
  16. newNetwork := &Network{
  17. UID: networkUID,
  18. Name: networkName,
  19. CIDR: "",
  20. Description: "",
  21. Nodes: []*Node{},
  22. }
  23. m.networks = append(m.networks, newNetwork)
  24. // Return the new network ID
  25. js, _ := json.Marshal(networkUID)
  26. utils.SendJSONResponse(w, string(js))
  27. }
  28. func (m *NetworkManager) HandleRemoveNetwork(w http.ResponseWriter, r *http.Request) {
  29. networkID, err := utils.PostPara(r, "id")
  30. if err != nil {
  31. utils.SendErrorResponse(w, "invalid or empty network id given")
  32. return
  33. }
  34. // Find the network with the given ID and remove it from the list of networks
  35. for i, network := range m.networks {
  36. if network.UID == networkID {
  37. m.networks = append(m.networks[:i], m.networks[i+1:]...)
  38. utils.SendOK(w)
  39. return
  40. }
  41. }
  42. // If the network is not found, return an error response
  43. utils.SendErrorResponse(w, "network not found")
  44. }
  45. func (m *NetworkManager) HandleListNetwork(w http.ResponseWriter, r *http.Request) {
  46. // Return the list of networks as JSON
  47. js, _ := json.Marshal(m.networks)
  48. utils.SendJSONResponse(w, string(js))
  49. }