Skip to content

Commit

Permalink
Merge pull request #34 from ipfs-force-community/feat/add_status_api
Browse files Browse the repository at this point in the history
feat: add status api to detect api ready
  • Loading branch information
simlecode authored Feb 21, 2023
2 parents 3c645cd + a3d8c56 commit 7917594
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
58 changes: 26 additions & 32 deletions cmd/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"syscall"

"github.com/dtynn/dix"
"github.com/etherlabsio/healthcheck/v2"
"github.com/filecoin-project/go-jsonrpc"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/v0api"
Expand All @@ -27,38 +28,12 @@ func serveRPC(ctx context.Context, authEndpoint, rateLimitRedis, listen string,
serverOptions = append(serverOptions, jsonrpc.WithMaxRequestSize(maxRequestSize))
}

rpcServer := jsonrpc.NewServer(serverOptions...)
rpcServer2 := jsonrpc.NewServer(serverOptions...)
rpcServer3 := jsonrpc.NewServer(serverOptions...)

var remoteJwtCli *jwtclient.AuthClient
if len(authEndpoint) > 0 {
remoteJwtCli, _ = jwtclient.NewAuthClient(authEndpoint)
}

// register hander to verify token in venus-auth
var handler, handler2 http.Handler
if remoteJwtCli != nil {
handler = (http.Handler)(jwtclient.NewAuthMux(jwt, jwtclient.WarpIJwtAuthClient(remoteJwtCli), rpcServer))
handler2 = (http.Handler)(jwtclient.NewAuthMux(jwt, jwtclient.WarpIJwtAuthClient(remoteJwtCli), rpcServer2))
} else {
handler = (http.Handler)(jwtclient.NewAuthMux(jwt, nil, rpcServer))
handler2 = (http.Handler)(jwtclient.NewAuthMux(jwt, nil, rpcServer2))
}
handler3 := (http.Handler)(jwtclient.NewAuthMux(jwt, nil, rpcServer3))

if repoter, err := metrics.RegisterJaeger(mCnf.ServerName, mCnf); err != nil {
log.Fatalf("register %s JaegerRepoter to %s failed:%s", mCnf.ServerName, mCnf.JaegerEndpoint, err)
} else if repoter != nil {
log.Infof("register jaeger-tracing exporter to %s, with node-name:%s", mCnf.JaegerEndpoint, mCnf.ServerName)
defer metrics.UnregisterJaeger(repoter)
handler = &ochttp.Handler{Handler: handler}
handler2 = &ochttp.Handler{Handler: handler2}
handler3 = &ochttp.Handler{Handler: handler3}
}

pma := api.PermissionedFullAPI(full)

if len(rateLimitRedis) > 0 && remoteJwtCli != nil {
log.Infof("use rate limit %s", rateLimitRedis)
limiter, err := ratelimit.NewRateLimitHandler(
Expand All @@ -76,18 +51,37 @@ func serveRPC(ctx context.Context, authEndpoint, rateLimitRedis, listen string,
pma = rateLimitAPI
}

serveRpc := func(path string, hnd interface{}, handler http.Handler, rpcSer *jsonrpc.RPCServer) {
mux := http.NewServeMux()

serveRpc := func(path string, hnd interface{}, rpcSer *jsonrpc.RPCServer) {
rpcSer.Register("Filecoin", hnd)
http.Handle(path, handler)

var handler http.Handler
if remoteJwtCli != nil {
handler = (http.Handler)(jwtclient.NewAuthMux(jwt, jwtclient.WarpIJwtAuthClient(remoteJwtCli), rpcSer))
} else {
handler = (http.Handler)(jwtclient.NewAuthMux(jwt, nil, rpcSer))
}
mux.Handle(path, handler)
}

serveRpc("/rpc/v0", &v0api.WrapperV1Full{FullNode: pma}, handler, rpcServer)
serveRpc("/rpc/v1", pma, handler2, rpcServer2)
serveRpc("/rpc/admin/v0", localApi, handler3, rpcServer3)
serveRpc("/rpc/v0", &v0api.WrapperV1Full{FullNode: pma}, jsonrpc.NewServer(serverOptions...))
serveRpc("/rpc/v1", pma, jsonrpc.NewServer(serverOptions...))
serveRpc("/rpc/admin/v0", localApi, jsonrpc.NewServer(serverOptions...))
mux.Handle("/healthcheck", healthcheck.Handler())

allHandler := (http.Handler)(mux)
if reporter, err := metrics.RegisterJaeger(mCnf.ServerName, mCnf); err != nil {
log.Fatalf("register %s JaegerRepoter to %s failed:%s", mCnf.ServerName, mCnf.JaegerEndpoint, err)
} else if reporter != nil {
log.Infof("register jaeger-tracing exporter to %s, with node-name:%s", mCnf.JaegerEndpoint, mCnf.ServerName)
defer metrics.UnregisterJaeger(reporter)
allHandler = &ochttp.Handler{Handler: allHandler}
}

server := http.Server{
Addr: listen,
Handler: http.DefaultServeMux,
Handler: allHandler,
BaseContext: func(net.Listener) context.Context {
return ctx
},
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.18

require (
github.com/dtynn/dix v0.1.0
github.com/etherlabsio/healthcheck/v2 v2.0.0
github.com/filecoin-project/go-address v1.1.0
github.com/filecoin-project/go-bitfield v0.2.4
github.com/filecoin-project/go-data-transfer v1.15.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.m
github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0=
github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/etherlabsio/healthcheck/v2 v2.0.0 h1:oKq8cbpwM/yNGPXf2Sff6MIjVUjx/pGYFydWzeK2MpA=
github.com/etherlabsio/healthcheck/v2 v2.0.0/go.mod h1:huNVOjKzu6FI1eaO1CGD3ZjhrmPWf5Obu/pzpI6/wog=
github.com/facebookgo/atomicfile v0.0.0-20151019160806-2de1f203e7d5 h1:BBso6MBKW8ncyZLv37o+KNyy0HrrHgfnOaGQC2qvN+A=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.8.0/go.mod h1:3l45GVGkyrnYNl9HoIjnp2NnNWvh6hLAqD8yTfGjnw8=
Expand Down

0 comments on commit 7917594

Please sign in to comment.