-
Notifications
You must be signed in to change notification settings - Fork 39
/
redeo.go
166 lines (138 loc) · 4.35 KB
/
redeo.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package redeo
import (
"errors"
"strings"
"github.com/bsm/redeo/v2/resp"
)
// UnknownCommand returns an unknown command error string
func UnknownCommand(cmd string) string {
return "ERR unknown command '" + cmd + "'"
}
// ErrUnknownCommand returns an unknown command error
func ErrUnknownCommand(cmd string) error {
return errors.New(UnknownCommand(cmd))
}
// WrongNumberOfArgs returns an unknown command error string
func WrongNumberOfArgs(cmd string) string {
return "ERR wrong number of arguments for '" + cmd + "' command"
}
// ErrWrongNumberOfArgs returns an unknown command error
func ErrWrongNumberOfArgs(cmd string) error {
return errors.New(WrongNumberOfArgs(cmd))
}
// Ping returns a ping handler.
// https://redis.io/commands/ping
func Ping() Handler {
return HandlerFunc(func(w resp.ResponseWriter, c *resp.Command) {
switch c.ArgN() {
case 0:
w.AppendInlineString("PONG")
case 1:
w.AppendBulk(c.Arg(0))
default:
w.AppendError(WrongNumberOfArgs(c.Name))
}
})
}
// Echo returns an echo handler.
// https://redis.io/commands/echo
func Echo() Handler {
return HandlerFunc(func(w resp.ResponseWriter, c *resp.Command) {
switch c.ArgN() {
case 1:
w.AppendBulk(c.Arg(0))
default:
w.AppendError(WrongNumberOfArgs(c.Name))
}
})
}
// Info returns an info handler.
// https://redis.io/commands/info
func Info(s *Server) Handler {
return HandlerFunc(func(w resp.ResponseWriter, c *resp.Command) {
info := s.Info()
resp := ""
if c.ArgN() == 1 {
resp = info.Find(c.Args[0].String()).String()
} else {
resp = info.String()
}
w.AppendBulkString(resp)
})
}
// CommandDescriptions returns a command handler.
// https://redis.io/commands/command
type CommandDescriptions []CommandDescription
func (s CommandDescriptions) ServeRedeo(w resp.ResponseWriter, c *resp.Command) {
w.AppendArrayLen(len(s))
for _, cmd := range s {
w.AppendArrayLen(6)
w.AppendBulkString(strings.ToLower(cmd.Name))
w.AppendInt(cmd.Arity)
w.AppendArrayLen(len(cmd.Flags))
for _, flag := range cmd.Flags {
w.AppendBulkString(flag)
}
w.AppendInt(cmd.FirstKey)
w.AppendInt(cmd.LastKey)
w.AppendInt(cmd.KeyStepCount)
}
}
// SubCommands returns a handler that is parsing sub-commands
type SubCommands map[string]Handler
func (s SubCommands) ServeRedeo(w resp.ResponseWriter, c *resp.Command) {
// First, check if we have a subcommand
if c.ArgN() == 0 {
w.AppendError(WrongNumberOfArgs(c.Name))
return
}
firstArg := c.Arg(0).String()
if h, ok := s[strings.ToLower(firstArg)]; ok {
cmd := resp.NewCommand(c.Name+" "+firstArg, c.Args[1:]...)
cmd.SetContext(c.Context())
h.ServeRedeo(w, cmd)
return
}
w.AppendError("ERR Unknown " + strings.ToLower(c.Name) + " subcommand '" + firstArg + "'")
}
// --------------------------------------------------------------------
// Handler is an abstract handler interface for responding to commands
type Handler interface {
// ServeRedeo serves a request.
ServeRedeo(w resp.ResponseWriter, c *resp.Command)
}
// HandlerFunc is a callback function, implementing Handler.
type HandlerFunc func(w resp.ResponseWriter, c *resp.Command)
// ServeRedeo calls f(w, c).
func (f HandlerFunc) ServeRedeo(w resp.ResponseWriter, c *resp.Command) { f(w, c) }
// WrapperFunc implements Handler, accepts a command and must return one of
// the following types:
//
// nil
// error
// string
// []byte
// bool
// float32, float64
// int, int8, int16, int32, int64
// uint, uint8, uint16, uint32, uint64
// resp.CustomResponse instances
// slices of any of the above typs
// maps containing keys and values of any of the above types
type WrapperFunc func(c *resp.Command) interface{}
// ServeRedeo implements Handler
func (f WrapperFunc) ServeRedeo(w resp.ResponseWriter, c *resp.Command) {
if err := w.Append(f(c)); err != nil {
w.AppendError("ERR " + err.Error())
}
}
// --------------------------------------------------------------------
// StreamHandler is an interface for responding to streaming commands
type StreamHandler interface {
// ServeRedeoStream serves a streaming request.
ServeRedeoStream(w resp.ResponseWriter, c *resp.CommandStream)
}
// StreamHandlerFunc is a callback function, implementing Handler.
type StreamHandlerFunc func(w resp.ResponseWriter, c *resp.CommandStream)
// ServeRedeoStream calls f(w, c).
func (f StreamHandlerFunc) ServeRedeoStream(w resp.ResponseWriter, c *resp.CommandStream) { f(w, c) }