Skip to content

Commit

Permalink
refactor: use minimal node API for gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Jan 31, 2023
1 parent 8d3b315 commit ccd1e83
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
63 changes: 57 additions & 6 deletions core/corehttp/gateway.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package corehttp

import (
"context"
"fmt"
"net"
"net/http"

"github.com/ipfs/go-libipfs/gateway"
iface "github.com/ipfs/interface-go-ipfs-core"
options "github.com/ipfs/interface-go-ipfs-core/options"
"github.com/ipfs/interface-go-ipfs-core/path"
version "github.com/ipfs/kubo"
core "github.com/ipfs/kubo/core"
coreapi "github.com/ipfs/kubo/core/coreapi"
Expand All @@ -21,7 +24,7 @@ func GatewayOption(writable bool, paths ...string) ServeOption {
return nil, err
}

api, err := coreapi.NewCoreAPI(n, options.Api.FetchBlocks(!cfg.Gateway.NoFetch))
nodeAPI, err := coreapi.NewCoreAPI(n, options.Api.FetchBlocks(!cfg.Gateway.NoFetch))
if err != nil {
return nil, err
}
Expand All @@ -33,16 +36,20 @@ func GatewayOption(writable bool, paths ...string) ServeOption {

gateway.AddAccessControlHeaders(headers)

offlineAPI, err := api.WithOptions(options.Api.Offline(true))
offlineNodeAPI, err := nodeAPI.WithOptions(options.Api.Offline(true))
if err != nil {
return nil, err
}

gateway := gateway.NewHandler(gateway.Config{
Headers: headers,
Writable: writable,
}, api, offlineAPI)
gatewayCfg := gateway.Config{
Headers: headers,
}

if writable {
gatewayCfg.WriterAPI = &writerAPI{nodeAPI}
}

gateway := gateway.NewHandler(gatewayCfg, &readerAPI{nodeAPI}, &offlineAPI{offlineNodeAPI})
gateway = otelhttp.NewHandler(gateway, "Gateway.Request")

for _, p := range paths {
Expand All @@ -62,3 +69,47 @@ func VersionOption() ServeOption {
return mux, nil
}
}

type readerAPI struct {
node iface.CoreAPI
}

func (a *readerAPI) UnixFs() gateway.ReaderUnixFsAPI {
return a.node.Unixfs()
}

func (a *readerAPI) Block() gateway.ReaderBlockAPI {
return a.node.Block()
}

func (a *readerAPI) Dag() gateway.ReaderDagAPI {
return a.node.Dag()
}

func (a *readerAPI) Routing() gateway.ReaderRoutingAPI {
return a.node.Routing()
}

func (a *readerAPI) ResolvePath(ctx context.Context, p path.Path) (path.Resolved, error) {
return a.node.ResolvePath(ctx, p)
}

type offlineAPI struct {
node iface.CoreAPI
}

func (a *offlineAPI) Block() gateway.OfflineBlockAPI {
return a.node.Block()
}

type writerAPI struct {
node iface.CoreAPI
}

func (w *writerAPI) UnixFs() gateway.WriterUnixFsAPI {
return w.node.Unixfs()
}

func (w *writerAPI) Dag() iface.APIDagService {
return w.node.Dag()
}
2 changes: 1 addition & 1 deletion docs/examples/kubo-as-a-library/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ go 1.18
replace github.com/ipfs/kubo => ./../../..

require (
github.com/ipfs/go-libipfs v0.4.1-0.20230130233950-a005a5006496
github.com/ipfs/go-libipfs v0.4.1-0.20230131113959-d5b45ee1d11d
github.com/ipfs/interface-go-ipfs-core v0.10.0
github.com/ipfs/kubo v0.0.0-00010101000000-000000000000
github.com/libp2p/go-libp2p v0.24.2
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/kubo-as-a-library/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@ github.com/ipfs/go-ipld-legacy v0.1.1 h1:BvD8PEuqwBHLTKqlGFTHSwrwFOMkVESEvwIYwR2
github.com/ipfs/go-ipld-legacy v0.1.1/go.mod h1:8AyKFCjgRPsQFf15ZQgDB8Din4DML/fOmKZkkFkrIEg=
github.com/ipfs/go-ipns v0.3.0 h1:ai791nTgVo+zTuq2bLvEGmWP1M0A6kGTXUsgv/Yq67A=
github.com/ipfs/go-ipns v0.3.0/go.mod h1:3cLT2rbvgPZGkHJoPO1YMJeh6LtkxopCkKFcio/wE24=
github.com/ipfs/go-libipfs v0.4.1-0.20230130233950-a005a5006496 h1:RVI31GQCFODREpasIFyVFkS6PjJT2bMwr/Bgr9Ryql4=
github.com/ipfs/go-libipfs v0.4.1-0.20230130233950-a005a5006496/go.mod h1:AAPvZADZ80i+QhGCWNWCsx8IGY0t9C+IBEngLeYtySY=
github.com/ipfs/go-libipfs v0.4.1-0.20230131113959-d5b45ee1d11d h1:Xb5vwV5LOh0jr7DHCtDtMGp30TxrsNvnoy7QjlZmmVQ=
github.com/ipfs/go-libipfs v0.4.1-0.20230131113959-d5b45ee1d11d/go.mod h1:AAPvZADZ80i+QhGCWNWCsx8IGY0t9C+IBEngLeYtySY=
github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM=
github.com/ipfs/go-log v1.0.2/go.mod h1:1MNjMxe0u6xvJZgeqbJ8vdo2TKaGwZ1a0Bpza+sr2Sk=
github.com/ipfs/go-log v1.0.3/go.mod h1:OsLySYkwIbiSUR/yBTdv1qPtcE4FW3WPWk/ewz9Ru+A=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ require (
github.com/ipfs/go-ipld-git v0.1.1
github.com/ipfs/go-ipld-legacy v0.1.1
github.com/ipfs/go-ipns v0.3.0
github.com/ipfs/go-libipfs v0.4.1-0.20230130233950-a005a5006496
github.com/ipfs/go-libipfs v0.4.1-0.20230131113959-d5b45ee1d11d
github.com/ipfs/go-log v1.0.5
github.com/ipfs/go-log/v2 v2.5.1
github.com/ipfs/go-merkledag v0.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,8 @@ github.com/ipfs/go-ipld-legacy v0.1.1 h1:BvD8PEuqwBHLTKqlGFTHSwrwFOMkVESEvwIYwR2
github.com/ipfs/go-ipld-legacy v0.1.1/go.mod h1:8AyKFCjgRPsQFf15ZQgDB8Din4DML/fOmKZkkFkrIEg=
github.com/ipfs/go-ipns v0.3.0 h1:ai791nTgVo+zTuq2bLvEGmWP1M0A6kGTXUsgv/Yq67A=
github.com/ipfs/go-ipns v0.3.0/go.mod h1:3cLT2rbvgPZGkHJoPO1YMJeh6LtkxopCkKFcio/wE24=
github.com/ipfs/go-libipfs v0.4.1-0.20230130233950-a005a5006496 h1:RVI31GQCFODREpasIFyVFkS6PjJT2bMwr/Bgr9Ryql4=
github.com/ipfs/go-libipfs v0.4.1-0.20230130233950-a005a5006496/go.mod h1:AAPvZADZ80i+QhGCWNWCsx8IGY0t9C+IBEngLeYtySY=
github.com/ipfs/go-libipfs v0.4.1-0.20230131113959-d5b45ee1d11d h1:Xb5vwV5LOh0jr7DHCtDtMGp30TxrsNvnoy7QjlZmmVQ=
github.com/ipfs/go-libipfs v0.4.1-0.20230131113959-d5b45ee1d11d/go.mod h1:AAPvZADZ80i+QhGCWNWCsx8IGY0t9C+IBEngLeYtySY=
github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM=
github.com/ipfs/go-log v1.0.2/go.mod h1:1MNjMxe0u6xvJZgeqbJ8vdo2TKaGwZ1a0Bpza+sr2Sk=
github.com/ipfs/go-log v1.0.3/go.mod h1:OsLySYkwIbiSUR/yBTdv1qPtcE4FW3WPWk/ewz9Ru+A=
Expand Down

0 comments on commit ccd1e83

Please sign in to comment.