-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample_test.go
53 lines (44 loc) · 1.09 KB
/
example_test.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
51
52
53
package planb_test
import (
"github.com/bsm/planb"
"github.com/bsm/redeo"
"github.com/bsm/redeo/resp"
"github.com/hashicorp/raft"
)
func ExampleServer() {
// Open a store
store := planb.NewInmemStore()
// Init config
conf := planb.NewConfig()
conf.Sentinel.MasterName = "mymaster" // handle SENTINEL commands
// Init server
srv, err := planb.NewServer("10.0.0.1:7230", ".", store, raft.NewInmemStore(), raft.NewInmemStore(), conf)
if err != nil {
panic(err)
}
// Setup SET handler
srv.HandleRW("SET", nil, redeo.WrapperFunc(func(cmd *resp.Command) interface{} {
if len(cmd.Args) != 2 {
return redeo.ErrWrongNumberOfArgs(cmd.Name)
}
if err := store.Put(cmd.Args[0], cmd.Args[1]); err != nil {
return err
}
return "OK"
}))
// Setup GET handler
srv.HandleRO("GET", nil, redeo.WrapperFunc(func(cmd *resp.Command) interface{} {
if len(cmd.Args) != 1 {
return redeo.ErrWrongNumberOfArgs(cmd.Name)
}
val, err := store.Get(cmd.Args[0])
if err != nil {
return err
}
return val
}))
// Start serving
if err := srv.ListenAndServe(); err != nil {
panic(err)
}
}