Created
April 11, 2014 02:09
-
-
Save dbehnke/10437286 to your computer and use it in GitHub Desktop.
JSON RPC with Go and Gorilla
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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