gitlab.com/jokerrs1/Sia@v1.3.2/node/api/gateway.go (about) 1 package api 2 3 import ( 4 "net/http" 5 6 "github.com/NebulousLabs/Sia/modules" 7 8 "github.com/julienschmidt/httprouter" 9 ) 10 11 // GatewayGET contains the fields returned by a GET call to "/gateway". 12 type GatewayGET struct { 13 NetAddress modules.NetAddress `json:"netaddress"` 14 Peers []modules.Peer `json:"peers"` 15 } 16 17 // gatewayHandler handles the API call asking for the gatway status. 18 func (api *API) gatewayHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) { 19 peers := api.gateway.Peers() 20 // nil slices are marshalled as 'null' in JSON, whereas 0-length slices are 21 // marshalled as '[]'. The latter is preferred, indicating that the value 22 // exists but contains no elements. 23 if peers == nil { 24 peers = make([]modules.Peer, 0) 25 } 26 WriteJSON(w, GatewayGET{api.gateway.Address(), peers}) 27 } 28 29 // gatewayConnectHandler handles the API call to add a peer to the gateway. 30 func (api *API) gatewayConnectHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { 31 addr := modules.NetAddress(ps.ByName("netaddress")) 32 err := api.gateway.Connect(addr) 33 if err != nil { 34 WriteError(w, Error{err.Error()}, http.StatusBadRequest) 35 return 36 } 37 38 WriteSuccess(w) 39 } 40 41 // gatewayDisconnectHandler handles the API call to remove a peer from the gateway. 42 func (api *API) gatewayDisconnectHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { 43 addr := modules.NetAddress(ps.ByName("netaddress")) 44 err := api.gateway.Disconnect(addr) 45 if err != nil { 46 WriteError(w, Error{err.Error()}, http.StatusBadRequest) 47 return 48 } 49 50 WriteSuccess(w) 51 }