lotus/cmd/lotus/rpc.go

123 lines
3.0 KiB
Go
Raw Normal View History

2019-07-18 23:18:26 +00:00
package main
import (
2019-09-17 14:23:08 +00:00
"context"
2019-10-23 09:18:22 +00:00
"encoding/json"
2019-07-18 23:18:26 +00:00
"net/http"
2019-09-30 23:25:45 +00:00
_ "net/http/pprof"
2019-09-17 14:23:08 +00:00
"os"
"os/signal"
"syscall"
2019-07-18 23:18:26 +00:00
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
"github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"golang.org/x/xerrors"
"contrib.go.opencensus.io/exporter/prometheus"
2020-05-20 17:43:22 +00:00
"github.com/filecoin-project/go-jsonrpc"
2020-05-20 18:23:51 +00:00
"github.com/filecoin-project/go-jsonrpc/auth"
2020-05-20 17:43:22 +00:00
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/apistruct"
2020-10-21 08:10:27 +00:00
"github.com/filecoin-project/lotus/metrics"
2020-05-20 17:43:22 +00:00
"github.com/filecoin-project/lotus/node"
"github.com/filecoin-project/lotus/node/impl"
2019-07-18 23:18:26 +00:00
)
2019-09-17 14:23:08 +00:00
var log = logging.Logger("main")
func serveRPC(a api.FullNode, stop node.StopFunc, addr multiaddr.Multiaddr, shutdownCh <-chan struct{}) error {
2019-07-18 23:18:26 +00:00
rpcServer := jsonrpc.NewServer()
2020-10-21 08:10:27 +00:00
rpcServer.Register("Filecoin", apistruct.PermissionedFullAPI(metrics.MetricedFullAPI(a)))
2019-07-24 13:40:42 +00:00
ah := &auth.Handler{
Verify: a.AuthVerify,
2019-07-25 12:54:19 +00:00
Next: rpcServer.ServeHTTP,
2019-07-24 13:40:42 +00:00
}
http.Handle("/rpc/v0", ah)
2019-09-17 14:23:08 +00:00
2019-10-23 09:18:22 +00:00
importAH := &auth.Handler{
Verify: a.AuthVerify,
Next: handleImport(a.(*impl.FullNodeAPI)),
}
http.Handle("/rest/v0/import", importAH)
exporter, err := prometheus.NewExporter(prometheus.Options{
Namespace: "lotus",
})
if err != nil {
log.Fatalf("could not create the prometheus stats exporter: %v", err)
}
http.Handle("/debug/metrics", exporter)
2020-02-23 22:54:49 +00:00
lst, err := manet.Listen(addr)
if err != nil {
return xerrors.Errorf("could not listen: %w", err)
}
srv := &http.Server{Handler: http.DefaultServeMux}
2019-09-17 14:23:08 +00:00
sigCh := make(chan os.Signal, 2)
shutdownDone := make(chan struct{})
2019-09-17 14:23:08 +00:00
go func() {
select {
case sig := <-sigCh:
log.Warnw("received shutdown", "signal", sig)
case <-shutdownCh:
log.Warn("received shutdown")
}
log.Warn("Shutting down...")
2019-09-17 14:23:08 +00:00
if err := srv.Shutdown(context.TODO()); err != nil {
log.Errorf("shutting down RPC server failed: %s", err)
}
2019-10-20 06:17:03 +00:00
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)
2019-09-17 14:23:08 +00:00
}()
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
2019-09-17 14:23:08 +00:00
err = srv.Serve(manet.NetListener(lst))
if err == http.ErrServerClosed {
<-shutdownDone
return nil
}
return err
2019-07-18 23:18:26 +00:00
}
2019-10-23 09:18:22 +00:00
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
}
2020-05-20 18:23:51 +00:00
if !auth.HasPerm(r.Context(), nil, apistruct.PermWrite) {
2019-10-23 09:18:22 +00:00
w.WriteHeader(401)
_ = json.NewEncoder(w).Encode(struct{ Error string }{"unauthorized: missing write permission"})
2019-10-23 09:18:22 +00:00
return
}
c, err := a.ClientImportLocal(r.Context(), r.Body)
if err != nil {
w.WriteHeader(500)
_ = json.NewEncoder(w).Encode(struct{ Error string }{err.Error()})
2019-10-23 09:18:22 +00:00
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
}
}
}