diff --git a/examples/gateway/car/main.go b/examples/gateway/car/main.go index d05fab8568..2c1918d720 100644 --- a/examples/gateway/car/main.go +++ b/examples/gateway/car/main.go @@ -31,7 +31,6 @@ func main() { if err != nil { log.Fatal(err) } - handler := common.NewBlocksHandler(gwAPI, *portPtr) // Initialize the public gateways that we will want to have available through diff --git a/examples/gateway/common/blocks.go b/examples/gateway/common/blocks.go index 0c78e88c13..a34acae01d 100644 --- a/examples/gateway/common/blocks.go +++ b/examples/gateway/common/blocks.go @@ -25,6 +25,7 @@ import ( uio "github.com/ipfs/go-unixfs/io" "github.com/ipfs/go-unixfsnode" iface "github.com/ipfs/interface-go-ipfs-core" + nsopts "github.com/ipfs/interface-go-ipfs-core/options/namesys" ifacepath "github.com/ipfs/interface-go-ipfs-core/path" dagpb "github.com/ipld/go-codec-dagpb" "github.com/ipld/go-ipld-prime" @@ -144,7 +145,15 @@ func (api *BlocksGateway) GetIPNSRecord(ctx context.Context, c cid.Cid) ([]byte, return nil, routing.ErrNotSupported } -func (api *BlocksGateway) GetDNSLinkRecord(ctx context.Context, host string) (ifacepath.Path, error) { +func (api *BlocksGateway) GetDNSLinkRecord(ctx context.Context, hostname string) (ifacepath.Path, error) { + if api.namesys != nil { + p, err := api.namesys.Resolve(ctx, "/ipns/"+hostname, nsopts.Depth(1)) + if err == namesys.ErrResolveRecursion { + err = nil + } + return ifacepath.New(p.String()), err + } + return nil, errors.New("not implemented") } diff --git a/examples/gateway/proxy/main.go b/examples/gateway/proxy/main.go index f4d8c0b7cc..5e99e61baa 100644 --- a/examples/gateway/proxy/main.go +++ b/examples/gateway/proxy/main.go @@ -9,6 +9,7 @@ import ( "github.com/ipfs/go-blockservice" offline "github.com/ipfs/go-ipfs-exchange-offline" "github.com/ipfs/go-libipfs/examples/gateway/common" + "github.com/ipfs/go-libipfs/gateway" ) func main() { @@ -24,16 +25,28 @@ func main() { routing := newProxyRouting(*gatewayUrlPtr, nil) // Creates the gateway with the block service and the routing. - gateway, err := common.NewBlocksGateway(blockService, routing) + gwAPI, err := common.NewBlocksGateway(blockService, routing) if err != nil { log.Fatal(err) } + handler := common.NewBlocksHandler(gwAPI, *portPtr) + + // Initialize the public gateways that we will want to have available through + // Host header rewritting. This step is optional and only required if you're + // running multiple public gateways and want different settings and support + // for DNSLink and Subdomain Gateways. + publicGateways := map[string]*gateway.Specification{ + "localhost": { + Paths: []string{"/ipfs", "/ipns"}, + NoDNSLink: true, + UseSubdomains: true, + }, + } + noDNSLink := true + handler = gateway.WithHostname(handler, gwAPI, publicGateways, noDNSLink) - handler := common.NewBlocksHandler(gateway, *portPtr) - address := "127.0.0.1:" + strconv.Itoa(*portPtr) - log.Printf("Listening on http://%s", address) - - if err := http.ListenAndServe(address, handler); err != nil { + log.Printf("Listening on http://localhost:%d", *portPtr) + if err := http.ListenAndServe(":"+strconv.Itoa(*portPtr), handler); err != nil { log.Fatal(err) } } diff --git a/gateway/gateway_test.go b/gateway/gateway_test.go index f449f81b89..f878cb631a 100644 --- a/gateway/gateway_test.go +++ b/gateway/gateway_test.go @@ -86,14 +86,6 @@ func (m *mockApi) GetIPNSRecord(context.Context, cid.Cid) ([]byte, error) { return nil, errors.New("not implemented") } -func (m *mockApi) IsCached(context.Context, ipath.Path) bool { - return false -} - -func (m *mockApi) ResolvePath(context.Context, ipath.Path) (ipath.Resolved, error) { - return nil, errors.New("not implemented") -} - func (m *mockApi) GetDNSLinkRecord(ctx context.Context, hostname string) (ipath.Path, error) { p, err := m.ns.Resolve(ctx, "/ipns/"+hostname, nsopts.Depth(1)) if err == namesys.ErrResolveRecursion { @@ -101,3 +93,11 @@ func (m *mockApi) GetDNSLinkRecord(ctx context.Context, hostname string) (ipath. } return ipath.New(p.String()), err } + +func (m *mockApi) IsCached(context.Context, ipath.Path) bool { + return false +} + +func (m *mockApi) ResolvePath(context.Context, ipath.Path) (ipath.Resolved, error) { + return nil, errors.New("not implemented") +}