150 lines
4.3 KiB
Go
150 lines
4.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net"
|
|
"net/http"
|
|
_ "net/http/pprof"
|
|
"os"
|
|
"os/signal"
|
|
"runtime"
|
|
"syscall"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
logging "github.com/ipfs/go-log/v2"
|
|
"github.com/multiformats/go-multiaddr"
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
|
promclient "github.com/prometheus/client_golang/prometheus"
|
|
"go.opencensus.io/tag"
|
|
"golang.org/x/xerrors"
|
|
|
|
"contrib.go.opencensus.io/exporter/prometheus"
|
|
|
|
"github.com/filecoin-project/go-jsonrpc"
|
|
"github.com/filecoin-project/go-jsonrpc/auth"
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
"github.com/filecoin-project/lotus/api/apistruct"
|
|
"github.com/filecoin-project/lotus/metrics"
|
|
"github.com/filecoin-project/lotus/node"
|
|
"github.com/filecoin-project/lotus/node/impl"
|
|
)
|
|
|
|
var log = logging.Logger("main")
|
|
|
|
func serveRPC(a api.FullNode, stop node.StopFunc, addr multiaddr.Multiaddr, shutdownCh <-chan struct{}, maxRequestSize int64) error {
|
|
serverOptions := make([]jsonrpc.ServerOption, 0)
|
|
if maxRequestSize != 0 { // config set
|
|
serverOptions = append(serverOptions, jsonrpc.WithMaxRequestSize(maxRequestSize))
|
|
}
|
|
rpcServer := jsonrpc.NewServer(serverOptions...)
|
|
rpcServer.Register("Filecoin", apistruct.PermissionedFullAPI(metrics.MetricedFullAPI(a)))
|
|
|
|
ah := &auth.Handler{
|
|
Verify: a.AuthVerify,
|
|
Next: rpcServer.ServeHTTP,
|
|
}
|
|
|
|
http.Handle("/rpc/v0", ah)
|
|
|
|
importAH := &auth.Handler{
|
|
Verify: a.AuthVerify,
|
|
Next: handleImport(a.(*impl.FullNodeAPI)),
|
|
}
|
|
|
|
http.Handle("/rest/v0/import", importAH)
|
|
|
|
// Prometheus globals are exposed as interfaces, but the prometheus
|
|
// OpenCensus exporter expects a concrete *Registry. The concrete type of
|
|
// the globals are actually *Registry, so we downcast them, staying
|
|
// defensive in case things change under the hood.
|
|
registry, ok := promclient.DefaultRegisterer.(*promclient.Registry)
|
|
if !ok {
|
|
log.Warnf("failed to export default prometheus registry; some metrics will be unavailable; unexpected type: %T", promclient.DefaultRegisterer)
|
|
}
|
|
exporter, err := prometheus.NewExporter(prometheus.Options{
|
|
Registry: registry,
|
|
Namespace: "lotus",
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("could not create the prometheus stats exporter: %v", err)
|
|
}
|
|
|
|
http.Handle("/debug/metrics", exporter)
|
|
http.Handle("/debug/pprof-set/block", handleFractionOpt("BlockProfileRate", runtime.SetBlockProfileRate))
|
|
http.Handle("/debug/pprof-set/mutex", handleFractionOpt("MutexProfileFraction",
|
|
func(x int) { runtime.SetMutexProfileFraction(x) },
|
|
))
|
|
|
|
lst, err := manet.Listen(addr)
|
|
if err != nil {
|
|
return xerrors.Errorf("could not listen: %w", err)
|
|
}
|
|
|
|
srv := &http.Server{
|
|
Handler: http.DefaultServeMux,
|
|
BaseContext: func(listener net.Listener) context.Context {
|
|
ctx, _ := tag.New(context.Background(), tag.Upsert(metrics.APIInterface, "lotus-daemon"))
|
|
return ctx
|
|
},
|
|
}
|
|
|
|
sigCh := make(chan os.Signal, 2)
|
|
shutdownDone := make(chan struct{})
|
|
go func() {
|
|
select {
|
|
case sig := <-sigCh:
|
|
log.Warnw("received shutdown", "signal", sig)
|
|
case <-shutdownCh:
|
|
log.Warn("received shutdown")
|
|
}
|
|
|
|
log.Warn("Shutting down...")
|
|
if err := srv.Shutdown(context.TODO()); err != nil {
|
|
log.Errorf("shutting down RPC server failed: %s", err)
|
|
}
|
|
if err := stop(context.TODO()); err != nil {
|
|
log.Errorf("graceful shutting down failed: %s", err)
|
|
}
|
|
log.Warn("Graceful shutdown successful")
|
|
_ = log.Sync() //nolint:errcheck
|
|
close(shutdownDone)
|
|
}()
|
|
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
err = srv.Serve(manet.NetListener(lst))
|
|
if err == http.ErrServerClosed {
|
|
<-shutdownDone
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
func handleImport(a *impl.FullNodeAPI) func(w http.ResponseWriter, r *http.Request) {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "PUT" {
|
|
w.WriteHeader(404)
|
|
return
|
|
}
|
|
if !auth.HasPerm(r.Context(), nil, apistruct.PermWrite) {
|
|
w.WriteHeader(401)
|
|
_ = json.NewEncoder(w).Encode(struct{ Error string }{"unauthorized: missing write permission"})
|
|
return
|
|
}
|
|
|
|
c, err := a.ClientImportLocal(r.Context(), r.Body)
|
|
if err != nil {
|
|
w.WriteHeader(500)
|
|
_ = json.NewEncoder(w).Encode(struct{ Error string }{err.Error()})
|
|
return
|
|
}
|
|
w.WriteHeader(200)
|
|
err = json.NewEncoder(w).Encode(struct{ Cid cid.Cid }{c})
|
|
if err != nil {
|
|
log.Errorf("/rest/v0/import: Writing response failed: %+v", err)
|
|
return
|
|
}
|
|
}
|
|
}
|