github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/publicapi/endpoint_peers.go (about)

     1  package publicapi
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  
     7  	"github.com/libp2p/go-libp2p/core/peer"
     8  )
     9  
    10  // peers godoc
    11  //
    12  //	@ID						peers
    13  //	@Summary				Returns the peers connected to the host via the transport layer.
    14  //	@Description.markdown	endpoints_peers
    15  //	@Tags					Utils
    16  //	@Produce				json
    17  //	@Success				200	{object}	[]peer.AddrInfo
    18  //	@Failure				500	{object}	string
    19  //	@Router					/peers [get]
    20  func (apiServer *APIServer) peers(res http.ResponseWriter, _ *http.Request) {
    21  	res.WriteHeader(http.StatusOK)
    22  	var peerInfos []peer.AddrInfo
    23  	for _, p := range apiServer.host.Peerstore().Peers() {
    24  		peerInfos = append(peerInfos, apiServer.host.Peerstore().PeerInfo(p))
    25  	}
    26  	err := json.NewEncoder(res).Encode(peerInfos)
    27  	if err != nil {
    28  		http.Error(res, err.Error(), http.StatusInternalServerError)
    29  		return
    30  	}
    31  }