aboutsummaryrefslogtreecommitdiffstats
path: root/node/api.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2016-02-13 20:53:48 +0800
committerPéter Szilágyi <peterke@gmail.com>2016-02-13 20:53:48 +0800
commit770b29fd80b8f25be31c2db5af6904cc5d0688ef (patch)
tree7b9da6bc13f6e881dd3a4ae231e9c69b6d9bbfeb /node/api.go
parentb05e472c076d30035233d6a8b5fb3360b236e3ff (diff)
parentdf75dbfd6804923b1c8a8388b67523072d59f155 (diff)
downloadgo-tangerine-770b29fd80b8f25be31c2db5af6904cc5d0688ef.tar
go-tangerine-770b29fd80b8f25be31c2db5af6904cc5d0688ef.tar.gz
go-tangerine-770b29fd80b8f25be31c2db5af6904cc5d0688ef.tar.bz2
go-tangerine-770b29fd80b8f25be31c2db5af6904cc5d0688ef.tar.lz
go-tangerine-770b29fd80b8f25be31c2db5af6904cc5d0688ef.tar.xz
go-tangerine-770b29fd80b8f25be31c2db5af6904cc5d0688ef.tar.zst
go-tangerine-770b29fd80b8f25be31c2db5af6904cc5d0688ef.zip
Merge pull request #2175 from karalabe/refactor-http-rpc
cmd, common, node, rpc: move HTTP RPC into node, drop singleton aspect
Diffstat (limited to 'node/api.go')
-rw-r--r--node/api.go95
1 files changed, 32 insertions, 63 deletions
diff --git a/node/api.go b/node/api.go
index bc1795407..48cbd0150 100644
--- a/node/api.go
+++ b/node/api.go
@@ -25,10 +25,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover"
- "github.com/ethereum/go-ethereum/rpc"
"github.com/rcrowley/go-metrics"
-
- "gopkg.in/fatih/set.v0"
)
// PrivateAdminAPI is the collection of administrative API methods exposed only
@@ -61,83 +58,55 @@ func (api *PrivateAdminAPI) AddPeer(url string) (bool, error) {
}
// StartRPC starts the HTTP RPC API server.
-func (api *PrivateAdminAPI) StartRPC(address string, port int, cors string, apis string) (bool, error) {
- var offeredAPIs []rpc.API
- if len(apis) > 0 {
- namespaces := set.New()
- for _, a := range strings.Split(apis, ",") {
- namespaces.Add(strings.TrimSpace(a))
- }
- for _, api := range api.node.APIs() {
- if namespaces.Has(api.Namespace) {
- offeredAPIs = append(offeredAPIs, api)
- }
- }
- } else { // use by default all public API's
- for _, api := range api.node.APIs() {
- if api.Public {
- offeredAPIs = append(offeredAPIs, api)
- }
- }
- }
+func (api *PrivateAdminAPI) StartRPC(host string, port int, cors string, apis string) (bool, error) {
+ api.node.lock.Lock()
+ defer api.node.lock.Unlock()
- if address == "" {
- address = "127.0.0.1"
+ if api.node.httpHandler != nil {
+ return false, fmt.Errorf("HTTP RPC already running on %s", api.node.httpEndpoint)
}
- if port == 0 {
- port = 8545
+ if err := api.node.startHTTP(fmt.Sprintf("%s:%d", host, port), api.node.rpcAPIs, strings.Split(apis, ","), cors); err != nil {
+ return false, err
}
-
- corsDomains := strings.Split(cors, " ")
- err := rpc.StartHTTP(address, port, corsDomains, offeredAPIs)
- return err == nil, err
+ return true, nil
}
// StopRPC terminates an already running HTTP RPC API endpoint.
func (api *PrivateAdminAPI) StopRPC() (bool, error) {
- err := rpc.StopHTTP()
- return err == nil, err
+ api.node.lock.Lock()
+ defer api.node.lock.Unlock()
+
+ if api.node.httpHandler == nil {
+ return false, fmt.Errorf("HTTP RPC not running")
+ }
+ api.node.stopHTTP()
+ return true, nil
}
// StartWS starts the websocket RPC API server.
-func (api *PrivateAdminAPI) StartWS(address string, port int, cors string, apis string) (bool, error) {
- var offeredAPIs []rpc.API
- if len(apis) > 0 {
- namespaces := set.New()
- for _, a := range strings.Split(apis, ",") {
- namespaces.Add(strings.TrimSpace(a))
- }
- for _, api := range api.node.APIs() {
- if namespaces.Has(api.Namespace) {
- offeredAPIs = append(offeredAPIs, api)
- }
- }
- } else {
- // use by default all public API's
- for _, api := range api.node.APIs() {
- if api.Public {
- offeredAPIs = append(offeredAPIs, api)
- }
- }
- }
+func (api *PrivateAdminAPI) StartWS(host string, port int, cors string, apis string) (bool, error) {
+ api.node.lock.Lock()
+ defer api.node.lock.Unlock()
- if address == "" {
- address = "127.0.0.1"
+ if api.node.wsHandler != nil {
+ return false, fmt.Errorf("WebSocket RPC already running on %s", api.node.wsEndpoint)
}
- if port == 0 {
- port = 8546
+ if err := api.node.startWS(fmt.Sprintf("%s:%d", host, port), api.node.rpcAPIs, strings.Split(apis, ","), cors); err != nil {
+ return false, err
}
-
- corsDomains := strings.Split(cors, " ")
-
- err := rpc.StartWS(address, port, corsDomains, offeredAPIs)
- return err == nil, err
+ return true, nil
}
// StopRPC terminates an already running websocket RPC API endpoint.
func (api *PrivateAdminAPI) StopWS() (bool, error) {
- err := rpc.StopWS()
- return err == nil, err
+ api.node.lock.Lock()
+ defer api.node.lock.Unlock()
+
+ if api.node.wsHandler == nil {
+ return false, fmt.Errorf("WebSocket RPC not running")
+ }
+ api.node.stopWS()
+ return true, nil
}
// PublicAdminAPI is the collection of administrative API methods exposed over