-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction.go
More file actions
120 lines (98 loc) · 2.42 KB
/
action.go
File metadata and controls
120 lines (98 loc) · 2.42 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
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 box
import "net/http"
const HttpMethodAny = "*"
// An A stands for Action
type A struct {
Attr
HttpMethod string
// Name is the name to identify the action AND the invocation to url suffix
Name string
// Bound is true if this action is not an extended action
Bound bool
// Interceptors is the list of actions that will be executed before executing handler
Interceptors []I
resource *R
handler interface{}
}
func Action(handler interface{}) *A {
// TODO: introspect and change to "POST" if needed
return &A{
HttpMethod: "GET",
Attr: Attr{},
handler: handler,
Interceptors: []I{},
}
}
func ActionPost(handler interface{}) *A {
return &A{
HttpMethod: "POST",
Attr: Attr{},
handler: handler,
Interceptors: []I{},
}
}
func actionBound(handler interface{}, method string) *A {
a := Action(handler)
a.Bound = true
a.HttpMethod = method
return a
}
// Bind shortcuts:
func Get(handler interface{}) *A {
return actionBound(handler, http.MethodGet)
}
func Head(handler interface{}) *A {
return actionBound(handler, http.MethodHead)
}
func Post(handler interface{}) *A {
return actionBound(handler, http.MethodPost)
}
func Put(handler interface{}) *A {
return actionBound(handler, http.MethodPut)
}
func Patch(handler interface{}) *A {
return actionBound(handler, http.MethodPatch)
}
func Delete(handler interface{}) *A {
return actionBound(handler, http.MethodDelete)
}
func Connect(handler interface{}) *A {
return actionBound(handler, http.MethodConnect)
}
func Options(handler interface{}) *A {
return actionBound(handler, http.MethodOptions)
}
func Trace(handler interface{}) *A {
return actionBound(handler, http.MethodTrace)
}
func AnyMethod(handler interface{}) *A {
return actionBound(handler, HttpMethodAny)
}
// WithName overwrite default action name
func (a *A) WithName(name string) *A {
a.Name = name
return a
}
func (a *A) Bind(method string) *A {
a.HttpMethod = method
a.Bound = true
return a
}
func (a *A) WithAttribute(key string, value interface{}) *A {
a.SetAttribute(key, value)
return a
}
func (a *A) WithInterceptors(interceptor ...I) *A {
for _, i := range interceptor {
a.Interceptors = append(a.Interceptors, i)
}
return a
}
// Use is an alias of WithInterceptors
func (a *A) Use(interceptor ...I) *A {
return a.WithInterceptors(interceptor...)
}
// GetHandler overwrite default action name
func (a *A) GetHandler() any {
return a.handler
}