Skip to content

Commit

Permalink
fix: print all car roots
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 2, 2023
1 parent 187bead commit 691d1b9
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions examples/gateway-car/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"errors"
"flag"
"io"
"log"
Expand All @@ -21,7 +20,7 @@ func main() {
portPtr := flag.Int("p", 8080, "port to run this gateway from")
flag.Parse()

blockService, root, f, err := newBlockServiceFromCAR(*carFilePtr)
blockService, roots, f, err := newBlockServiceFromCAR(*carFilePtr)
if err != nil {
log.Fatal(err)
}
Expand All @@ -36,14 +35,16 @@ func main() {

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())
for _, cid := range roots {
log.Printf("Hosting CAR root at http://%s/ipfs/%s", address, cid.String())
}

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

func newBlockServiceFromCAR(filepath string) (blockservice.BlockService, *cid.Cid, 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, nil, err
Expand All @@ -60,12 +61,8 @@ func newBlockServiceFromCAR(filepath string) (blockservice.BlockService, *cid.Ci
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, &roots[0], r, nil
return blockService, roots, r, nil
}

func newHandler(gw *blocksGateway, port int) http.Handler {
Expand Down

0 comments on commit 691d1b9

Please sign in to comment.