-
Notifications
You must be signed in to change notification settings - Fork 1
/
endpoint.go
208 lines (192 loc) · 5.03 KB
/
endpoint.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package service
import (
"bytes"
"github.com/shisa-platform/core/httpx"
)
// Endpoint is collection of pipelines for a route (URL path),
// one for each HTTP method. Only supported methods should have
// pipelines, but at least one pipleline is requried.
type Endpoint struct {
Route string // the absolute URL path for this endpoint
Head *Pipeline // HEAD method pipeline
Get *Pipeline // GET method pipeline
Put *Pipeline // PUT method pipeline
Post *Pipeline // POST method pipeline
Patch *Pipeline // PATCH method pipeline
Delete *Pipeline // DELETE method pipeline
Connect *Pipeline // CONNECT method pipeline
Options *Pipeline // OPTIONS method pipeline
Trace *Pipeline // TRACE method pipeline
}
// GetEndpoint returns an Endpoint configured for the given route
// with the GET pipeline using the given handlers.
func GetEndpoint(route string, handlers ...httpx.Handler) Endpoint {
return Endpoint{
Route: route,
Get: &Pipeline{
Handlers: handlers,
},
}
}
// GetEndpointWithPolicy returns an Endpoint configured for the
// given route, with the given policy and the GET pipeline using
// the given handlers.
func GetEndpointWithPolicy(route string, policy Policy, handlers ...httpx.Handler) Endpoint {
return Endpoint{
Route: route,
Get: &Pipeline{
Policy: policy,
Handlers: handlers,
},
}
}
// PutEndpoint returns an Endpoint configured for the given route
// with the PUT pipeline using the given handlers.
func PutEndpoint(route string, handlers ...httpx.Handler) Endpoint {
return Endpoint{
Route: route,
Put: &Pipeline{
Handlers: handlers,
},
}
}
// PutEndpointWithPolicy returns an Endpoint configured for the
// given route, with the given policy and the PUT pipeline using
// the given handlers.
func PutEndpointWithPolicy(route string, policy Policy, handlers ...httpx.Handler) Endpoint {
return Endpoint{
Route: route,
Put: &Pipeline{
Policy: policy,
Handlers: handlers,
},
}
}
// PostEndpoint returns an Endpoint configured for the given route
// with the POST pipeline using the given handlers.
func PostEndpoint(route string, handlers ...httpx.Handler) Endpoint {
return Endpoint{
Route: route,
Post: &Pipeline{
Handlers: handlers,
},
}
}
// PostEndpointWithPolicy returns an Endpoint configured for the
// given route, with the given policy and the POST pipeline
// using the given handlers.
func PostEndpointWithPolicy(route string, policy Policy, handlers ...httpx.Handler) Endpoint {
return Endpoint{
Route: route,
Post: &Pipeline{
Policy: policy,
Handlers: handlers,
},
}
}
// PatchEndpoint returns an Endpoint configured for the given
// route with the PATCH pipeline using the given handlers.
func PatchEndpoint(route string, handlers ...httpx.Handler) Endpoint {
return Endpoint{
Route: route,
Patch: &Pipeline{
Handlers: handlers,
},
}
}
// PatchEndpointWithPolicy returns an Endpoint configured for the
// given route, with the given policy and the PATCH pipeline
// using the given handlers.
func PatchEndpointWithPolicy(route string, policy Policy, handlers ...httpx.Handler) Endpoint {
return Endpoint{
Route: route,
Patch: &Pipeline{
Policy: policy,
Handlers: handlers,
},
}
}
// DeleteEndpoint returns an Endpoint configured for the given
// route with the DELETE pipeline using the given handlers.
func DeleteEndpoint(route string, handlers ...httpx.Handler) Endpoint {
return Endpoint{
Route: route,
Delete: &Pipeline{
Handlers: handlers,
},
}
}
// DeleteEndpointWithPolicy returns an Endpoint configured for
// the given route, with the given policy and the DELETE
// pipeline using the given handlers.
func DeleteEndpointWithPolicy(route string, policy Policy, handlers ...httpx.Handler) Endpoint {
return Endpoint{
Route: route,
Delete: &Pipeline{
Policy: policy,
Handlers: handlers,
},
}
}
// String implements `expvar.Var.String`
func (e Endpoint) String() string {
var buf bytes.Buffer
var written bool
buf.WriteByte('{')
if e.Head != nil {
written = true
buf.WriteString("\"HEAD\":")
e.Head.jsonify(&buf)
}
if e.Get != nil {
comma(&written, &buf)
buf.WriteString("\"GET\":")
e.Get.jsonify(&buf)
}
if e.Put != nil {
comma(&written, &buf)
buf.WriteString("\"PUT\":")
e.Put.jsonify(&buf)
}
if e.Post != nil {
comma(&written, &buf)
buf.WriteString("\"POST\":")
e.Post.jsonify(&buf)
}
if e.Patch != nil {
comma(&written, &buf)
buf.WriteString("\"PATCH\":")
e.Patch.jsonify(&buf)
}
if e.Delete != nil {
comma(&written, &buf)
buf.WriteString("\"DELETE\":")
e.Delete.jsonify(&buf)
}
if e.Connect != nil {
comma(&written, &buf)
buf.WriteString("\"CONNECT\":")
e.Connect.jsonify(&buf)
}
if e.Options != nil {
comma(&written, &buf)
buf.WriteString("\"OPTIONS\":")
e.Options.jsonify(&buf)
}
if e.Trace != nil {
if written {
buf.WriteByte(',')
}
buf.WriteString("\"TRACE\":")
e.Trace.jsonify(&buf)
}
buf.WriteByte('}')
return buf.String()
}
func comma(rest *bool, buf *bytes.Buffer) {
if *rest {
buf.WriteByte(',')
} else {
*rest = true
}
}