aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/http.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2015-03-10 06:00:27 +0800
committerFelix Lange <fjl@twurst.com>2015-03-10 06:08:46 +0800
commita11f1d6a7ec2eaa1a348776072c49019368a5ef3 (patch)
treecd2d7e9c3ade2ea02462436a908ca1caf9c06de3 /rpc/http.go
parent73d1ebe244644c2d74a1c0c38a3b339e72140886 (diff)
downloaddexon-a11f1d6a7ec2eaa1a348776072c49019368a5ef3.tar.gz
dexon-a11f1d6a7ec2eaa1a348776072c49019368a5ef3.tar.zst
dexon-a11f1d6a7ec2eaa1a348776072c49019368a5ef3.zip
rpc: add dataDir parameter and JSON-RPC handler
Diffstat (limited to 'rpc/http.go')
-rw-r--r--rpc/http.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/rpc/http.go b/rpc/http.go
new file mode 100644
index 000000000..44e2ad6ab
--- /dev/null
+++ b/rpc/http.go
@@ -0,0 +1,42 @@
+package rpc
+
+import (
+ "net/http"
+
+ "github.com/ethereum/go-ethereum/logger"
+ "github.com/ethereum/go-ethereum/xeth"
+)
+
+var rpchttplogger = logger.NewLogger("RPC-HTTP")
+
+// JSONRPC returns a handler that implements the Ethereum JSON-RPC API.
+func JSONRPC(pipe *xeth.XEth, dataDir string) http.Handler {
+ var json JsonWrapper
+ const jsonrpcver = "2.0"
+ api := NewEthereumApi(pipe, dataDir)
+
+ return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
+ w.Header().Set("Access-Control-Allow-Origin", "*")
+
+ rpchttplogger.DebugDetailln("Handling request")
+
+ reqParsed, reqerr := json.ParseRequestBody(req)
+ if reqerr != nil {
+ jsonerr := &RpcErrorObject{-32700, "Error: Could not parse request"}
+ json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr})
+ return
+ }
+
+ var response interface{}
+ reserr := api.GetRequestReply(&reqParsed, &response)
+ if reserr != nil {
+ rpchttplogger.Warnln(reserr)
+ jsonerr := &RpcErrorObject{-32603, reserr.Error()}
+ json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Error: jsonerr})
+ return
+ }
+
+ rpchttplogger.DebugDetailf("Generated response: %T %s", response, response)
+ json.Send(w, &RpcSuccessResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Result: response})
+ })
+}