Register blockchain.* handlers in jsonrpc_service.go.
This commit is contained in:
parent
20e32437e9
commit
8c8871b4d2
|
@ -12,6 +12,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gorilla/rpc"
|
||||||
"github.com/lbryio/herald.go/db"
|
"github.com/lbryio/herald.go/db"
|
||||||
"github.com/lbryio/herald.go/internal"
|
"github.com/lbryio/herald.go/internal"
|
||||||
"github.com/lbryio/lbcd/chaincfg"
|
"github.com/lbryio/lbcd/chaincfg"
|
||||||
|
@ -22,35 +23,22 @@ import (
|
||||||
"golang.org/x/exp/constraints"
|
"golang.org/x/exp/constraints"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: import this from gorilla/rpc
|
|
||||||
// Codec creates a CodecRequest to process each request.
|
|
||||||
type Codec interface {
|
|
||||||
NewRequest(*http.Request) CodecRequest
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: import this from gorilla/rpc
|
|
||||||
// CodecRequest decodes a request and encodes a response using a specific
|
|
||||||
// serialization scheme.
|
|
||||||
type CodecRequest interface {
|
|
||||||
// Reads request and returns the RPC method name.
|
|
||||||
Method() (string, error)
|
|
||||||
// Reads request filling the RPC method args.
|
|
||||||
ReadRequest(interface{}) error
|
|
||||||
// Writes response using the RPC method reply. The error parameter is
|
|
||||||
// the error returned by the method call, if any.
|
|
||||||
WriteResponse(http.ResponseWriter, interface{}, error) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type BlockchainCodec struct {
|
type BlockchainCodec struct {
|
||||||
Codec
|
rpc.Codec
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *BlockchainCodec) NewRequest(r *http.Request) CodecRequest {
|
func (c *BlockchainCodec) NewRequest(r *http.Request) rpc.CodecRequest {
|
||||||
return &BlockchainCodecRequest{c.Codec.NewRequest(r)}
|
return &BlockchainCodecRequest{c.Codec.NewRequest(r)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BlockchainCodecRequest provides ability to rewrite the incoming
|
||||||
|
// request "method" field. For example:
|
||||||
|
// blockchain.block.get_header -> blockchain_block.Get_header
|
||||||
|
// blockchain.address.listunspent -> blockchain_address.Listunspent
|
||||||
|
// This makes the "method" string compatible with Gorilla/RPC
|
||||||
|
// requirements.
|
||||||
type BlockchainCodecRequest struct {
|
type BlockchainCodecRequest struct {
|
||||||
CodecRequest
|
rpc.CodecRequest
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cr *BlockchainCodecRequest) Method() (string, error) {
|
func (cr *BlockchainCodecRequest) Method() (string, error) {
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
pb "github.com/lbryio/herald.go/protobuf/go"
|
pb "github.com/lbryio/herald.go/protobuf/go"
|
||||||
)
|
)
|
||||||
|
|
||||||
type JSONServer struct {
|
type ClaimtrieService struct {
|
||||||
DB *db.ReadOnlyDBColumnFamily
|
DB *db.ReadOnlyDBColumnFamily
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ type Result struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve is the json rpc endpoint for resolve
|
// Resolve is the json rpc endpoint for resolve
|
||||||
func (t *JSONServer) Resolve(r *http.Request, args *ResolveData, result **pb.Outputs) error {
|
func (t *ClaimtrieService) Resolve(r *http.Request, args *ResolveData, result **pb.Outputs) error {
|
||||||
log.Println("Resolve")
|
log.Println("Resolve")
|
||||||
res, err := InternalResolve(args.Data, t.DB)
|
res, err := InternalResolve(args.Data, t.DB)
|
||||||
*result = res
|
*result = res
|
||||||
|
@ -33,14 +33,21 @@ func (t *JSONServer) Resolve(r *http.Request, args *ResolveData, result **pb.Out
|
||||||
|
|
||||||
// StartJsonRPC starts the json rpc server and registers the endpoints.
|
// StartJsonRPC starts the json rpc server and registers the endpoints.
|
||||||
func (s *Server) StartJsonRPC() error {
|
func (s *Server) StartJsonRPC() error {
|
||||||
server := new(JSONServer)
|
|
||||||
server.DB = s.DB
|
|
||||||
|
|
||||||
port := ":" + s.Args.JSONRPCPort
|
port := ":" + s.Args.JSONRPCPort
|
||||||
|
|
||||||
s1 := rpc.NewServer() // Create a new RPC server
|
s1 := rpc.NewServer() // Create a new RPC server
|
||||||
s1.RegisterCodec(json.NewCodec(), "application/json") // Register the type of data requested as JSON
|
// Register the type of data requested as JSON, with custom codec.
|
||||||
s1.RegisterService(server, "") // Register the service by creating a new JSON server
|
s1.RegisterCodec(&BlockchainCodec{json.NewCodec()}, "application/json")
|
||||||
|
|
||||||
|
// Register "blockchain.claimtrie.*"" handlers.
|
||||||
|
claimtrieSvc := &ClaimtrieService{s.DB}
|
||||||
|
s1.RegisterService(claimtrieSvc, "blockchain_claimtrie")
|
||||||
|
|
||||||
|
// Register other "blockchain.{block,address,scripthash}.*" handlers.
|
||||||
|
blockchainSvc := &BlockchainService{s.DB, s.Chain}
|
||||||
|
s1.RegisterService(&blockchainSvc, "blockchain_block")
|
||||||
|
s1.RegisterService(&BlockchainAddressService{*blockchainSvc}, "blockchain_address")
|
||||||
|
s1.RegisterService(&BlockchainScripthashService{*blockchainSvc}, "blockchain_scripthash")
|
||||||
|
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
r.Handle("/rpc", s1)
|
r.Handle("/rpc", s1)
|
||||||
|
|
Loading…
Reference in a new issue