Base implementation of the RPC endpoint
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
38
cmd/server/rpc/server.go
Normal file
38
cmd/server/rpc/server.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user