Add new RPC helpers wrapping over regular rpc packages, add middleware chaining ability

This commit is contained in:
Harshavardhana
2015-07-01 00:37:43 -07:00
parent 188785a886
commit 701c3e5242
3 changed files with 83 additions and 39 deletions

View File

@@ -17,14 +17,35 @@
package rpc
import (
"net/http"
"github.com/gorilla/rpc/v2"
"github.com/gorilla/rpc/v2/json"
)
// HelloServiceHandler -
func HelloServiceHandler() *rpc.Server {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
// Server rpc server container
type Server struct {
RPCServer *rpc.Server
}
// RegisterJSONCodec - register standard json codec
func (s Server) RegisterJSONCodec() {
s.RPCServer.RegisterCodec(json.NewCodec(), "application/json")
}
// RegisterService - register new services
func (s Server) RegisterService(recv interface{}, name string) {
s.RPCServer.RegisterService(recv, name)
}
// NewServer - provide a new instance of RPC server
func NewServer() *Server {
s := &Server{}
s.RPCServer = rpc.NewServer()
return s
}
// ServeHTTP wrapper method for http.Handler interface
func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.RPCServer.ServeHTTP(w, r)
}