diff --git a/bootstrap.go b/bootstrap.go new file mode 100644 index 0000000..d52c061 --- /dev/null +++ b/bootstrap.go @@ -0,0 +1,35 @@ +package main + +import ( + "encoding/json" + "fmt" + "io" + "net/http" +) + +func Bootstrap() { + // Connect to all peers' peers + peers := GetPeers() + for _, peer := range peers { + // Get the peer's peers + req, err := http.NewRequest(http.MethodGet, peer+"/peers", nil) + if err != nil { + panic(err) + } + resp, err := http.DefaultClient.Do(req) + if err != nil { + fmt.Println("Peer is down.") + continue + } + peerPeersBytes, err := io.ReadAll(resp.Body) + if err != nil { + panic(err) + } + var peerPeers []string + err = json.Unmarshal(peerPeersBytes, &peerPeers) + for _, peerPeer := range peerPeers { + // Add the peer's peers to the list of peers + AddPeer(peerPeer) + } + } +}