forked from cerc-io/ipld-eth-server
58 lines
2.1 KiB
Go
58 lines
2.1 KiB
Go
// VulcanizeDB
|
|
// Copyright © 2020 Vulcanize
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
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
|
|
}
|