Skip to content

Instantly share code, notes, and snippets.

@anujb
Created January 14, 2013 06:54
Show Gist options
  • Save anujb/4528263 to your computer and use it in GitHub Desktop.
Save anujb/4528263 to your computer and use it in GitHub Desktop.
Gorilla json-rpc sample
package main
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() {
server := rpc.NewServer()
codec := json.NewCodec()
server.RegisterCodec(codec, "application/json")
server.RegisterCodec(codec, "application/json; charset=UTF-8") // For firefox 11 and other browsers which append the charset=UTF-8
server.RegisterService(new(HelloService), "")
http.Handle("/api", server)
http.ListenAndServe("127.0.0.1:8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment