Skip to content

Commit

Permalink
feat: UX improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 2, 2023
1 parent 97549ec commit 187bead
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/gateway-car/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Then, you can start the gateway with:
./gateway -c data.car -p 8040
```

Now you can access the gateway in [localhost:8040](http://localhost:8040). It will
Now you can access the gateway in [127.0.0.1:8040](http://127.0.0.1:8040). It will
behave like a regular IPFS Gateway, except for the fact that all contents are provided
from the CAR file. Therefore, things such as IPNS resolution and fetching contents
from nodes in the IPFS network won't work.
26 changes: 19 additions & 7 deletions examples/gateway-car/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"flag"
"io"
"log"
Expand All @@ -9,6 +10,7 @@ import (
"strconv"

"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-cid"
offline "github.com/ipfs/go-ipfs-exchange-offline"
"github.com/ipfs/go-libipfs/gateway"
carblockstore "github.com/ipld/go-car/v2/blockstore"
Expand All @@ -19,7 +21,7 @@ func main() {
portPtr := flag.Int("p", 8080, "port to run this gateway from")
flag.Parse()

blockService, f, err := newBlockServiceFromCAR(*carFilePtr)
blockService, root, f, err := newBlockServiceFromCAR(*carFilePtr)
if err != nil {
log.Fatal(err)
}
Expand All @@ -32,28 +34,38 @@ func main() {

handler := newHandler(gateway, *portPtr)

address := ":" + strconv.Itoa(*portPtr)
log.Printf("Listening on %s", address)
address := "127.0.0.1:" + strconv.Itoa(*portPtr)
log.Printf("Listening on http://%s", address)
log.Printf("Hosting CAR root at http://%s/ipfs/%s", address, root.String())

if err := http.ListenAndServe(address, handler); err != nil {
log.Fatal(err)
}
}

func newBlockServiceFromCAR(filepath string) (blockservice.BlockService, io.Closer, error) {
func newBlockServiceFromCAR(filepath string) (blockservice.BlockService, *cid.Cid, io.Closer, error) {
r, err := os.Open(filepath)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}

bs, err := carblockstore.NewReadOnly(r, nil)
if err != nil {
_ = r.Close()
return nil, nil, err
return nil, nil, nil, err
}

roots, err := bs.Roots()
if err != nil {
return nil, nil, nil, err
}

if len(roots) == 0 {
return nil, nil, nil, errors.New("provided CAR file has no roots")
}

blockService := blockservice.New(bs, offline.Exchange(bs))
return blockService, r, nil
return blockService, &roots[0], r, nil
}

func newHandler(gw *blocksGateway, port int) http.Handler {
Expand Down
2 changes: 1 addition & 1 deletion examples/gateway-car/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
)

func newTestServer() (*httptest.Server, io.Closer, error) {
blockService, f, err := newBlockServiceFromCAR("./test.car")
blockService, _, f, err := newBlockServiceFromCAR("./test.car")
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 187bead

Please sign in to comment.