-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
52 lines (43 loc) · 1.18 KB
/
handler.go
File metadata and controls
52 lines (43 loc) · 1.18 KB
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
package brouter
import "net/http"
type handler struct {
Func func(w http.ResponseWriter, r *http.Request) (interface{}, error)
ErrorFunc func(w http.ResponseWriter, r *http.Request, err error)
SuccessFunc func(w http.ResponseWriter, r *http.Request, data interface{})
}
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
data, err := h.Func(w, r)
if err != nil {
h.ErrorFunc(w, r, err)
return
}
h.SuccessFunc(w, r, data)
}
func (h *handler) route(structRoute StructRoute) {
if structRoute.HandlerFunc != nil {
h.Func = structRoute.HandlerFunc
}
if structRoute.HandlerErrorFunc != nil {
h.ErrorFunc = structRoute.HandlerErrorFunc
}
if structRoute.HandlerSuccessFunc != nil {
h.SuccessFunc = structRoute.HandlerSuccessFunc
}
}
func (h *handler) setDefaultFunc(r *bRouter) {
if h.Func == nil {
h.Func = defaultHandlerFunc
}
if h.ErrorFunc == nil {
h.ErrorFunc = defaultHandlerErrorFunc
if r.defaultHandlerErrorFunc != nil {
h.ErrorFunc = r.defaultHandlerErrorFunc
}
}
if h.SuccessFunc == nil {
h.SuccessFunc = defaultHandlerSuccessFunc
if r.defaultHandlerSuccessFunc != nil {
h.SuccessFunc = r.defaultHandlerSuccessFunc
}
}
}