42 lines
1.4 KiB
Go
42 lines
1.4 KiB
Go
|
package rpc
|
||
|
|
||
|
import (
|
||
|
"net"
|
||
|
|
||
|
"github.com/ethereum/go-ethereum/rpc"
|
||
|
log "github.com/sirupsen/logrus"
|
||
|
"github.com/vulcanize/ipld-eth-server/pkg/prom"
|
||
|
)
|
||
|
|
||
|
// StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules.
|
||
|
func StartHTTPEndpoint(endpoint string, apis []rpc.API, modules []string, cors []string, vhosts []string, timeouts rpc.HTTPTimeouts) (net.Listener, *rpc.Server, error) {
|
||
|
if bad, available := checkModuleAvailability(modules, apis); len(bad) > 0 {
|
||
|
log.Error("Unavailable modules in HTTP API list", "unavailable", bad, "available", available)
|
||
|
}
|
||
|
// Generate the whitelist based on the allowed modules
|
||
|
whitelist := make(map[string]bool)
|
||
|
for _, module := range modules {
|
||
|
whitelist[module] = true
|
||
|
}
|
||
|
// Register all the APIs exposed by the services
|
||
|
handler := rpc.NewServer()
|
||
|
for _, api := range apis {
|
||
|
if whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
|
||
|
if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
|
||
|
return nil, nil, err
|
||
|
}
|
||
|
log.Debug("HTTP registered", "namespace", api.Namespace)
|
||
|
}
|
||
|
}
|
||
|
// All APIs registered, start the HTTP listener
|
||
|
var (
|
||
|
listener net.Listener
|
||
|
err error
|
||
|
)
|
||
|
if listener, err = net.Listen("tcp", endpoint); err != nil {
|
||
|
return nil, nil, err
|
||
|
}
|
||
|
go rpc.NewHTTPServer(cors, vhosts, timeouts, prom.HTTPMiddleware(handler)).Serve(listener)
|
||
|
return listener, handler, err
|
||
|
}
|