Skip to content

Commit

Permalink
feat: add status api to detect api ready
Browse files Browse the repository at this point in the history
  • Loading branch information
hunjixin committed Feb 21, 2023
1 parent 3c645cd commit 3d74d94
Showing 1 changed file with 24 additions and 32 deletions.
56 changes: 24 additions & 32 deletions cmd/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,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 +50,36 @@ 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...))

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

0 comments on commit 3d74d94

Please sign in to comment.