forked from docker-archive/go-redis-server
-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler.go
51 lines (44 loc) · 1.03 KB
/
handler.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
package redis
import (
"reflect"
"strings"
)
type HandlerFn func(r *Request) (ReplyWriter, error)
func (srv *Server) RegisterFct(key string, f interface{}) error {
v := reflect.ValueOf(f)
handlerFn, err := srv.createHandlerFn(f, &v)
if err != nil {
return err
}
srv.Register(key, handlerFn)
return nil
}
func (srv *Server) Register(name string, fn HandlerFn) {
if srv.methods == nil {
srv.methods = make(map[string]HandlerFn)
}
if fn != nil {
Debugf("REGISTER: %s", strings.ToLower(name))
srv.methods[strings.ToLower(name)] = fn
}
}
func (srv *Server) Apply(r *Request) (ReplyWriter, error) {
srv.Lock()
defer srv.Unlock()
if srv == nil || srv.methods == nil {
Debugf("The method map is uninitialized")
return ErrMethodNotSupported, nil
}
fn, exists := srv.methods[strings.ToLower(r.Name)]
if !exists {
return ErrMethodNotSupported, nil
}
return fn(r)
}
func (srv *Server) ApplyString(r *Request) (string, error) {
reply, err := srv.Apply(r)
if err != nil {
return "", err
}
return ReplyToString(reply)
}