Base implementation of the RPC endpoint

This commit is contained in:
wpetit 2018-09-24 08:49:21 +02:00
parent 030bb818e1
commit 2f05a4573b
4 changed files with 50 additions and 1 deletions

View File

@ -5,12 +5,14 @@ import (
"log" "log"
"net/http" "net/http"
"forge.cadoles.com/Pyxis/orion/cmd/server/rpc"
"github.com/go-chi/chi" "github.com/go-chi/chi"
"github.com/go-chi/chi/middleware" "github.com/go-chi/chi/middleware"
) )
var ( var (
conf = newDefaultConfig() conf = newDefaultConfig()
jsonrpc = rpc.NewServer()
) )
func main() { func main() {
@ -26,6 +28,8 @@ func main() {
r.Use(middleware.RealIP) r.Use(middleware.RealIP)
r.Use(middleware.Logger) r.Use(middleware.Logger)
r.Post("/rpc", handleRPC)
hostStr := fmt.Sprintf("%s:%s", conf.HTTPHost, conf.HTTPPort) hostStr := fmt.Sprintf("%s:%s", conf.HTTPHost, conf.HTTPPort)
log.Printf("listening on http://%s", hostStr) 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)
}

38
cmd/server/rpc/server.go Normal file
View File

@ -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
}

1
go.mod
View File

@ -6,6 +6,7 @@ require (
github.com/cenkalti/backoff v2.0.0+incompatible // indirect github.com/cenkalti/backoff v2.0.0+incompatible // indirect
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-chi/chi v3.3.3+incompatible 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/gorilla/websocket v1.4.0 // indirect
github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1 github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1
github.com/miekg/dns v1.0.12 // indirect github.com/miekg/dns v1.0.12 // indirect

2
go.sum
View File

@ -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/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 h1:KHkmBEMNkwKuK4FdQL7N2wOeB9jnIx7jR5wsuSBEFI8=
github.com/go-chi/chi v3.3.3+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= 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 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1 h1:VSELJSxQlpi1bz4ZwT+93hPpzNLRcgytLr77iVRJpcE= github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1 h1:VSELJSxQlpi1bz4ZwT+93hPpzNLRcgytLr77iVRJpcE=