diff options
author | Péter Szilágyi <peterke@gmail.com> | 2016-02-13 20:53:48 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2016-02-13 20:53:48 +0800 |
commit | 770b29fd80b8f25be31c2db5af6904cc5d0688ef (patch) | |
tree | 7b9da6bc13f6e881dd3a4ae231e9c69b6d9bbfeb /rpc/http.go | |
parent | b05e472c076d30035233d6a8b5fb3360b236e3ff (diff) | |
parent | df75dbfd6804923b1c8a8388b67523072d59f155 (diff) | |
download | go-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 'rpc/http.go')
-rw-r--r-- | rpc/http.go | 63 |
1 files changed, 8 insertions, 55 deletions
diff --git a/rpc/http.go b/rpc/http.go index c5eb41af1..d9053b003 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -20,7 +20,6 @@ import ( "bufio" "bytes" "encoding/json" - "errors" "fmt" "io" "io/ioutil" @@ -29,7 +28,6 @@ import ( "net/url" "strconv" "strings" - "sync" "time" "github.com/ethereum/go-ethereum/logger" @@ -41,12 +39,6 @@ const ( httpReadDeadLine = 60 * time.Second // wait max httpReadDeadeline for next request ) -var ( - httpServerMu sync.Mutex // prevent concurrent access to the httpListener and httpServer - httpListener net.Listener // listener for the http server - httpRPCServer *Server // the node can only start 1 HTTP RPC server instance -) - // httpMessageStream is the glue between a HTTP connection which is message based // and the RPC codecs that expect json requests to be read from a stream. It will // parse HTTP messages and offer the bodies of these requests as a stream through @@ -249,53 +241,14 @@ func (h *httpConnHijacker) ServeHTTP(w http.ResponseWriter, req *http.Request) { go h.rpcServer.ServeCodec(codec) } -// StartHTTP will start the JSONRPC HTTP RPC interface when its not yet running. -func StartHTTP(address string, port int, corsdomains []string, apis []API) error { - httpServerMu.Lock() - defer httpServerMu.Unlock() - - if httpRPCServer != nil { - return fmt.Errorf("HTTP RPC interface already started on %s", httpListener.Addr()) - } - - rpcServer := NewServer() - - for _, api := range apis { - if err := rpcServer.RegisterName(api.Namespace, api.Service); err != nil { - return err - } - } - - listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", address, port)) - if err != nil { - return err +// NewHTTPServer creates a new HTTP RPC server around an API provider. +func NewHTTPServer(cors string, handler *Server) *http.Server { + return &http.Server{ + Handler: &httpConnHijacker{ + corsdomains: strings.Split(cors, ","), + rpcServer: handler, + }, } - - httpServer := http.Server{Handler: &httpConnHijacker{corsdomains, rpcServer}} - go httpServer.Serve(listener) - - httpListener = listener - httpRPCServer = rpcServer - - return nil -} - -// StopHTTP will stop the running HTTP interface. If it is not running an error will be returned. -func StopHTTP() error { - httpServerMu.Lock() - defer httpServerMu.Unlock() - - if httpRPCServer == nil { - return errors.New("HTTP RPC interface not started") - } - - httpListener.Close() - httpRPCServer.Stop() - - httpRPCServer = nil - httpListener = nil - - return nil } // httpClient connects to a geth RPC server over HTTP. @@ -306,7 +259,7 @@ type httpClient struct { // NewHTTPClient create a new RPC clients that connection to a geth RPC server // over HTTP. -func NewHTTPClient(endpoint string) (*httpClient, error) { +func NewHTTPClient(endpoint string) (Client, error) { url, err := url.Parse(endpoint) if err != nil { return nil, err |