gitlab.com/SiaPrime/SiaPrime@v1.4.1/node/api/gateway.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/julienschmidt/httprouter"
     8  
     9  	"gitlab.com/SiaPrime/SiaPrime/modules"
    10  )
    11  
    12  // GatewayGET contains the fields returned by a GET call to "/gateway".
    13  type GatewayGET struct {
    14  	NetAddress modules.NetAddress `json:"netaddress"`
    15  	Peers      []modules.Peer     `json:"peers"`
    16  
    17  	MaxDownloadSpeed int64 `json:"maxdownloadspeed"`
    18  	MaxUploadSpeed   int64 `json:"maxuploadspeed"`
    19  }
    20  
    21  // gatewayHandlerGET handles the API call asking for the gatway status.
    22  func (api *API) gatewayHandlerGET(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
    23  	peers := api.gateway.Peers()
    24  	mds, mus := api.gateway.RateLimits()
    25  	// nil slices are marshalled as 'null' in JSON, whereas 0-length slices are
    26  	// marshalled as '[]'. The latter is preferred, indicating that the value
    27  	// exists but contains no elements.
    28  	if peers == nil {
    29  		peers = make([]modules.Peer, 0)
    30  	}
    31  	WriteJSON(w, GatewayGET{api.gateway.Address(), peers, mds, mus})
    32  }
    33  
    34  // gatewayHandlerPOST handles the API call changing gateway specific settings.
    35  func (api *API) gatewayHandlerPOST(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
    36  	maxDownloadSpeed, maxUploadSpeed := api.gateway.RateLimits()
    37  	// Scan the download speed limit. (optional parameter)
    38  	if d := req.FormValue("maxdownloadspeed"); d != "" {
    39  		var downloadSpeed int64
    40  		if _, err := fmt.Sscan(d, &downloadSpeed); err != nil {
    41  			WriteError(w, Error{"unable to parse downloadspeed: " + err.Error()}, http.StatusBadRequest)
    42  			return
    43  		}
    44  		maxDownloadSpeed = downloadSpeed
    45  	}
    46  	// Scan the upload speed limit. (optional parameter)
    47  	if u := req.FormValue("maxuploadspeed"); u != "" {
    48  		var uploadSpeed int64
    49  		if _, err := fmt.Sscan(u, &uploadSpeed); err != nil {
    50  			WriteError(w, Error{"unable to parse uploadspeed: " + err.Error()}, http.StatusBadRequest)
    51  			return
    52  		}
    53  		maxUploadSpeed = uploadSpeed
    54  	}
    55  	// Try to set the limits.
    56  	err := api.gateway.SetRateLimits(maxDownloadSpeed, maxUploadSpeed)
    57  	if err != nil {
    58  		WriteError(w, Error{"failed to set new rate limit: " + err.Error()}, http.StatusBadRequest)
    59  		return
    60  	}
    61  	WriteSuccess(w)
    62  }
    63  
    64  // gatewayConnectHandler handles the API call to add a peer to the gateway.
    65  func (api *API) gatewayConnectHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
    66  	addr := modules.NetAddress(ps.ByName("netaddress"))
    67  	err := api.gateway.ConnectManual(addr)
    68  	if err != nil {
    69  		WriteError(w, Error{err.Error()}, http.StatusBadRequest)
    70  		return
    71  	}
    72  
    73  	WriteSuccess(w)
    74  }
    75  
    76  // gatewayDisconnectHandler handles the API call to remove a peer from the gateway.
    77  func (api *API) gatewayDisconnectHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
    78  	addr := modules.NetAddress(ps.ByName("netaddress"))
    79  	err := api.gateway.DisconnectManual(addr)
    80  	if err != nil {
    81  		WriteError(w, Error{err.Error()}, http.StatusBadRequest)
    82  		return
    83  	}
    84  
    85  	WriteSuccess(w)
    86  }