-
-
Save rnix/fc03d74ec128cb6a3099 to your computer and use it in GitHub Desktop.
Gorilla RPC/v2 example
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
gorilla-go-json-rpc-test | |
gorilla-go-json-rpc-test.out |
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 ( | |
j "encoding/json" | |
"flag" | |
"fmt" | |
"github.com/gorilla/rpc/v2" | |
"github.com/gorilla/rpc/v2/json" | |
"io/ioutil" | |
"log" | |
"math/rand" | |
"net/http" | |
"strings" | |
"time" | |
) | |
type HelloArgs struct { | |
Who string | |
} | |
type HelloReply struct { | |
Message string | |
} | |
type HelloResponse struct { | |
Result HelloReply `json:"result"` | |
Error string `json:"error"` | |
Id string `json:"id"` | |
} | |
type HelloRequest struct { | |
Method string `json:"method"` | |
Params []HelloArgs `json:"params"` | |
Id string `json:"id"` | |
} | |
func init() { | |
rand.Seed(time.Now().UnixNano()) | |
} | |
func (h *HelloRequest) Say(p HelloArgs) *HelloRequest { | |
h.Method = "HelloService.Say" | |
h.Params = []HelloArgs{p} | |
h.Id = fmt.Sprintf("%v.%v", time.Now().UnixNano(), rand.Int63()) | |
return h | |
} | |
type HelloService struct{} | |
func (h *HelloService) Say(r *http.Request, args *HelloArgs, reply *HelloReply) error { | |
reply.Message = "Hello, " + args.Who + "!" | |
log.Printf("request: %v\nargs: %v\nreply: %v", r, args, reply) | |
return nil | |
} | |
func main() { | |
flag.Parse() | |
if len(flag.Args()) > 0 { | |
log.Printf("Calling RPC Server on :10000\n") | |
r := &HelloRequest{} | |
if b, err := j.Marshal(r.Say(HelloArgs{flag.Arg(0)})); err != nil { | |
log.Fatal(err) | |
} else { | |
log.Printf("json %s", b) | |
if res, err := http.Post("http://localhost:10000/rpc", "application/json", strings.NewReader(string(b))); err != nil { | |
log.Fatal(err) | |
} else { | |
log.Printf("res : %v", res) | |
hello, err := ioutil.ReadAll(res.Body) | |
res.Body.Close() | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Printf("raw %s", hello) | |
var data HelloResponse | |
if err := j.Unmarshal(hello, &data); err != nil { | |
log.Fatal(err) | |
} | |
log.Printf("parsed %+v", data) | |
} | |
} | |
} else { | |
log.Printf("Starting RPC Server on :10000\n") | |
s := rpc.NewServer() | |
s.RegisterCodec(json.NewCodec(), "application/json") | |
s.RegisterService(new(HelloService), "") | |
http.Handle("/rpc", s) | |
http.ListenAndServe("localhost:10000", nil) | |
} | |
} |
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
.PHONY: run get build | |
run: get | |
@./run.sh | |
get: | |
@go get github.com/gorilla/rpc | |
build: gorilla-go-json-rpc-test | |
gorilla-go-json-rpc-test: gorilla-go-json-rpc-test.go | |
go build gorilla-go-json-rpc-test.go | |
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
#!/bin/bash | |
set -o nounset | |
make build | |
[ -x gorilla-go-json-rpc-test ] || exit ${LINENO} | |
echo;echo;echo Starting RPC server. | |
./gorilla-go-json-rpc-test >gorilla-go-json-rpc-test.out 2>&1 & p=$! | |
trap "kill ${p}" 0 | |
echo;echo;echo Waiting a moment for it to accept requests. | |
sleep 1 | |
echo;echo;echo Call HelloService.Say | |
set -o verbose | |
curl -is -X POST \ | |
-H "Content-Type: application/json" \ | |
-d '{"method":"HelloService.Say","params":[{"Who":"Test"}], "id":"1"}' \ | |
http://localhost:10000/rpc | |
echo;set +o verbose | |
echo;echo;echo Server output. | |
cat gorilla-go-json-rpc-test.out | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment