erigon-pulse/cmd/rpcdaemon/commands/daemon.go

78 lines
2.0 KiB
Go
Raw Normal View History

package commands
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"github.com/ledgerwatch/turbo-geth/log"
"github.com/ledgerwatch/turbo-geth/rpc"
)
// splitAndTrim splits input separated by a comma
// and trims excessive white space from the substrings.
func splitAndTrim(input string) []string {
result := strings.Split(input, ",")
for i, r := range result {
result[i] = strings.TrimSpace(r)
}
return result
}
// EthAPI is a collection of functions that are exposed in the
type EthAPI interface {
BlockNumber(ctx context.Context) (uint64, error)
}
2019-11-29 08:13:48 +00:00
// EthAPIImpl is implementation of the EthAPI interface based on remote Db access
type EthAPIImpl struct {
remoteDbAdddress string
}
// BlockNumber returns the currently highest block number available in the remote db
2019-11-29 08:13:48 +00:00
func (api *EthAPIImpl) BlockNumber(ctx context.Context) (uint64, error) {
return 0, nil
}
2019-11-29 08:13:48 +00:00
func daemon(config Config) {
vhosts := splitAndTrim(cfg.rpcVirtualHost)
cors := splitAndTrim(cfg.rpcCORSDomain)
enabledApis := splitAndTrim(cfg.rpcAPI)
var rpcAPI = []rpc.API{}
for _, enabledAPI := range enabledApis {
switch enabledAPI {
case "eth":
2019-11-29 08:13:48 +00:00
var ethAPI EthAPI = &EthAPIImpl{remoteDbAdddress: cfg.remoteDbAdddress}
rpcAPI = append(rpcAPI, rpc.API{
Namespace: "eth",
Public: true,
2019-11-29 08:13:48 +00:00
Service: ethAPI,
Version: "1.0",
})
default:
log.Error("Unrecognised", "api", enabledAPI)
}
}
httpEndpoint := fmt.Sprintf("%s:%d", cfg.rpcListenAddress, cfg.rpcPort)
2019-11-29 08:13:48 +00:00
listener, _, err := rpc.StartHTTPEndpoint(httpEndpoint, rpcAPI, []string{"test", "eth", "debug", "web3"}, cors, vhosts, rpc.DefaultHTTPTimeouts)
if err != nil {
log.Error("Could not start RPC api", "error", err)
return
}
extapiURL := fmt.Sprintf("http://%s", httpEndpoint)
log.Info("HTTP endpoint opened", "url", extapiURL)
defer func() {
listener.Close()
log.Info("HTTP endpoint closed", "url", httpEndpoint)
}()
2019-11-29 08:13:48 +00:00
abortChan := make(chan os.Signal)
signal.Notify(abortChan, os.Interrupt)
sig := <-abortChan
log.Info("Exiting...", "signal", sig)
}