Skip to content

Commit

Permalink
feat(gateway): implement IPIP-402 extensions for gateway CAR requests (
Browse files Browse the repository at this point in the history
…#303)

Co-authored-by: Henrique Dias <[email protected]>
  • Loading branch information
aschmahmann and hacdias authored Jun 8, 2023
1 parent a85e005 commit 475d576
Show file tree
Hide file tree
Showing 32 changed files with 1,622 additions and 1,005 deletions.
4 changes: 2 additions & 2 deletions examples/gateway/car/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ func main() {
defer f.Close()

// Creates the gateway API with the block service.
gwAPI, err := gateway.NewBlocksGateway(blockService)
backend, err := gateway.NewBlocksBackend(blockService)
if err != nil {
log.Fatal(err)
}

handler := common.NewHandler(gwAPI)
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)
Expand Down
4 changes: 2 additions & 2 deletions examples/gateway/car/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ func newTestServer() (*httptest.Server, io.Closer, error) {
return nil, nil, err
}

gateway, err := gateway.NewBlocksGateway(blockService)
backend, err := gateway.NewBlocksBackend(blockService)
if err != nil {
_ = f.Close()
return nil, nil, err
}

handler := common.NewHandler(gateway)
handler := common.NewHandler(backend)
ts := httptest.NewServer(handler)
return ts, f, nil
}
Expand Down
4 changes: 2 additions & 2 deletions examples/gateway/common/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewHandler(gwAPI gateway.IPFSBackend) http.Handler {
// if you're running multiple public gateways on different hostnames
// and want different settings such as support for Deserialized
// Responses on localhost, or DNSLink and Subdomain Gateways.
PublicGateways: map[string]*gateway.Specification{
PublicGateways: map[string]*gateway.PublicGateway{
// Support public requests with Host: CID.ipfs.example.net and ID.ipns.example.net
"example.net": {
Paths: []string{"/ipfs", "/ipns"},
Expand Down Expand Up @@ -80,7 +80,7 @@ func NewHandler(gwAPI gateway.IPFSBackend) http.Handler {
// or example.net. If you want to expose the metrics on such gateways,
// you will have to add the path "/debug" to the variable Paths.
var handler http.Handler
handler = gateway.WithHostname(conf, gwAPI, mux)
handler = gateway.NewHostnameHandler(conf, gwAPI, mux)

// Then, wrap with the withConnect middleware. This is required since we use
// http.ServeMux which does not support CONNECT by default.
Expand Down
4 changes: 2 additions & 2 deletions examples/gateway/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ func main() {
routing := newProxyRouting(*gatewayUrlPtr, nil)

// Creates the gateway with the block service and the routing.
gwAPI, err := gateway.NewBlocksGateway(blockService, gateway.WithValueStore(routing))
backend, err := gateway.NewBlocksBackend(blockService, gateway.WithValueStore(routing))
if err != nil {
log.Fatal(err)
}

handler := common.NewHandler(gwAPI)
handler := common.NewHandler(backend)

log.Printf("Listening on http://localhost:%d", *port)
log.Printf("Try loading an image: http://localhost:%d/ipfs/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi", *port)
Expand Down
4 changes: 2 additions & 2 deletions examples/gateway/proxy/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func newProxyGateway(t *testing.T, rs *httptest.Server) *httptest.Server {
blockService := blockservice.New(blockStore, exch)
routing := newProxyRouting(rs.URL, nil)

gw, err := gateway.NewBlocksGateway(blockService, gateway.WithValueStore(routing))
backend, err := gateway.NewBlocksBackend(blockService, gateway.WithValueStore(routing))
if err != nil {
t.Error(err)
}

handler := common.NewHandler(gw)
handler := common.NewHandler(backend)
ts := httptest.NewServer(handler)
t.Cleanup(ts.Close)

Expand Down
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ require (
github.com/ipfs/go-log/v2 v2.5.1 // indirect
github.com/ipfs/go-metrics-interface v0.0.1 // indirect
github.com/ipfs/go-peertaskqueue v0.8.1 // indirect
github.com/ipfs/go-unixfsnode v1.6.0 // indirect
github.com/ipfs/go-unixfsnode v1.7.1 // indirect
github.com/ipld/go-codec-dagpb v1.6.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ github.com/ipfs/go-metrics-interface v0.0.1/go.mod h1:6s6euYU4zowdslK0GKHmqaIZ3j
github.com/ipfs/go-peertaskqueue v0.8.1 h1:YhxAs1+wxb5jk7RvS0LHdyiILpNmRIRnZVztekOF0pg=
github.com/ipfs/go-peertaskqueue v0.8.1/go.mod h1:Oxxd3eaK279FxeydSPPVGHzbwVeHjatZ2GA8XD+KbPU=
github.com/ipfs/go-unixfs v0.4.5 h1:wj8JhxvV1G6CD7swACwSKYa+NgtdWC1RUit+gFnymDU=
github.com/ipfs/go-unixfsnode v1.6.0 h1:JOSA02yaLylRNi2rlB4ldPr5VcZhcnaIVj5zNLcOjDo=
github.com/ipfs/go-unixfsnode v1.6.0/go.mod h1:PVfoyZkX1B34qzT3vJO4nsLUpRCyhnMuHBznRcXirlk=
github.com/ipfs/go-unixfsnode v1.7.1 h1:RRxO2b6CSr5UQ/kxnGzaChTjp5LWTdf3Y4n8ANZgB/s=
github.com/ipfs/go-unixfsnode v1.7.1/go.mod h1:PVfoyZkX1B34qzT3vJO4nsLUpRCyhnMuHBznRcXirlk=
github.com/ipfs/go-verifcid v0.0.2 h1:XPnUv0XmdH+ZIhLGKg6U2vaPaRDXb9urMyNVCE7uvTs=
github.com/ipld/go-car/v2 v2.9.1-0.20230325062757-fff0e4397a3d h1:22g+x1tgWSXK34i25qjs+afr7basaneEkHaglBshd2g=
github.com/ipld/go-codec-dagpb v1.6.0 h1:9nYazfyu9B1p3NAgfVdpRco3Fs2nFC72DqVsMj6rOcc=
Expand Down
20 changes: 12 additions & 8 deletions gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
## Documentation

* Go Documentation: https://pkg.go.dev/github.com/ipfs/boxo/gateway
* Gateway Specification: https://github.com/ipfs/specs/tree/main/http-gateways#readme
* Types of HTTP Gateways: https://docs.ipfs.tech/how-to/address-ipfs-on-web/#http-gateways
- Go Documentation: https://pkg.go.dev/github.com/ipfs/boxo/gateway
- Gateway Specification: https://specs.ipfs.tech/
- Types of HTTP Gateways: https://docs.ipfs.tech/how-to/address-ipfs-on-web/#http-gateways

## Example

This example shows how you can start your own gateway, assuming you have an `IPFSBackend`
implementation.

```go
// Initialize your headers and apply the default headers.
headers := map[string][]string{}
Expand All @@ -18,15 +22,15 @@ conf := gateway.Config{
Headers: headers,
}

// Initialize a NodeAPI interface for both an online and offline versions.
// Initialize an IPFSBackend interface for both an online and offline versions.
// The offline version should not make any network request for missing content.
ipfs := ...
ipfsBackend := ...

// Create http mux and setup path gateway handler.
mux := http.NewServeMux()
gwHandler := gateway.NewHandler(conf, ipfs)
mux.Handle("/ipfs/", gwHandler)
mux.Handle("/ipns/", gwHandler)
handler := gateway.NewHandler(conf, ipfsBackend)
mux.Handle("/ipfs/", handler)
mux.Handle("/ipns/", handler)

// Start the server on :8080 and voilá! You have a basic IPFS gateway running
// in http://localhost:8080.
Expand Down
Loading

0 comments on commit 475d576

Please sign in to comment.