2020-09-12 03:01:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2021-02-01 21:13:20 +00:00
|
|
|
"contrib.go.opencensus.io/exporter/prometheus"
|
2020-09-12 03:01:37 +00:00
|
|
|
"github.com/filecoin-project/go-jsonrpc"
|
2021-02-01 21:13:20 +00:00
|
|
|
promclient "github.com/prometheus/client_golang/prometheus"
|
2020-10-21 08:37:50 +00:00
|
|
|
"go.opencensus.io/tag"
|
|
|
|
|
2020-09-12 03:01:37 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
lcli "github.com/filecoin-project/lotus/cli"
|
|
|
|
"github.com/filecoin-project/lotus/lib/lotuslog"
|
2020-10-21 08:37:50 +00:00
|
|
|
"github.com/filecoin-project/lotus/metrics"
|
|
|
|
|
2021-04-06 13:04:32 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2020-10-21 08:37:50 +00:00
|
|
|
"go.opencensus.io/stats/view"
|
2020-09-12 03:01:37 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("gateway")
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
lotuslog.SetupLogLevels()
|
|
|
|
|
|
|
|
local := []*cli.Command{
|
|
|
|
runCmd,
|
|
|
|
}
|
|
|
|
|
|
|
|
app := &cli.App{
|
|
|
|
Name: "lotus-gateway",
|
|
|
|
Usage: "Public API server for lotus",
|
|
|
|
Version: build.UserVersion(),
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "repo",
|
|
|
|
EnvVars: []string{"LOTUS_PATH"},
|
|
|
|
Value: "~/.lotus", // TODO: Consider XDG_DATA_HOME
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Commands: local,
|
|
|
|
}
|
|
|
|
app.Setup()
|
|
|
|
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
log.Warnf("%+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var runCmd = &cli.Command{
|
|
|
|
Name: "run",
|
|
|
|
Usage: "Start api server",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "listen",
|
|
|
|
Usage: "host address and port the api server will listen on",
|
2020-09-15 09:45:03 +00:00
|
|
|
Value: "0.0.0.0:2346",
|
2020-09-12 03:01:37 +00:00
|
|
|
},
|
2020-11-17 13:39:53 +00:00
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "api-max-req-size",
|
|
|
|
Usage: "maximum API request size accepted by the JSON RPC server",
|
|
|
|
},
|
2020-09-12 03:01:37 +00:00
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
2020-09-15 09:45:03 +00:00
|
|
|
log.Info("Starting lotus gateway")
|
2020-09-12 03:01:37 +00:00
|
|
|
|
|
|
|
ctx := lcli.ReqContext(cctx)
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-10-21 08:37:50 +00:00
|
|
|
// Register all metric views
|
|
|
|
if err := view.Register(
|
2021-02-21 10:03:00 +00:00
|
|
|
metrics.ChainNodeViews...,
|
2020-10-21 08:37:50 +00:00
|
|
|
); err != nil {
|
|
|
|
log.Fatalf("Cannot register the view: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-09-12 03:01:37 +00:00
|
|
|
api, closer, err := lcli.GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
address := cctx.String("listen")
|
|
|
|
mux := mux.NewRouter()
|
|
|
|
|
|
|
|
log.Info("Setting up API endpoint at " + address)
|
|
|
|
|
2020-11-17 13:39:53 +00:00
|
|
|
serverOptions := make([]jsonrpc.ServerOption, 0)
|
|
|
|
if maxRequestSize := cctx.Int("api-max-req-size"); maxRequestSize != 0 {
|
|
|
|
serverOptions = append(serverOptions, jsonrpc.WithMaxRequestSize(int64(maxRequestSize)))
|
|
|
|
}
|
|
|
|
rpcServer := jsonrpc.NewServer(serverOptions...)
|
2020-10-21 08:37:50 +00:00
|
|
|
rpcServer.Register("Filecoin", metrics.MetricedGatewayAPI(NewGatewayAPI(api)))
|
2020-09-12 03:01:37 +00:00
|
|
|
|
|
|
|
mux.Handle("/rpc/v0", rpcServer)
|
2021-02-01 21:13:20 +00:00
|
|
|
|
|
|
|
registry := promclient.DefaultRegisterer.(*promclient.Registry)
|
|
|
|
exporter, err := prometheus.NewExporter(prometheus.Options{
|
|
|
|
Registry: registry,
|
|
|
|
Namespace: "lotus_gw",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mux.Handle("/debug/metrics", exporter)
|
|
|
|
|
2020-09-12 03:01:37 +00:00
|
|
|
mux.PathPrefix("/").Handler(http.DefaultServeMux)
|
|
|
|
|
|
|
|
/*ah := &auth.Handler{
|
|
|
|
Verify: nodeApi.AuthVerify,
|
|
|
|
Next: mux.ServeHTTP,
|
|
|
|
}*/
|
|
|
|
|
|
|
|
srv := &http.Server{
|
|
|
|
Handler: mux,
|
|
|
|
BaseContext: func(listener net.Listener) context.Context {
|
2020-10-21 08:37:50 +00:00
|
|
|
ctx, _ := tag.New(context.Background(), tag.Upsert(metrics.APIInterface, "lotus-gateway"))
|
2020-09-12 03:01:37 +00:00
|
|
|
return ctx
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
log.Warn("Shutting down...")
|
|
|
|
if err := srv.Shutdown(context.TODO()); err != nil {
|
|
|
|
log.Errorf("shutting down RPC server failed: %s", err)
|
|
|
|
}
|
|
|
|
log.Warn("Graceful shutdown successful")
|
|
|
|
}()
|
|
|
|
|
|
|
|
nl, err := net.Listen("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return srv.Serve(nl)
|
|
|
|
},
|
|
|
|
}
|