mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 18:37:45 +08:00
coreapi: smarter way of dealing with the different APIs
License: MIT Signed-off-by: Lars Gierth <larsg@systemli.org>
This commit is contained in:
parent
6efa429a99
commit
e260d2fd06
@ -10,6 +10,19 @@ import (
|
||||
ipld "gx/ipfs/QmYDscK7dmdo2GZ9aumS8s5auUUAH5mR1jvj5pYhWusfK7/go-ipld-node"
|
||||
)
|
||||
|
||||
type CoreAPI struct {
|
||||
node *core.IpfsNode
|
||||
}
|
||||
|
||||
func NewCoreAPI(n *core.IpfsNode) coreiface.CoreAPI {
|
||||
api := &CoreAPI{n}
|
||||
return api
|
||||
}
|
||||
|
||||
func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI {
|
||||
return (*UnixfsAPI)(api)
|
||||
}
|
||||
|
||||
func resolve(ctx context.Context, n *core.IpfsNode, p string) (ipld.Node, error) {
|
||||
pp, err := path.ParsePath(p)
|
||||
if err != nil {
|
||||
|
||||
@ -9,11 +9,6 @@ import (
|
||||
ipld "gx/ipfs/QmYDscK7dmdo2GZ9aumS8s5auUUAH5mR1jvj5pYhWusfK7/go-ipld-node"
|
||||
)
|
||||
|
||||
// type CoreAPI interface {
|
||||
// ID() CoreID
|
||||
// Version() CoreVersion
|
||||
// }
|
||||
|
||||
type Link ipld.Link
|
||||
|
||||
type Reader interface {
|
||||
@ -21,6 +16,10 @@ type Reader interface {
|
||||
io.Closer
|
||||
}
|
||||
|
||||
type CoreAPI interface {
|
||||
Unixfs() UnixfsAPI
|
||||
}
|
||||
|
||||
type UnixfsAPI interface {
|
||||
Add(context.Context, io.Reader) (*cid.Cid, error)
|
||||
Cat(context.Context, string) (Reader, error)
|
||||
|
||||
@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"io"
|
||||
|
||||
core "github.com/ipfs/go-ipfs/core"
|
||||
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
|
||||
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
|
||||
uio "github.com/ipfs/go-ipfs/unixfs/io"
|
||||
@ -12,14 +11,7 @@ import (
|
||||
cid "gx/ipfs/QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk/go-cid"
|
||||
)
|
||||
|
||||
type UnixfsAPI struct {
|
||||
node *core.IpfsNode
|
||||
}
|
||||
|
||||
func NewUnixfsAPI(n *core.IpfsNode) coreiface.UnixfsAPI {
|
||||
api := &UnixfsAPI{n}
|
||||
return api
|
||||
}
|
||||
type UnixfsAPI CoreAPI
|
||||
|
||||
func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (*cid.Cid, error) {
|
||||
k, err := coreunix.AddWithContext(ctx, api.node, r)
|
||||
|
||||
@ -41,7 +41,7 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) {
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
api := coreapi.NewUnixfsAPI(node)
|
||||
api := coreapi.NewCoreAPI(node).Unixfs()
|
||||
return node, api, nil
|
||||
}
|
||||
|
||||
|
||||
@ -28,7 +28,7 @@ func GatewayOption(writable bool, paths ...string) ServeOption {
|
||||
Headers: cfg.Gateway.HTTPHeaders,
|
||||
Writable: writable,
|
||||
PathPrefixes: cfg.Gateway.PathPrefixes,
|
||||
}, coreapi.NewUnixfsAPI(n))
|
||||
}, coreapi.NewCoreAPI(n))
|
||||
|
||||
for _, p := range paths {
|
||||
mux.Handle(p+"/", gateway)
|
||||
|
||||
@ -37,10 +37,10 @@ const (
|
||||
type gatewayHandler struct {
|
||||
node *core.IpfsNode
|
||||
config GatewayConfig
|
||||
api coreiface.UnixfsAPI
|
||||
api coreiface.CoreAPI
|
||||
}
|
||||
|
||||
func newGatewayHandler(n *core.IpfsNode, c GatewayConfig, api coreiface.UnixfsAPI) *gatewayHandler {
|
||||
func newGatewayHandler(n *core.IpfsNode, c GatewayConfig, api coreiface.CoreAPI) *gatewayHandler {
|
||||
i := &gatewayHandler{
|
||||
node: n,
|
||||
config: c,
|
||||
@ -158,7 +158,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
|
||||
ipnsHostname = true
|
||||
}
|
||||
|
||||
dr, err := i.api.Cat(ctx, urlPath)
|
||||
dr, err := i.api.Unixfs().Cat(ctx, urlPath)
|
||||
dir := false
|
||||
switch err {
|
||||
case nil:
|
||||
@ -218,7 +218,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
|
||||
return
|
||||
}
|
||||
|
||||
links, err := i.api.Ls(ctx, urlPath)
|
||||
links, err := i.api.Unixfs().Ls(ctx, urlPath)
|
||||
if err != nil {
|
||||
internalWebError(w, err)
|
||||
return
|
||||
@ -247,7 +247,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
|
||||
}
|
||||
|
||||
// return index page instead.
|
||||
dr, err := i.api.Cat(ctx, p.String())
|
||||
dr, err := i.api.Unixfs().Cat(ctx, p.String())
|
||||
if err != nil {
|
||||
internalWebError(w, err)
|
||||
return
|
||||
@ -314,7 +314,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
|
||||
}
|
||||
|
||||
func (i *gatewayHandler) postHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
||||
k, err := i.api.Add(ctx, r.Body)
|
||||
k, err := i.api.Unixfs().Add(ctx, r.Body)
|
||||
if err != nil {
|
||||
internalWebError(w, err)
|
||||
return
|
||||
|
||||
Loading…
Reference in New Issue
Block a user