forked from jason-xxl/rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.go
50 lines (41 loc) · 1.11 KB
/
counter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2009 The Go Authors. All rights reserved.
// Copyright 2013 The Gorilla Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"log"
"net/http"
"github.com/agronomhidden/rpc/v2"
"github.com/agronomhidden/rpc/v2/json2"
)
type Counter struct {
Count int
}
type IncrReq struct {
Delta int
}
// Notification.
func (c *Counter) Incr(r *http.Request, req *IncrReq, res *json2.EmptyResponse) error {
log.Printf("<- Incr %+v", *req)
c.Count += req.Delta
return nil
}
type GetReq struct {
}
func (c *Counter) Get(r *http.Request, req *GetReq, res *Counter) error {
log.Printf("<- Get %+v", *req)
*res = *c
log.Printf("-> %v", *res)
return nil
}
func main() {
address := flag.String("address", ":65534", "")
s := rpc.NewServer()
s.RegisterCodec(json2.NewCustomCodec(&rpc.CompressionSelector{}), "application/json")
s.RegisterService(new(Counter), "")
http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("./"))))
http.Handle("/jsonrpc/", s)
log.Fatal(http.ListenAndServe(*address, nil))
}