rpc: add dataDir parameter and JSON-RPC handler
This commit is contained in:
@ -11,6 +11,7 @@ package rpc
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"path"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@ -55,8 +56,8 @@ type EthereumApi struct {
|
||||
defaultBlockAge int64
|
||||
}
|
||||
|
||||
func NewEthereumApi(eth *xeth.XEth) *EthereumApi {
|
||||
db, _ := ethdb.NewLDBDatabase("dapps")
|
||||
func NewEthereumApi(eth *xeth.XEth, dataDir string) *EthereumApi {
|
||||
db, _ := ethdb.NewLDBDatabase(path.Join(dataDir, "dapps"))
|
||||
api := &EthereumApi{
|
||||
eth: eth,
|
||||
mux: eth.Backend().EventMux(),
|
||||
|
42
rpc/http.go
Normal file
42
rpc/http.go
Normal file
@ -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})
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user