erigon-pulse/cmd/rpcdaemon/commands/daemon.go
Alexey Akhunov 2026f74f04 Fix CLI
2019-12-03 09:07:13 +07:00

78 lines
2.0 KiB
Go

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)
}
// 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
func (api *EthAPIImpl) BlockNumber(ctx context.Context) (uint64, error) {
return 0, nil
}
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":
var ethAPI EthAPI = &EthAPIImpl{remoteDbAdddress: cfg.remoteDbAdddress}
rpcAPI = append(rpcAPI, rpc.API{
Namespace: "eth",
Public: true,
Service: ethAPI,
Version: "1.0",
})
default:
log.Error("Unrecognised", "api", enabledAPI)
}
}
httpEndpoint := fmt.Sprintf("%s:%d", cfg.rpcListenAddress, cfg.rpcPort)
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)
}()
abortChan := make(chan os.Signal)
signal.Notify(abortChan, os.Interrupt)
sig := <-abortChan
log.Info("Exiting...", "signal", sig)
}