diff options
author | Péter Szilágyi <peterke@gmail.com> | 2016-02-05 19:45:36 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2016-02-05 19:45:36 +0800 |
commit | a13bc9d7a1bc96fab93ace40045c0f0fea4da836 (patch) | |
tree | c6bd5be2e3e1ce06450a8611b2f1253b2ac18351 /rpc/http.go | |
parent | ba7c125153ce1be30985784a18edf38645406d03 (diff) | |
download | go-tangerine-a13bc9d7a1bc96fab93ace40045c0f0fea4da836.tar go-tangerine-a13bc9d7a1bc96fab93ace40045c0f0fea4da836.tar.gz go-tangerine-a13bc9d7a1bc96fab93ace40045c0f0fea4da836.tar.bz2 go-tangerine-a13bc9d7a1bc96fab93ace40045c0f0fea4da836.tar.lz go-tangerine-a13bc9d7a1bc96fab93ace40045c0f0fea4da836.tar.xz go-tangerine-a13bc9d7a1bc96fab93ace40045c0f0fea4da836.tar.zst go-tangerine-a13bc9d7a1bc96fab93ace40045c0f0fea4da836.zip |
cmd, common, node, rpc: move HTTP RPC into node, drop singletone aspect
Diffstat (limited to 'rpc/http.go')
-rw-r--r-- | rpc/http.go | 61 |
1 files changed, 7 insertions, 54 deletions
diff --git a/rpc/http.go b/rpc/http.go index c5eb41af1..e58a88c08 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. |