From d723568f2d0d4397e0d7459d7666492e0aeac625 Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Tue, 8 Aug 2023 15:31:57 -0700 Subject: [PATCH] Add CustomRootHandler option --- p2p/http/options.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/p2p/http/options.go b/p2p/http/options.go index a1bd9e27b4..2e095d1b86 100644 --- a/p2p/http/options.go +++ b/p2p/http/options.go @@ -3,6 +3,7 @@ package libp2phttp import ( "crypto/tls" "fmt" + "net/http" "github.com/libp2p/go-libp2p/core/host" ma "github.com/multiformats/go-multiaddr" @@ -10,7 +11,7 @@ import ( 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 @@ -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 @@ -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 + } +}