-
Notifications
You must be signed in to change notification settings - Fork 2
/
router_element_impl.go
120 lines (100 loc) · 3.31 KB
/
router_element_impl.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
package rapi
import (
"context"
"net/http"
)
func NewHandlerMethod[I, O any](f func(ctx context.Context, param *I) (*O, error)) HandlerMethod[I] {
return &handlerMethod[I, O]{
ServiceFunc: f,
}
}
type handlerMethod[I, O any] struct {
InputFunc func(ctx context.Context, r *http.Request, param any) error
AfterInputFunc func(ctx context.Context, r *http.Request, param *I) error
ValidateFunc func(ctx context.Context, param any) error
AfterValidateFunc func(ctx context.Context, param *I) error
ServiceFunc func(ctx context.Context, param *I) (*O, error)
BeforeHandleErrorFunc func(ctx context.Context, w http.ResponseWriter, r *http.Request, err error) error
HandleErrorFunc func(ctx context.Context, w http.ResponseWriter, r *http.Request, err error)
RenderFunc func(ctx context.Context, w http.ResponseWriter, r *http.Request, output any)
}
// --- handlerMethod implements ---
func (h *handlerMethod[I, O]) SetInputFunc(f func(ctx context.Context, r *http.Request, param any) error) {
h.InputFunc = f
}
func (h *handlerMethod[I, O]) SetValidateFunc(f func(ctx context.Context, param any) error) {
h.ValidateFunc = f
}
func (h *handlerMethod[I, O]) SetRenderFunc(f func(ctx context.Context, w http.ResponseWriter, r *http.Request, output any)) {
h.RenderFunc = f
}
func (h *handlerMethod[I, O]) SetHandleErrorFunc(f func(ctx context.Context, w http.ResponseWriter, r *http.Request, err error)) {
h.HandleErrorFunc = f
}
func (h *handlerMethod[I, O]) BeforeHandleError(f func(ctx context.Context, w http.ResponseWriter, r *http.Request, err error) error) {
h.BeforeHandleErrorFunc = f
}
func (h *handlerMethod[I, O]) AfterInput(f func(ctx context.Context, r *http.Request, param *I) error) {
h.AfterInputFunc = f
}
func (h *handlerMethod[I, O]) AfterValidate(f func(ctx context.Context, param *I) error) {
h.AfterValidateFunc = f
}
// --- RouterElement implements ---
func (h *handlerMethod[I, O]) handleError(ctx context.Context, w http.ResponseWriter, r *http.Request, err error) {
if h.BeforeHandleErrorFunc != nil {
err = h.BeforeHandleErrorFunc(ctx, w, r, err)
}
if h.HandleErrorFunc != nil {
h.HandleErrorFunc(ctx, w, r, err)
}
}
func (h *handlerMethod[I, O]) GetHandleFunc() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var param I
if h.InputFunc != nil {
if err := h.InputFunc(ctx, r, ¶m); err != nil {
h.handleError(ctx, w, r, err)
return
}
}
if h.AfterInputFunc != nil {
if err := h.AfterInputFunc(ctx, r, ¶m); err != nil {
h.handleError(ctx, w, r, err)
return
}
}
if h.ValidateFunc != nil {
if err := h.ValidateFunc(ctx, ¶m); err != nil {
h.handleError(ctx, w, r, err)
return
}
}
if h.AfterValidateFunc != nil {
if err := h.AfterValidateFunc(ctx, ¶m); err != nil {
h.handleError(ctx, w, r, err)
return
}
}
var output *O
var err error
if h.ServiceFunc != nil {
output, err = h.ServiceFunc(ctx, ¶m)
if err != nil {
h.handleError(ctx, w, r, err)
return
}
}
if h.RenderFunc == nil {
panic("RenderFunc is required")
}
h.RenderFunc(ctx, w, r, output)
}
}
func (h *handlerMethod[I, O]) GetEmptyInput() any {
return *new(I)
}
func (h *handlerMethod[I, O]) GetEmptyOutput() any {
return *new(O)
}