laconicd/server/json_rpc.go
Daniel Burckhardt 34c2593e43
rpc: restructure JSON-RPC directory and rename server config (#612)
* Restructure ethermint/rpc repo structure and change import statements

* Add #400 to changelog

* fix filepath in util and json_rpc

* Move #400  to unreleased section
2021-10-01 14:49:22 +00:00

82 lines
2.2 KiB
Go

package server
import (
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/rs/cors"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/server/types"
ethrpc "github.com/ethereum/go-ethereum/rpc"
"github.com/tharsis/ethermint/rpc"
"github.com/tharsis/ethermint/server/config"
)
// StartJSONRPC starts the JSON-RPC server
func StartJSONRPC(ctx *server.Context, clientCtx client.Context, tmRPCAddr, tmEndpoint string, config config.Config) (*http.Server, chan struct{}, error) {
tmWsClient := ConnectTmWS(tmRPCAddr, tmEndpoint, ctx.Logger)
rpcServer := ethrpc.NewServer()
rpcAPIArr := config.JSONRPC.API
apis := rpc.GetRPCAPIs(ctx, clientCtx, tmWsClient, rpcAPIArr)
for _, api := range apis {
if err := rpcServer.RegisterName(api.Namespace, api.Service); err != nil {
ctx.Logger.Error(
"failed to register service in JSON RPC namespace",
"namespace", api.Namespace,
"service", api.Service,
)
return nil, nil, err
}
}
r := mux.NewRouter()
r.HandleFunc("/", rpcServer.ServeHTTP).Methods("POST")
handlerWithCors := cors.Default()
if config.API.EnableUnsafeCORS {
handlerWithCors = cors.AllowAll()
}
httpSrv := &http.Server{
Addr: config.JSONRPC.Address,
Handler: handlerWithCors.Handler(r),
}
httpSrvDone := make(chan struct{}, 1)
errCh := make(chan error)
go func() {
ctx.Logger.Info("Starting JSON-RPC server", "address", config.JSONRPC.Address)
if err := httpSrv.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
close(httpSrvDone)
return
}
ctx.Logger.Error("failed to start JSON-RPC server", "error", err.Error())
errCh <- err
}
}()
select {
case err := <-errCh:
ctx.Logger.Error("failed to boot JSON-RPC server", "error", err.Error())
return nil, nil, err
case <-time.After(types.ServerStartTime): // assume JSON RPC server started successfully
}
ctx.Logger.Info("Starting JSON WebSocket server", "address", config.JSONRPC.WsAddress)
// allocate separate WS connection to Tendermint
tmWsClient = ConnectTmWS(tmRPCAddr, tmEndpoint, ctx.Logger)
wsSrv := rpc.NewWebsocketsServer(ctx.Logger, tmWsClient, config)
wsSrv.Start()
return httpSrv, httpSrvDone, nil
}