Skip to content

Commit

Permalink
docs: address feedback on examples
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 3, 2023
1 parent 613d25f commit d73cc9a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
11 changes: 7 additions & 4 deletions examples/gateway/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,17 @@ func (api *BlocksGateway) ResolvePath(ctx context.Context, p ifacepath.Path) (if
return p.(ifacepath.Resolved), nil
}

if err := p.IsValid(); err != nil {
err := p.IsValid()
if err != nil {
return nil, err
}

ipath := ipfspath.Path(p.String())
ipath, err := resolve.ResolveIPNS(ctx, api.namesys, ipath)
if err != nil {
return nil, err
if ipath.Segments()[0] == "ipns" {
ipath, err = resolve.ResolveIPNS(ctx, api.namesys, ipath)
if err != nil {
return nil, err
}
}

if ipath.Segments()[0] != "ipfs" {
Expand Down
2 changes: 1 addition & 1 deletion examples/gateway/proxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ types. Once you have it, run the proxy gateway with its address as the host para


```
./proxy -h ipfs.io -p 8040
./proxy -h https://ipfs.io -p 8040
```

Now you can access the gateway in [127.0.0.1:8040](http://127.0.0.1:8040). It will
Expand Down
13 changes: 9 additions & 4 deletions examples/gateway/proxy/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ var (

type proxyStore struct {
httpClient *http.Client
host string
gatewayURL string
validate bool
}

func newProxyStore(host string, client *http.Client) blockstore.Blockstore {
func newProxyStore(gatewayURL string, client *http.Client) blockstore.Blockstore {
if client == nil {
client = http.DefaultClient
}

return &proxyStore{
host: host,
gatewayURL: gatewayURL,
httpClient: client,
}
}

func (ps *proxyStore) fetch(ctx context.Context, c cid.Cid) (blocks.Block, error) {
u, err := url.Parse(fmt.Sprintf("http://%s/ipfs/%s?format=raw", ps.host, c))
u, err := url.Parse(fmt.Sprintf("%s/ipfs/%s?format=raw", ps.gatewayURL, c))
if err != nil {
return nil, err
}
Expand All @@ -50,6 +50,11 @@ func (ps *proxyStore) fetch(ctx context.Context, c cid.Cid) (blocks.Block, error
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status from remote gateway: %s", resp.Status)
}

rb, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions examples/gateway/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import (
)

func main() {
hostPtr := flag.String("h", "", "hostname of gateway to proxy to")
gatewayUrlPtr := flag.String("g", "", "gateway to proxy to")
portPtr := flag.Int("p", 8080, "port to run this gateway from")
flag.Parse()

// Sets up the block store, which will proxy the block requests to the given gateway.
blockStore := newProxyStore(*hostPtr, nil)
blockStore := newProxyStore(*gatewayUrlPtr, nil)
blockService := blockservice.New(blockStore, offline.Exchange(blockStore))

// Sets up the routing system, which will proxy the IPNS routing requests to the given gateway.
routing := newProxyRouting(*hostPtr, nil)
routing := newProxyRouting(*gatewayUrlPtr, nil)

// Creates the gateway with the block service and the routing.
gateway, err := gw.NewBlocksGateway(blockService, routing)
Expand Down
12 changes: 8 additions & 4 deletions examples/gateway/proxy/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ import (
)

type proxyRouting struct {
host string
gatewayURL string
httpClient *http.Client
}

func newProxyRouting(host string, client *http.Client) routing.ValueStore {
func newProxyRouting(gatewayURL string, client *http.Client) routing.ValueStore {
if client == nil {
client = http.DefaultClient
}

return &proxyRouting{
host: host,
gatewayURL: gatewayURL,
httpClient: client,
}
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func (ps *proxyRouting) SearchValue(ctx context.Context, k string, opts ...routi
}

func (ps *proxyRouting) fetch(ctx context.Context, id peer.ID) ([]byte, error) {
u, err := url.Parse(fmt.Sprintf("http://%s/ipns/%s", ps.host, peer.ToCid(id).String()))
u, err := url.Parse(fmt.Sprintf("%s/ipns/%s", ps.gatewayURL, peer.ToCid(id).String()))
if err != nil {
return nil, err
}
Expand All @@ -94,6 +94,10 @@ func (ps *proxyRouting) fetch(ctx context.Context, id peer.ID) ([]byte, error) {
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status from remote gateway: %s", resp.Status)
}

rb, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
Expand Down

0 comments on commit d73cc9a

Please sign in to comment.