/
handler.go
374 lines (350 loc) · 12.3 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package http
import (
"context"
"encoding/json"
"errors"
http1 "net/http"
"strconv"
http "github.com/go-kit/kit/transport/http"
handlers "github.com/gorilla/handlers"
mux "github.com/gorilla/mux"
endpoint "github.com/money-hub/MoneyDodo.service/user/pkg/endpoint"
)
func getQueries(r *http1.Request) (error, int, int, int, string) {
page, offset, limit := 1, 0, -1
orderby := "id"
var err error
// 解析Queries
vals := r.URL.Query()
pages, ok := vals["page"]
if ok {
page, err = strconv.Atoi(pages[0])
if err != nil {
return err, 0, 0, 0, ""
}
}
offsets, ok := vals["offset"]
if ok {
offset, err = strconv.Atoi(offsets[0])
if err != nil {
return err, 0, 0, 0, ""
}
}
limits, ok := vals["limit"]
if ok {
limit, err = strconv.Atoi(limits[0])
if err != nil {
return err, 0, 0, 0, ""
}
}
orderbys, ok := vals["orderby"]
if ok {
orderby = orderbys[0]
}
if page <= 0 || offset < 0 || (orderby != "id" && orderby != "-id") {
return errors.New("The url queries are not correct."), 0, 0, 0, ""
}
return nil, page, offset, limit, orderby
}
// makeGetSpecHandler creates the handler logic
func makeGetSpecHandler(m *mux.Router, endpoints endpoint.Endpoints, options []http.ServerOption) {
// swagger:operation GET /api/users/{userId} users swaggGetSpecReq
// ---
// summary: Get the user's profile with userId.
// description: You need to specify the userId to get the detail profile about the user.
// parameters:
// - name: userId
// in: path
// description: id of user
// type: string
// required: true
// responses:
// "200":
// "$ref": "#/responses/swaggUserResp"
// "400":
// "$ref": "#/responses/swaggBadReq"
m.Methods("GET").Path("/api/users/{userId}").Handler(
handlers.CORS(
handlers.AllowedMethods([]string{"GET"}),
handlers.AllowedOrigins([]string{"*"}),
)(http.NewServer(endpoints.GetSpecEndpoint, decodeGetSpecRequest, encodeGetSpecResponse, options...)),
)
}
// decodeGetSpecRequest is a transport/http.DecodeRequestFunc that decodes a
// JSON-encoded request from the HTTP request body.
func decodeGetSpecRequest(_ context.Context, r *http1.Request) (interface{}, error) {
vars := mux.Vars(r)
id, ok := vars["userId"]
if !ok {
return nil, errors.New("not a valid ID")
}
req := endpoint.GetSpecRequest{
Id: id,
}
return req, nil
}
// encodeGetSpecResponse is a transport/http.EncodeResponseFunc that encodes
// the response as JSON to the response writer
func encodeGetSpecResponse(ctx context.Context, w http1.ResponseWriter, response interface{}) (err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
err = json.NewEncoder(w).Encode(response)
return
}
// makeGetAllHandler creates the handler logic
func makeGetAllHandler(m *mux.Router, endpoints endpoint.Endpoints, options []http.ServerOption) {
// swagger:operation GET /api/users users swaggGetAllReq
// ---
// summary: Get all users' profiles
// description: Get all users' profiles
// parameters:
// - name: page
// in: query
// description: page indicates the number of pages you want to get from server.
// type: int
// - name: offset
// in: query
// description: offset indicates the number of targets you want to skip.
// type: int
// - name: limit
// in: query
// description: limit indicates the number of targets in one page you want to get from server.
// type: int
// - name: orderby
// in: query
// description: orderby indicates the order of targets you want to get from server.
// type: int
// responses:
// "200":
// "$ref": "#/responses/swaggUsersResp"
// "400":
// "$ref": "#/responses/swaggBadReq"
m.Methods("GET").Path("/api/users").Handler(
handlers.CORS(
handlers.AllowedMethods([]string{"GET"}),
handlers.AllowedOrigins([]string{"*"}),
)(http.NewServer(endpoints.GetAllEndpoint, decodeGetAllRequest, encodeGetAllResponse, options...)),
)
}
// decodeGetAllRequest is a transport/http.DecodeRequestFunc that decodes a
// JSON-encoded request from the HTTP request body.
func decodeGetAllRequest(_ context.Context, r *http1.Request) (interface{}, error) {
err, page, offset, limit, orderby := getQueries(r)
req := endpoint.GetAllRequest{
Page: page,
Offset: offset,
Limit: limit,
Orderby: orderby,
}
return req, err
}
// encodeGetAllResponse is a transport/http.EncodeResponseFunc that encodes
// the response as JSON to the response writer
func encodeGetAllResponse(ctx context.Context, w http1.ResponseWriter, response interface{}) (err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
err = json.NewEncoder(w).Encode(response)
return
}
// makeGetUDFHandler creates the handler logic
func makeGetUDFHandler(m *mux.Router, endpoints endpoint.Endpoints, options []http.ServerOption) {
// swagger:operation GET /api/users/{username} users swaggGetUDFReq
// ---
// summary: Get all users' profiles with the request username
// description: Get all users' profiles with the request username
// parameters:
// - name: username
// in: path
// description: name of user
// type: string
// required: true
// - name: page
// in: query
// description: page indicates the number of pages you want to get from server.
// type: int
// - name: offset
// in: query
// description: offset indicates the number of targets you want to skip.
// type: int
// - name: limit
// in: query
// description: limit indicates the number of targets in one page you want to get from server.
// type: int
// - name: orderby
// in: query
// description: orderby indicates the order of targets you want to get from server.
// type: int
// responses:
// "200":
// "$ref": "#/responses/swaggUsersResp"
// "400":
// "$ref": "#/responses/swaggBadReq"
m.Methods("GET").Path("/api/users/{username:[a-zA-Z]}").Handler(
handlers.CORS(
handlers.AllowedMethods([]string{"GET"}),
handlers.AllowedOrigins([]string{"*"}),
)(http.NewServer(endpoints.GetUDFEndpoint, decodeGetUDFRequest, encodeGetUDFResponse, options...)),
)
}
// decodeGetUDFRequest is a transport/http.DecodeRequestFunc that decodes a
// JSON-encoded request from the HTTP request body.
func decodeGetUDFRequest(_ context.Context, r *http1.Request) (interface{}, error) {
vars := mux.Vars(r)
name, ok := vars["username"]
if !ok {
return nil, errors.New("not a valid username")
}
err, page, offset, limit, orderby := getQueries(r)
req := endpoint.GetUDFRequest{
Name: name,
Page: page,
Offset: offset,
Limit: limit,
Orderby: orderby,
}
return req, err
}
// encodeGetUDFResponse is a transport/http.EncodeResponseFunc that encodes
// the response as JSON to the response writer
func encodeGetUDFResponse(ctx context.Context, w http1.ResponseWriter, response interface{}) (err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
err = json.NewEncoder(w).Encode(response)
return
}
// makePostHandler creates the handler logic
func makePostHandler(m *mux.Router, endpoints endpoint.Endpoints, options []http.ServerOption) {
// swagger:route POST /api/users users swaggCreateUserReq
// Create a new user with the profile.
// If the user's id is "exists", error will be returned.
// responses:
// 200: swaggUserResp
// 400: swaggBadReq
m.Methods("POST").Path("/api/users").Handler(
handlers.CORS(
handlers.AllowedMethods([]string{"POST"}),
handlers.AllowedOrigins([]string{"*"}),
)(http.NewServer(endpoints.PostEndpoint, decodePostRequest, encodePostResponse, options...)),
)
}
// decodePostRequest is a transport/http.DecodeRequestFunc that decodes a
// JSON-encoded request from the HTTP request body.
func decodePostRequest(_ context.Context, r *http1.Request) (interface{}, error) {
req := endpoint.PostRequest{}
err := json.NewDecoder(r.Body).Decode(&req.User)
return req, err
}
// encodePostResponse is a transport/http.EncodeResponseFunc that encodes
// the response as JSON to the response writer
func encodePostResponse(ctx context.Context, w http1.ResponseWriter, response interface{}) (err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
err = json.NewEncoder(w).Encode(response)
return
}
// makePatchHandler creates the handler logic
func makePatchHandler(m *mux.Router, endpoints endpoint.Endpoints, options []http.ServerOption) {
m.Methods("POST").Path("/patch").Handler(handlers.CORS(handlers.AllowedMethods([]string{"POST"}), handlers.AllowedOrigins([]string{"*"}))(http.NewServer(endpoints.PatchEndpoint, decodePatchRequest, encodePatchResponse, options...)))
}
// decodePatchRequest is a transport/http.DecodeRequestFunc that decodes a
// JSON-encoded request from the HTTP request body.
func decodePatchRequest(_ context.Context, r *http1.Request) (interface{}, error) {
req := endpoint.PatchRequest{}
err := json.NewDecoder(r.Body).Decode(&req)
return req, err
}
// encodePatchResponse is a transport/http.EncodeResponseFunc that encodes
// the response as JSON to the response writer
func encodePatchResponse(ctx context.Context, w http1.ResponseWriter, response interface{}) (err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
err = json.NewEncoder(w).Encode(response)
return
}
// makePutHandler creates the handler logic
func makePutHandler(m *mux.Router, endpoints endpoint.Endpoints, options []http.ServerOption) {
// swagger:operation PUT /api/users/{userId} users swaggPutReq
// ---
// summary: Update the user profile
// description: Update the user profile with the profile. Also, you need to specify the user ID.
// parameters:
// - name: userId
// in: path
// description: id of user
// type: string
// required: true
// - name: Body
// in: body
// schema:
// "$ref": "#/definitions/User"
// responses:
// "200":
// "$ref": "#/responses/swaggUserResp"
// "400":
// "$ref": "#/responses/swaggBadReq"
m.Methods("PUT").Path("/api/users/{userId}").Handler(
handlers.CORS(
handlers.AllowedMethods([]string{"PUT"}),
handlers.AllowedOrigins([]string{"*"}),
)(http.NewServer(endpoints.PutEndpoint, decodePutRequest, encodePutResponse, options...)),
)
}
// decodePutRequest is a transport/http.DecodeRequestFunc that decodes a
// JSON-encoded request from the HTTP request body.
func decodePutRequest(_ context.Context, r *http1.Request) (interface{}, error) {
req := endpoint.PutRequest{}
err := json.NewDecoder(r.Body).Decode(&req.User)
vars := mux.Vars(r)
id, ok := vars["userId"]
if !ok {
return nil, errors.New("not a valid ID")
}
req.Id = id
return req, err
}
// encodePutResponse is a transport/http.EncodeResponseFunc that encodes
// the response as JSON to the response writer
func encodePutResponse(ctx context.Context, w http1.ResponseWriter, response interface{}) (err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
err = json.NewEncoder(w).Encode(response)
return
}
// makeDeleteHandler creates the handler logic
func makeDeleteHandler(m *mux.Router, endpoints endpoint.Endpoints, options []http.ServerOption) {
// swagger:operation DELETE /api/users/{userId} users swaggDeleteReq
// ---
// summary: Delete the user
// description: You need to specify the user ID to delete the user .
// parameters:
// - name: userId
// in: path
// description: id of user
// type: string
// required: true
// responses:
// "200":
// "$ref": "#/responses/swaggNoReturnValue"
// "400":
// "$ref": "#/responses/swaggBadReq"
m.Methods("DELETE").Path("/api/users/{userId}").Handler(
handlers.CORS(
handlers.AllowedMethods([]string{"DELETE"}),
handlers.AllowedOrigins([]string{"*"}),
)(http.NewServer(endpoints.DeleteEndpoint, decodeDeleteRequest, encodeDeleteResponse, options...)),
)
}
// decodeDeleteRequest is a transport/http.DecodeRequestFunc that decodes a
// JSON-encoded request from the HTTP request body.
func decodeDeleteRequest(_ context.Context, r *http1.Request) (interface{}, error) {
vars := mux.Vars(r)
id, ok := vars["userId"]
if !ok {
return nil, errors.New("not a valid ID")
}
req := endpoint.DeleteRequest{
Id: id,
}
return req, nil
}
// encodeDeleteResponse is a transport/http.EncodeResponseFunc that encodes
// the response as JSON to the response writer
func encodeDeleteResponse(ctx context.Context, w http1.ResponseWriter, response interface{}) (err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
err = json.NewEncoder(w).Encode(response)
return
}