Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(libp2phttp): don't require specific port for the HTTP host example #3047

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading