Skip to content

Instantly share code, notes, and snippets.

@dbehnke
Created April 11, 2014 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dbehnke/10437286 to your computer and use it in GitHub Desktop.
Save dbehnke/10437286 to your computer and use it in GitHub Desktop.
JSON RPC with Go and Gorilla
package main
/*test with curl
curl -X POST -H "Content-Type: application/json" \
-d '{"method":"HelloService.Say","params":[{"Who":"Test"}], "id":"1"}' \
http://localhost:10000/rpc
*/
import (
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
"net/http"
)
type HelloArgs struct {
Who string
}
type HelloReply struct {
Message string
}
type HelloService struct{}
func (h *HelloService) Say(r *http.Request, args *HelloArgs, reply *HelloReply) error {
reply.Message = "Hello, " + args.Who + "!"
return nil
}
func main() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
http.Handle("/rpc", s)
http.ListenAndServe(":10000", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment