-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (67 loc) · 1.98 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"context"
"flag"
"io"
"log"
"net/http"
"os"
"strconv"
"github.com/ipfs/boxo/blockservice"
"github.com/ipfs/boxo/examples/gateway/common"
offline "github.com/ipfs/boxo/exchange/offline"
"github.com/ipfs/boxo/gateway"
"github.com/ipfs/go-cid"
carblockstore "github.com/ipld/go-car/v2/blockstore"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
carFilePtr := flag.String("c", "", "path to CAR file to back this gateway from")
port := flag.Int("p", 8040, "port to run this gateway from")
flag.Parse()
// Setups up tracing. This is optional and only required if the implementer
// wants to be able to enable tracing.
tp, err := common.SetupTracing(ctx, "CAR Gateway Example")
if err != nil {
log.Fatal(err)
}
defer (func() { _ = tp.Shutdown(ctx) })()
// Sets up a block service based on the CAR file.
blockService, roots, f, err := newBlockServiceFromCAR(*carFilePtr)
if err != nil {
log.Fatal(err)
}
defer f.Close()
// Creates the gateway API with the block service.
backend, err := gateway.NewBlocksBackend(blockService)
if err != nil {
log.Fatal(err)
}
handler := common.NewHandler(backend)
log.Printf("Listening on http://localhost:%d", *port)
log.Printf("Metrics available at http://127.0.0.1:%d/debug/metrics/prometheus", *port)
for _, cid := range roots {
log.Printf("Hosting CAR root at http://localhost:%d/ipfs/%s", *port, cid.String())
}
if err := http.ListenAndServe(":"+strconv.Itoa(*port), handler); err != nil {
log.Fatal(err)
}
}
func newBlockServiceFromCAR(filepath string) (blockservice.BlockService, []cid.Cid, io.Closer, error) {
r, err := os.Open(filepath)
if err != nil {
return nil, nil, nil, err
}
bs, err := carblockstore.NewReadOnly(r, nil)
if err != nil {
_ = r.Close()
return nil, nil, nil, err
}
roots, err := bs.Roots()
if err != nil {
return nil, nil, nil, err
}
blockService := blockservice.New(bs, offline.Exchange(bs))
return blockService, roots, r, nil
}