Skip to content

Commit

Permalink
refactor(libp2phttp): don't require specific port for the HTTP host e…
Browse files Browse the repository at this point in the history
…xample (#3047)
  • Loading branch information
MarcoPolo committed Nov 19, 2024
1 parent ae0d78a commit 0606295
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions p2p/http/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net"
"net/http"
"regexp"
"strings"

"github.com/libp2p/go-libp2p"
Expand Down Expand Up @@ -125,18 +126,24 @@ func ExampleHost_overLibp2pStreams() {
// Output: Hello HTTP
}

var tcpPortRE = regexp.MustCompile(`/tcp/(\d+)`)

func ExampleHost_Serve() {
server := libp2phttp.Host{
InsecureAllowHTTP: true, // For our example, we'll allow insecure HTTP
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50221/http")},
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/0/http")},
}

go server.Serve()
defer server.Close()

fmt.Println(server.Addrs())
for _, a := range server.Addrs() {
s := a.String()
addrWithoutSpecificPort := tcpPortRE.ReplaceAllString(s, "/tcp/<runtime-port>")
fmt.Println(addrWithoutSpecificPort)
}

// Output: [/ip4/127.0.0.1/tcp/50221/http]
// Output: /ip4/127.0.0.1/tcp/<runtime-port>/http
}

func ExampleHost_SetHTTPHandler() {
Expand Down

0 comments on commit 0606295

Please sign in to comment.