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
|
|
|
|
2019-10-22 17:18:06 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-01-08 19:10:57 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2019-10-22 17:18:06 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2020-08-25 23:46:31 +00:00
|
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
2019-10-22 17:18:06 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-02-23 22:35:29 +00:00
|
|
|
|
2020-02-26 00:40:07 +00:00
|
|
|
"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")
|
|
|
|
|
2020-06-02 18:54:24 +00:00
|
|
|
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)
|
|
|
|
|
2020-02-26 00:40:07 +00:00
|
|
|
exporter, err := prometheus.NewExporter(prometheus.Options{
|
|
|
|
Namespace: "lotus",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not create the prometheus stats exporter: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-02-26 00:49:32 +00:00
|
|
|
http.Handle("/debug/metrics", exporter)
|
2020-02-23 22:54:49 +00:00
|
|
|
|
2019-10-22 17:18:06 +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
|
|
|
|
2020-06-02 18:54:24 +00:00
|
|
|
sigCh := make(chan os.Signal, 2)
|
2020-07-08 18:56:03 +00:00
|
|
|
shutdownDone := make(chan struct{})
|
2019-09-17 14:23:08 +00:00
|
|
|
go func() {
|
2020-06-02 18:54:24 +00:00
|
|
|
select {
|
2020-09-18 18:07:58 +00:00
|
|
|
case sig := <-sigCh:
|
|
|
|
log.Warnw("received shutdown", "signal", sig)
|
2020-06-02 18:54:24 +00:00
|
|
|
case <-shutdownCh:
|
2020-09-18 18:07:58 +00:00
|
|
|
log.Warn("received shutdown")
|
2020-06-02 18:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2020-06-02 18:54:24 +00:00
|
|
|
log.Warn("Graceful shutdown successful")
|
2020-07-08 19:13:22 +00:00
|
|
|
_ = log.Sync() //nolint:errcheck
|
2020-07-08 18:56:03 +00:00
|
|
|
close(shutdownDone)
|
2019-09-17 14:23:08 +00:00
|
|
|
}()
|
2020-06-02 18:54:24 +00:00
|
|
|
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
|
2019-09-17 14:23:08 +00:00
|
|
|
|
2020-07-08 18:56:03 +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)
|
2020-05-27 20:53:20 +00:00
|
|
|
_ = 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)
|
2020-05-27 20:53:20 +00:00
|
|
|
_ = 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|