Skip to content

Commit

Permalink
cleanup examples
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 6, 2023
1 parent fd4daf7 commit f96dcbb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
1 change: 0 additions & 1 deletion examples/gateway/car/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion examples/gateway/common/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}

Expand Down
25 changes: 19 additions & 6 deletions examples/gateway/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}
}
16 changes: 8 additions & 8 deletions gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,18 @@ 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 {
err = nil
}
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")
}

0 comments on commit f96dcbb

Please sign in to comment.