From 2f05a4573be0a4777c4448936e8520e0087f4bc5 Mon Sep 17 00:00:00 2001 From: William Petit Date: Mon, 24 Sep 2018 08:49:21 +0200 Subject: [PATCH] Base implementation of the RPC endpoint --- cmd/server/main.go | 10 +++++++++- cmd/server/rpc/server.go | 38 ++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 cmd/server/rpc/server.go diff --git a/cmd/server/main.go b/cmd/server/main.go index 452ee3f..44b626d 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -5,12 +5,14 @@ import ( "log" "net/http" + "forge.cadoles.com/Pyxis/orion/cmd/server/rpc" "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" ) var ( - conf = newDefaultConfig() + conf = newDefaultConfig() + jsonrpc = rpc.NewServer() ) func main() { @@ -26,6 +28,8 @@ func main() { r.Use(middleware.RealIP) r.Use(middleware.Logger) + r.Post("/rpc", handleRPC) + hostStr := fmt.Sprintf("%s:%s", conf.HTTPHost, conf.HTTPPort) log.Printf("listening on http://%s", hostStr) @@ -34,3 +38,7 @@ func main() { } } + +func handleRPC(w http.ResponseWriter, r *http.Request) { + jsonrpc.ServeHTTP(w, r) +} diff --git a/cmd/server/rpc/server.go b/cmd/server/rpc/server.go new file mode 100644 index 0000000..92673c3 --- /dev/null +++ b/cmd/server/rpc/server.go @@ -0,0 +1,38 @@ +package rpc + +import ( + "fmt" + "net/http" + + "github.com/gorilla/rpc" + "github.com/gorilla/rpc/json" +) + +// OrionService is the JSON-RPC API +type OrionService struct{} + +// HelloArgs is the arguments expected by the Hello method +type HelloArgs struct { + Who string +} + +// HelloResponse is the response returned from the Hello method +type HelloResponse struct { + Message string +} + +// Hello is a demo RPC method fro the OrionService +func (o *OrionService) Hello(r *http.Request, args *HelloArgs, reply *HelloResponse) error { + reply.Message = fmt.Sprintf("hello %s", args.Who) + return nil +} + +// NewServer returns a new configured JSON-RPC server +func NewServer() *rpc.Server { + server := rpc.NewServer() + server.RegisterCodec(json.NewCodec(), "application/json") + if err := server.RegisterService(new(OrionService), "Orion"); err != nil { + panic(err) + } + return server +} diff --git a/go.mod b/go.mod index 4989193..8490b43 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/cenkalti/backoff v2.0.0+incompatible // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-chi/chi v3.3.3+incompatible + github.com/gorilla/rpc v1.1.0 github.com/gorilla/websocket v1.4.0 // indirect github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1 github.com/miekg/dns v1.0.12 // indirect diff --git a/go.sum b/go.sum index 63a97cb..c0132ef 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-chi/chi v3.3.3+incompatible h1:KHkmBEMNkwKuK4FdQL7N2wOeB9jnIx7jR5wsuSBEFI8= github.com/go-chi/chi v3.3.3+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= +github.com/gorilla/rpc v1.1.0 h1:marKfvVP0Gpd/jHlVBKCQ8RAoUPdX7K1Nuh6l1BNh7A= +github.com/gorilla/rpc v1.1.0/go.mod h1:V4h9r+4sF5HnzqbwIez0fKSpANP0zlYd3qR7p36jkTQ= github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1 h1:VSELJSxQlpi1bz4ZwT+93hPpzNLRcgytLr77iVRJpcE=