Browse Source

fix test race and update readme

pull/456/head
Ethan Buchman 9 years ago
parent
commit
7ea86f6506
2 changed files with 61 additions and 4 deletions
  1. +57
    -0
      README.md
  2. +4
    -4
      rpc_test.go

+ 57
- 0
README.md View File

@ -1,4 +1,61 @@
# go-rpc # go-rpc
[![CircleCI](https://circleci.com/gh/tendermint/go-rpc.svg?style=svg)](https://circleci.com/gh/tendermint/go-rpc)
HTTP RPC server supporting calls via uri params, jsonrpc, and jsonrpc over websockets HTTP RPC server supporting calls via uri params, jsonrpc, and jsonrpc over websockets
# How To
Define some types and routes:
```
// Define a type for results and register concrete versions with go-wire
type Result interface{}
type ResultStatus struct {
Value string
}
var _ = wire.RegisterInterface(
struct{ Result }{},
wire.ConcreteType{&ResultStatus{}, 0x1},
)
// Define some routes
var Routes = map[string]*rpcserver.RPCFunc{
"status": rpcserver.NewRPCFunc(StatusResult, "arg"),
}
// an rpc function
func StatusResult(v string) (Result, error) {
return &ResultStatus{v}, nil
}
```
Now start the server:
```
mux := http.NewServeMux()
rpcserver.RegisterRPCFuncs(mux, Routes)
wm := rpcserver.NewWebsocketManager(Routes, nil)
mux.HandleFunc("/websocket", wm.WebsocketHandler)
go func() {
_, err := rpcserver.StartHTTPServer("0.0.0.0:46657", mux)
if err != nil {
panic(err)
}
}()
```
Note that unix sockets are supported as well (eg. `/path/to/socket` instead of `0.0.0.0:46657`)
Now see all available endpoints by sending a GET request to `0.0.0.0:46657`.
Each route is available as a GET request, as a JSONRPCv2 POST request, and via JSONRPCv2 over websockets
# Examples
* [Tendermint](https://github.com/tendermint/tendermint/blob/master/rpc/core/routes.go)
* [Network Monitor](https://github.com/tendermint/netmon/blob/master/handlers/routes.go)

+ 4
- 4
rpc_test.go View File

@ -54,12 +54,12 @@ func init() {
} }
}() }()
mux = http.NewServeMux()
rpcserver.RegisterRPCFuncs(mux, Routes)
mux2 := http.NewServeMux()
rpcserver.RegisterRPCFuncs(mux2, Routes)
wm = rpcserver.NewWebsocketManager(Routes, nil) wm = rpcserver.NewWebsocketManager(Routes, nil)
mux.HandleFunc(websocketEndpoint, wm.WebsocketHandler)
mux2.HandleFunc(websocketEndpoint, wm.WebsocketHandler)
go func() { go func() {
_, err := rpcserver.StartHTTPServer(unixAddr, mux)
_, err := rpcserver.StartHTTPServer(unixAddr, mux2)
if err != nil { if err != nil {
panic(err) panic(err)
} }


Loading…
Cancel
Save