1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package ganserv
- import (
- "encoding/json"
- "net/http"
- "imuslab.com/zoraxy/mod/utils"
- )
- func (m *NetworkManager) HandleAddNetwork(w http.ResponseWriter, r *http.Request) {
- networkName, err := utils.PostPara(r, "name")
- if err != nil {
- utils.SendErrorResponse(w, "invalid or empty name given")
- return
- }
- // Generate a new UUID for this network
- networkUID := newNetworkID()
- // Create a new network object and add it to the list of networks
- newNetwork := &Network{
- UID: networkUID,
- Name: networkName,
- CIDR: "",
- Description: "",
- Nodes: []*Node{},
- }
- m.networks = append(m.networks, newNetwork)
- // Return the new network ID
- js, _ := json.Marshal(networkUID)
- utils.SendJSONResponse(w, string(js))
- }
- func (m *NetworkManager) HandleRemoveNetwork(w http.ResponseWriter, r *http.Request) {
- networkID, err := utils.PostPara(r, "id")
- if err != nil {
- utils.SendErrorResponse(w, "invalid or empty network id given")
- return
- }
- // Find the network with the given ID and remove it from the list of networks
- for i, network := range m.networks {
- if network.UID == networkID {
- m.networks = append(m.networks[:i], m.networks[i+1:]...)
- utils.SendOK(w)
- return
- }
- }
- // If the network is not found, return an error response
- utils.SendErrorResponse(w, "network not found")
- }
- func (m *NetworkManager) HandleListNetwork(w http.ResponseWriter, r *http.Request) {
- // Return the list of networks as JSON
- js, _ := json.Marshal(m.networks)
- utils.SendJSONResponse(w, string(js))
- }
|