Skip to content

Commit f96dcbb

Browse files
committed
cleanup examples
1 parent fd4daf7 commit f96dcbb

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

examples/gateway/car/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ func main() {
3131
if err != nil {
3232
log.Fatal(err)
3333
}
34-
3534
handler := common.NewBlocksHandler(gwAPI, *portPtr)
3635

3736
// Initialize the public gateways that we will want to have available through

examples/gateway/common/blocks.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
uio "github.com/ipfs/go-unixfs/io"
2626
"github.com/ipfs/go-unixfsnode"
2727
iface "github.com/ipfs/interface-go-ipfs-core"
28+
nsopts "github.com/ipfs/interface-go-ipfs-core/options/namesys"
2829
ifacepath "github.com/ipfs/interface-go-ipfs-core/path"
2930
dagpb "github.com/ipld/go-codec-dagpb"
3031
"github.com/ipld/go-ipld-prime"
@@ -144,7 +145,15 @@ func (api *BlocksGateway) GetIPNSRecord(ctx context.Context, c cid.Cid) ([]byte,
144145
return nil, routing.ErrNotSupported
145146
}
146147

147-
func (api *BlocksGateway) GetDNSLinkRecord(ctx context.Context, host string) (ifacepath.Path, error) {
148+
func (api *BlocksGateway) GetDNSLinkRecord(ctx context.Context, hostname string) (ifacepath.Path, error) {
149+
if api.namesys != nil {
150+
p, err := api.namesys.Resolve(ctx, "/ipns/"+hostname, nsopts.Depth(1))
151+
if err == namesys.ErrResolveRecursion {
152+
err = nil
153+
}
154+
return ifacepath.New(p.String()), err
155+
}
156+
148157
return nil, errors.New("not implemented")
149158
}
150159

examples/gateway/proxy/main.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/ipfs/go-blockservice"
1010
offline "github.com/ipfs/go-ipfs-exchange-offline"
1111
"github.com/ipfs/go-libipfs/examples/gateway/common"
12+
"github.com/ipfs/go-libipfs/gateway"
1213
)
1314

1415
func main() {
@@ -24,16 +25,28 @@ func main() {
2425
routing := newProxyRouting(*gatewayUrlPtr, nil)
2526

2627
// Creates the gateway with the block service and the routing.
27-
gateway, err := common.NewBlocksGateway(blockService, routing)
28+
gwAPI, err := common.NewBlocksGateway(blockService, routing)
2829
if err != nil {
2930
log.Fatal(err)
3031
}
32+
handler := common.NewBlocksHandler(gwAPI, *portPtr)
33+
34+
// Initialize the public gateways that we will want to have available through
35+
// Host header rewritting. This step is optional and only required if you're
36+
// running multiple public gateways and want different settings and support
37+
// for DNSLink and Subdomain Gateways.
38+
publicGateways := map[string]*gateway.Specification{
39+
"localhost": {
40+
Paths: []string{"/ipfs", "/ipns"},
41+
NoDNSLink: true,
42+
UseSubdomains: true,
43+
},
44+
}
45+
noDNSLink := true
46+
handler = gateway.WithHostname(handler, gwAPI, publicGateways, noDNSLink)
3147

32-
handler := common.NewBlocksHandler(gateway, *portPtr)
33-
address := "127.0.0.1:" + strconv.Itoa(*portPtr)
34-
log.Printf("Listening on http://%s", address)
35-
36-
if err := http.ListenAndServe(address, handler); err != nil {
48+
log.Printf("Listening on http://localhost:%d", *portPtr)
49+
if err := http.ListenAndServe(":"+strconv.Itoa(*portPtr), handler); err != nil {
3750
log.Fatal(err)
3851
}
3952
}

gateway/gateway_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,18 @@ func (m *mockApi) GetIPNSRecord(context.Context, cid.Cid) ([]byte, error) {
8686
return nil, errors.New("not implemented")
8787
}
8888

89-
func (m *mockApi) IsCached(context.Context, ipath.Path) bool {
90-
return false
91-
}
92-
93-
func (m *mockApi) ResolvePath(context.Context, ipath.Path) (ipath.Resolved, error) {
94-
return nil, errors.New("not implemented")
95-
}
96-
9789
func (m *mockApi) GetDNSLinkRecord(ctx context.Context, hostname string) (ipath.Path, error) {
9890
p, err := m.ns.Resolve(ctx, "/ipns/"+hostname, nsopts.Depth(1))
9991
if err == namesys.ErrResolveRecursion {
10092
err = nil
10193
}
10294
return ipath.New(p.String()), err
10395
}
96+
97+
func (m *mockApi) IsCached(context.Context, ipath.Path) bool {
98+
return false
99+
}
100+
101+
func (m *mockApi) ResolvePath(context.Context, ipath.Path) (ipath.Resolved, error) {
102+
return nil, errors.New("not implemented")
103+
}

0 commit comments

Comments
 (0)