Created
September 24, 2015 14:19
-
-
Save nicwest/dd014688600ba8d39bf6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
import ( | |
"github.com/gorilla/rpc/v2" | |
"github.com/gorilla/rpc/v2/json" | |
"log" | |
"net/http" | |
) | |
type MathArgs struct { | |
Numbers []int `json:"numbers"` | |
} | |
type MathReply struct { | |
Value int | |
} | |
type MathService struct{} | |
func (h *MathService) Sum(r *http.Request, args *MathArgs, reply *MathReply) error { | |
var v int | |
for _, num := range args.Numbers { | |
v = v + num | |
} | |
reply.Value = v | |
return nil | |
} | |
func (h *MathService) Subtract(r *http.Request, args *MathArgs, reply *MathReply) error { | |
v := args.Numbers[0] | |
for _, num := range args.Numbers[1:] { | |
v = v - num | |
} | |
reply.Value = v | |
return nil | |
} | |
func main() { | |
s := rpc.NewServer() | |
s.RegisterCodec(json.NewCodec(), "application/json") | |
s.RegisterService(new(MathService), "Math") | |
http.Handle("/rpc", s) | |
log.Fatal(http.ListenAndServe(":8888", nil)) | |
} |
This file contains hidden or 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
[ | |
{"jsonrpc": "2.0", "method": "Math.Sum", "params": [{"Numbers": [1,2,4]}], "id": "1"}, | |
{"foo": "boo"}, | |
{"jsonrpc": "2.0", "method": "Math.Subtract", "params": [{"Numbers": [42,23]}], "id": "2"} | |
] |
Author
nicwest
commented
Sep 24, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment