netmaker/controllers/relay.go

63 lines
1.9 KiB
Go
Raw Normal View History

package controller
import (
"encoding/json"
"net/http"
2021-09-18 22:33:14 +08:00
"github.com/gorilla/mux"
2021-12-07 04:31:08 +08:00
"github.com/gravitl/netmaker/logger"
2021-10-27 00:27:29 +08:00
"github.com/gravitl/netmaker/logic"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/mq"
)
func createRelay(w http.ResponseWriter, r *http.Request) {
var relay models.RelayRequest
var params = mux.Vars(r)
w.Header().Set("Content-Type", "application/json")
err := json.NewDecoder(r.Body).Decode(&relay)
if err != nil {
returnErrorResponse(w, r, formatError(err, "internal"))
return
}
relay.NetID = params["network"]
2022-01-11 06:52:21 +08:00
relay.NodeID = params["nodeid"]
2022-02-08 00:30:15 +08:00
updatenodes, node, err := logic.CreateRelay(relay)
if err != nil {
returnErrorResponse(w, r, formatError(err, "internal"))
return
}
2021-12-07 04:31:08 +08:00
logger.Log(1, r.Header.Get("user"), "created relay on node", relay.NodeID, "on network", relay.NetID)
2022-02-08 00:30:15 +08:00
for _, relayedNode := range updatenodes {
err = mq.NodeUpdate(&relayedNode)
if err != nil {
2022-02-08 00:30:15 +08:00
logger.Log(1, "error sending update to relayed node ", relayedNode.Address, "on network", relay.NetID, ": ", err.Error())
}
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(node)
2022-02-17 09:42:57 +08:00
runUpdates(&node, true)
}
func deleteRelay(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var params = mux.Vars(r)
2022-01-11 06:52:21 +08:00
nodeid := params["nodeid"]
netid := params["network"]
2022-02-08 00:30:15 +08:00
updatenodes, node, err := logic.DeleteRelay(netid, nodeid)
if err != nil {
returnErrorResponse(w, r, formatError(err, "internal"))
return
}
2022-01-25 04:16:29 +08:00
logger.Log(1, r.Header.Get("user"), "deleted relay server", nodeid, "on network", netid)
2022-02-08 00:30:15 +08:00
for _, relayedNode := range updatenodes {
err = mq.NodeUpdate(&relayedNode)
if err != nil {
logger.Log(1, "error sending update to relayed node ", relayedNode.Address, "on network", netid, ": ", err.Error())
}
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(node)
2022-02-17 09:42:57 +08:00
runUpdates(&node, true)
}