Skip to content

Commit

Permalink
Add CustomRootHandler option
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Aug 8, 2023
1 parent 06a9aad commit d723568
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion p2p/http/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package libp2phttp
import (
"crypto/tls"
"fmt"
"net/http"

"github.com/libp2p/go-libp2p/core/host"
ma "github.com/multiformats/go-multiaddr"
)

type HTTPHostOption func(*HTTPHost) error

// WithStreamHost sets the stream host to use for HTTP over libp2p streams.
// ListenAddrs sets the listen addresses for the HTTP transport.
func ListenAddrs(addrs []ma.Multiaddr) HTTPHostOption {
return func(h *HTTPHost) error {
// assert that each addr contains a /http component
Expand All @@ -26,6 +27,7 @@ func ListenAddrs(addrs []ma.Multiaddr) HTTPHostOption {
}
}

// TLSConfig sets the server TLS config for the HTTP transport.
func TLSConfig(tlsConfig *tls.Config) HTTPHostOption {
return func(h *HTTPHost) error {
h.httpTransport.tlsConfig = tlsConfig
Expand All @@ -48,3 +50,14 @@ func WithTLSClientConfig(tlsConfig *tls.Config) HTTPHostOption {
return nil
}
}

// CustomRootHandler sets the root handler for the HTTP transport. It *does not*
// set up a .well-known/libp2p handler. Users assume the responsibility of
// handling .well-known/libp2p, as well as keeping it up to date.
func CustomRootHandler(mux http.Handler) HTTPHostOption {
return func(h *HTTPHost) error {
h.rootHandler = http.ServeMux{}
h.rootHandler.Handle("/", mux)
return nil
}
}

0 comments on commit d723568

Please sign in to comment.