laconicd/server/rpc/rpc.go

39 lines
960 B
Go
Raw Normal View History

2018-08-09 10:36:47 +00:00
package rpc
import (
"context"
"fmt"
2018-09-28 21:40:58 +00:00
2018-08-09 10:36:47 +00:00
"github.com/ethereum/go-ethereum/rpc"
2018-08-09 10:36:47 +00:00
)
2018-08-09 10:36:47 +00:00
2018-09-28 21:40:58 +00:00
// StartHTTPEndpoint starts the Tendermint Web3-compatible RPC layer. Consumes
// a Context for cancellation, a config struct, and a list of rpc.API interfaces
// that will be automatically wired into a JSON-RPC webserver.
func StartHTTPEndpoint(ctx context.Context, config *Config, apis []rpc.API, timeouts rpc.HTTPTimeouts) (*rpc.Server, error) {
2018-08-09 10:36:47 +00:00
uniqModules := make(map[string]string)
for _, api := range apis {
uniqModules[api.Namespace] = api.Namespace
}
2018-09-28 21:40:58 +00:00
2018-08-09 10:36:47 +00:00
modules := make([]string, len(uniqModules))
i := 0
for k := range uniqModules {
modules[i] = k
i++
}
endpoint := fmt.Sprintf("%s:%d", config.RPCAddr, config.RPCPort)
2018-09-28 21:40:58 +00:00
_, server, err := rpc.StartHTTPEndpoint(
endpoint, apis, modules, config.RPCCORSDomains, config.RPCVHosts, timeouts,
)
2018-08-09 10:36:47 +00:00
go func() {
<-ctx.Done()
fmt.Println("Shutting down server.")
server.Stop()
}()
return server, err
}