diff --git a/cmd/lotus/daemon.go b/cmd/lotus/daemon.go new file mode 100644 index 000000000..3ad9f4620 --- /dev/null +++ b/cmd/lotus/daemon.go @@ -0,0 +1,54 @@ +// +build !nodaemon + +package main + +import ( + "context" + "github.com/multiformats/go-multiaddr" + "gopkg.in/urfave/cli.v2" + + "github.com/filecoin-project/go-lotus/node" + "github.com/filecoin-project/go-lotus/node/repo" +) + +// DaemonCmd is the `go-lotus daemon` command +var DaemonCmd = &cli.Command{ + Name: "daemon", + Usage: "Start a lotus daemon process", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "api", + Value: "1234", + }, + }, + Action: func(cctx *cli.Context) error { + ctx := context.Background() + r, err := repo.NewFS(cctx.String("repo")) + if err != nil { + return err + } + + if err := r.Init(); err != nil && err != repo.ErrRepoExists { + return err + } + + 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 + } + + // TODO: properly parse api endpoint (or make it a URL) + return serveRPC(api, "127.0.0.1:"+cctx.String("api")) + }, +} diff --git a/cmd/lotus/daemon_nodaemon.go b/cmd/lotus/daemon_nodaemon.go new file mode 100644 index 000000000..bbaaa4963 --- /dev/null +++ b/cmd/lotus/daemon_nodaemon.go @@ -0,0 +1,24 @@ +// +build nodaemon + +package main + +import ( + "errors" + + "gopkg.in/urfave/cli.v2" +) + +// DaemonCmd is the `go-lotus daemon` command +var Cmd = &cli.Command{ + Name: "daemon", + Usage: "Start a lotus daemon process", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "api", + Value: ":1234", + }, + }, + Action: func(cctx *cli.Context) error { + return errors.New("daemon support not included in this binary") + }, +} diff --git a/cmd/lotus/rpc.go b/cmd/lotus/rpc.go new file mode 100644 index 000000000..8801bd0c4 --- /dev/null +++ b/cmd/lotus/rpc.go @@ -0,0 +1,15 @@ +package main + +import ( + "net/http" + + "github.com/filecoin-project/go-lotus/api" + "github.com/filecoin-project/go-lotus/lib/jsonrpc" +) + +func serveRPC(api api.API, addr string) error { + rpcServer := jsonrpc.NewServer() + rpcServer.Register("Filecoin", api) + http.Handle("/rpc/v0", rpcServer) + return http.ListenAndServe(addr, http.DefaultServeMux) +}