cli: use repo api endpoint

This commit is contained in:
Łukasz Magiera 2019-07-10 19:28:49 +02:00
parent 2e8dfc759b
commit e639670195
9 changed files with 52 additions and 13 deletions

View File

@ -18,7 +18,10 @@ var chainHeadCmd = &cli.Command{
Name: "head",
Usage: "Print chain head",
Action: func(cctx *cli.Context) error {
api := getApi(cctx)
api, err := getApi(cctx)
if err != nil {
return err
}
ctx := reqContext(cctx)
head, err := api.ChainHead(ctx)

View File

@ -6,20 +6,33 @@ import (
"os/signal"
"syscall"
"github.com/filecoin-project/go-lotus/api"
manet "github.com/multiformats/go-multiaddr-net"
"gopkg.in/urfave/cli.v2"
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/api/client"
"github.com/filecoin-project/go-lotus/node/repo"
)
const (
metadataContext = "context"
metadataAPI = "api"
)
// ApiConnector returns API instance
type ApiConnector func() api.API
func getApi(ctx *cli.Context) api.API {
return ctx.App.Metadata[metadataAPI].(ApiConnector)()
func getApi(ctx *cli.Context) (api.API, error) {
r, err := repo.NewFS(ctx.String("repo"))
if err != nil {
return nil, err
}
ma, err := r.APIEndpoint()
if err != nil {
return nil, err
}
_, addr, err := manet.DialArgs(ma)
return client.NewRPC("http://" + addr + "/rpc/v0"), nil
}
// reqContext returns context for cli execution. Calling it for the first time

View File

@ -26,7 +26,10 @@ var netPeers = &cli.Command{
Name: "peers",
Usage: "Print peers",
Action: func(cctx *cli.Context) error {
api := getApi(cctx)
api, err := getApi(cctx)
if err != nil {
return err
}
ctx := reqContext(cctx)
peers, err := api.NetPeers(ctx)
if err != nil {
@ -45,7 +48,10 @@ var netListen = &cli.Command{
Name: "listen",
Usage: "List listen addresses",
Action: func(cctx *cli.Context) error {
api := getApi(cctx)
api, err := getApi(cctx)
if err != nil {
return err
}
ctx := reqContext(cctx)
addrs, err := api.NetAddrsListen(ctx)
@ -64,7 +70,10 @@ var netConnect = &cli.Command{
Name: "connect",
Usage: "Connect to a peer",
Action: func(cctx *cli.Context) error {
api := getApi(cctx)
api, err := getApi(cctx)
if err != nil {
return err
}
ctx := reqContext(cctx)
pis, err := parseAddresses(ctx, cctx.Args().Slice())

View File

@ -5,6 +5,7 @@ package daemon
import (
"context"
"github.com/multiformats/go-multiaddr"
"gopkg.in/urfave/cli.v2"
"github.com/filecoin-project/go-lotus/node"
@ -18,7 +19,7 @@ var Cmd = &cli.Command{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "api",
Value: ":1234",
Value: "1234",
},
},
Action: func(cctx *cli.Context) error {
@ -35,11 +36,19 @@ var Cmd = &cli.Command{
api, err := node.New(ctx,
node.Online(),
node.Repo(r),
node.Override(node.SetApiEndpointKey, func(lr repo.LockedRepo) error {
apima, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/" + cctx.String("api"))
if err != nil {
return err
}
return lr.SetAPIEndpoint(apima)
}),
)
if err != nil {
return err
}
return serveRPC(api, cctx.String("api"))
return serveRPC(api, ":"+cctx.String("api"))
},
}

1
go.mod
View File

@ -42,6 +42,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/multiformats/go-multiaddr v0.0.4
github.com/multiformats/go-multiaddr-dns v0.0.2
github.com/multiformats/go-multiaddr-net v0.0.1
github.com/multiformats/go-multihash v0.0.5
github.com/pkg/errors v0.8.1
github.com/polydawn/refmt v0.0.0-20190408063855-01bf1e26dd14

View File

@ -66,6 +66,8 @@ const (
HandleIncomingBlocksKey
HandleIncomingMessagesKey
SetApiEndpointKey
_nInvokes // keep this last
)

View File

@ -2,10 +2,11 @@ package node_test
import (
"context"
"github.com/filecoin-project/go-lotus/node"
"net/http/httptest"
"testing"
"github.com/filecoin-project/go-lotus/node"
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/api/client"
"github.com/filecoin-project/go-lotus/api/test"

View File

@ -2,6 +2,7 @@ package node_test
import (
"errors"
"github.com/filecoin-project/go-lotus/node"
"github.com/filecoin-project/go-lotus/node/modules/lp2p"

View File

@ -115,8 +115,8 @@ type fsLockedRepo struct {
path string
closer io.Closer
ds datastore.Batching
dsErr error
ds datastore.Batching
dsErr error
dsOnce sync.Once
}