github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/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 // TODO: GatewayInfo is not the right name for this struct. 12 type GatewayInfo 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 (srv *Server) gatewayHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) { 19 peers := srv.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, GatewayInfo{srv.gateway.Address(), peers}) 27 } 28 29 // gatewayAddHandler handles the API call to add a peer to the gateway. 30 func (srv *Server) gatewayAddHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { 31 addr := modules.NetAddress(ps.ByName("netaddress")) 32 err := srv.gateway.Connect(addr) 33 if err != nil { 34 writeError(w, err.Error(), http.StatusBadRequest) 35 return 36 } 37 38 writeSuccess(w) 39 } 40 41 // gatewayRemoveHandler handles the API call to remove a peer from the gateway. 42 func (srv *Server) gatewayRemoveHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { 43 addr := modules.NetAddress(ps.ByName("netaddress")) 44 err := srv.gateway.Disconnect(addr) 45 if err != nil { 46 writeError(w, err.Error(), http.StatusBadRequest) 47 return 48 } 49 50 writeSuccess(w) 51 }