spikard 0.5.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +674 -674
- data/ext/spikard_rb/Cargo.toml +17 -17
- data/ext/spikard_rb/extconf.rb +13 -10
- data/ext/spikard_rb/src/lib.rs +6 -6
- data/lib/spikard/app.rb +405 -405
- data/lib/spikard/background.rb +27 -27
- data/lib/spikard/config.rb +396 -396
- data/lib/spikard/converters.rb +13 -13
- data/lib/spikard/handler_wrapper.rb +113 -113
- data/lib/spikard/provide.rb +214 -214
- data/lib/spikard/response.rb +173 -173
- data/lib/spikard/schema.rb +243 -243
- data/lib/spikard/sse.rb +111 -111
- data/lib/spikard/streaming_response.rb +44 -44
- data/lib/spikard/testing.rb +256 -256
- data/lib/spikard/upload_file.rb +131 -131
- data/lib/spikard/version.rb +5 -5
- data/lib/spikard/websocket.rb +59 -59
- data/lib/spikard.rb +43 -43
- data/sig/spikard.rbs +366 -366
- data/vendor/crates/spikard-bindings-shared/Cargo.toml +63 -63
- data/vendor/crates/spikard-bindings-shared/examples/config_extraction.rs +132 -132
- data/vendor/crates/spikard-bindings-shared/src/config_extractor.rs +752 -752
- data/vendor/crates/spikard-bindings-shared/src/conversion_traits.rs +194 -194
- data/vendor/crates/spikard-bindings-shared/src/di_traits.rs +246 -246
- data/vendor/crates/spikard-bindings-shared/src/error_response.rs +401 -401
- data/vendor/crates/spikard-bindings-shared/src/handler_base.rs +238 -238
- data/vendor/crates/spikard-bindings-shared/src/lib.rs +24 -24
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_base.rs +292 -292
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_executor.rs +616 -616
- data/vendor/crates/spikard-bindings-shared/src/response_builder.rs +305 -305
- data/vendor/crates/spikard-bindings-shared/src/test_client_base.rs +248 -248
- data/vendor/crates/spikard-bindings-shared/src/validation_helpers.rs +351 -351
- data/vendor/crates/spikard-bindings-shared/tests/comprehensive_coverage.rs +454 -454
- data/vendor/crates/spikard-bindings-shared/tests/error_response_edge_cases.rs +383 -383
- data/vendor/crates/spikard-bindings-shared/tests/handler_base_integration.rs +280 -280
- data/vendor/crates/spikard-core/Cargo.toml +40 -40
- data/vendor/crates/spikard-core/src/bindings/mod.rs +3 -3
- data/vendor/crates/spikard-core/src/bindings/response.rs +133 -133
- data/vendor/crates/spikard-core/src/debug.rs +127 -127
- data/vendor/crates/spikard-core/src/di/container.rs +702 -702
- data/vendor/crates/spikard-core/src/di/dependency.rs +273 -273
- data/vendor/crates/spikard-core/src/di/error.rs +118 -118
- data/vendor/crates/spikard-core/src/di/factory.rs +534 -534
- data/vendor/crates/spikard-core/src/di/graph.rs +506 -506
- data/vendor/crates/spikard-core/src/di/mod.rs +192 -192
- data/vendor/crates/spikard-core/src/di/resolved.rs +405 -405
- data/vendor/crates/spikard-core/src/di/value.rs +281 -281
- data/vendor/crates/spikard-core/src/errors.rs +69 -69
- data/vendor/crates/spikard-core/src/http.rs +415 -415
- data/vendor/crates/spikard-core/src/lib.rs +29 -29
- data/vendor/crates/spikard-core/src/lifecycle.rs +1186 -1186
- data/vendor/crates/spikard-core/src/metadata.rs +389 -389
- data/vendor/crates/spikard-core/src/parameters.rs +2525 -2525
- data/vendor/crates/spikard-core/src/problem.rs +344 -344
- data/vendor/crates/spikard-core/src/request_data.rs +1154 -1154
- data/vendor/crates/spikard-core/src/router.rs +510 -510
- data/vendor/crates/spikard-core/src/schema_registry.rs +183 -183
- data/vendor/crates/spikard-core/src/type_hints.rs +304 -304
- data/vendor/crates/spikard-core/src/validation/error_mapper.rs +696 -688
- data/vendor/crates/spikard-core/src/validation/mod.rs +457 -457
- data/vendor/crates/spikard-http/Cargo.toml +62 -64
- data/vendor/crates/spikard-http/examples/sse-notifications.rs +148 -148
- data/vendor/crates/spikard-http/examples/websocket-chat.rs +92 -92
- data/vendor/crates/spikard-http/src/auth.rs +296 -296
- data/vendor/crates/spikard-http/src/background.rs +1860 -1860
- data/vendor/crates/spikard-http/src/bindings/mod.rs +3 -3
- data/vendor/crates/spikard-http/src/bindings/response.rs +1 -1
- data/vendor/crates/spikard-http/src/body_metadata.rs +8 -8
- data/vendor/crates/spikard-http/src/cors.rs +1005 -1005
- data/vendor/crates/spikard-http/src/debug.rs +128 -128
- data/vendor/crates/spikard-http/src/di_handler.rs +1668 -1668
- data/vendor/crates/spikard-http/src/handler_response.rs +901 -901
- data/vendor/crates/spikard-http/src/handler_trait.rs +838 -830
- data/vendor/crates/spikard-http/src/handler_trait_tests.rs +290 -290
- data/vendor/crates/spikard-http/src/lib.rs +534 -534
- data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +230 -230
- data/vendor/crates/spikard-http/src/lifecycle.rs +1193 -1193
- data/vendor/crates/spikard-http/src/middleware/mod.rs +560 -540
- data/vendor/crates/spikard-http/src/middleware/multipart.rs +912 -912
- data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +513 -513
- data/vendor/crates/spikard-http/src/middleware/validation.rs +768 -735
- data/vendor/crates/spikard-http/src/openapi/mod.rs +309 -309
- data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +535 -535
- data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +1363 -1363
- data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +665 -665
- data/vendor/crates/spikard-http/src/query_parser.rs +793 -793
- data/vendor/crates/spikard-http/src/response.rs +720 -720
- data/vendor/crates/spikard-http/src/server/handler.rs +1650 -1650
- data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +234 -234
- data/vendor/crates/spikard-http/src/server/mod.rs +1593 -1502
- data/vendor/crates/spikard-http/src/server/request_extraction.rs +789 -770
- data/vendor/crates/spikard-http/src/server/routing_factory.rs +629 -599
- data/vendor/crates/spikard-http/src/sse.rs +1409 -1409
- data/vendor/crates/spikard-http/src/testing/form.rs +52 -52
- data/vendor/crates/spikard-http/src/testing/multipart.rs +64 -60
- data/vendor/crates/spikard-http/src/testing/test_client.rs +311 -283
- data/vendor/crates/spikard-http/src/testing.rs +406 -377
- data/vendor/crates/spikard-http/src/websocket.rs +1404 -1375
- data/vendor/crates/spikard-http/tests/background_behavior.rs +832 -832
- data/vendor/crates/spikard-http/tests/common/handlers.rs +309 -309
- data/vendor/crates/spikard-http/tests/common/mod.rs +26 -26
- data/vendor/crates/spikard-http/tests/di_integration.rs +192 -192
- data/vendor/crates/spikard-http/tests/doc_snippets.rs +5 -5
- data/vendor/crates/spikard-http/tests/lifecycle_execution.rs +1093 -1093
- data/vendor/crates/spikard-http/tests/multipart_behavior.rs +656 -656
- data/vendor/crates/spikard-http/tests/server_config_builder.rs +314 -314
- data/vendor/crates/spikard-http/tests/sse_behavior.rs +620 -620
- data/vendor/crates/spikard-http/tests/websocket_behavior.rs +663 -663
- data/vendor/crates/spikard-rb/Cargo.toml +48 -48
- data/vendor/crates/spikard-rb/build.rs +199 -199
- data/vendor/crates/spikard-rb/src/background.rs +63 -63
- data/vendor/crates/spikard-rb/src/config/mod.rs +5 -5
- data/vendor/crates/spikard-rb/src/config/server_config.rs +285 -285
- data/vendor/crates/spikard-rb/src/conversion.rs +554 -554
- data/vendor/crates/spikard-rb/src/di/builder.rs +100 -100
- data/vendor/crates/spikard-rb/src/di/mod.rs +375 -375
- data/vendor/crates/spikard-rb/src/handler.rs +618 -618
- data/vendor/crates/spikard-rb/src/integration/mod.rs +3 -3
- data/vendor/crates/spikard-rb/src/lib.rs +1806 -1810
- data/vendor/crates/spikard-rb/src/lifecycle.rs +275 -275
- data/vendor/crates/spikard-rb/src/metadata/mod.rs +5 -5
- data/vendor/crates/spikard-rb/src/metadata/route_extraction.rs +442 -447
- data/vendor/crates/spikard-rb/src/runtime/mod.rs +5 -5
- data/vendor/crates/spikard-rb/src/runtime/server_runner.rs +324 -324
- data/vendor/crates/spikard-rb/src/server.rs +305 -308
- data/vendor/crates/spikard-rb/src/sse.rs +231 -231
- data/vendor/crates/spikard-rb/src/testing/client.rs +538 -551
- data/vendor/crates/spikard-rb/src/testing/mod.rs +7 -7
- data/vendor/crates/spikard-rb/src/testing/sse.rs +143 -143
- data/vendor/crates/spikard-rb/src/testing/websocket.rs +608 -635
- data/vendor/crates/spikard-rb/src/websocket.rs +377 -374
- metadata +15 -1
|
@@ -1,599 +1,629 @@
|
|
|
1
|
-
//! Method routing factory for consolidating HTTP method handler creation.
|
|
2
|
-
//!
|
|
3
|
-
//! This module provides a factory pattern to eliminate duplication in the
|
|
4
|
-
//! create_method_router function. It handles:
|
|
5
|
-
//! - Different HTTP methods (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, TRACE)
|
|
6
|
-
//! - Both path variants (with and without path parameters)
|
|
7
|
-
//! - Handler wrapping and request data extraction
|
|
8
|
-
//! - Lifecycle hook integration
|
|
9
|
-
|
|
10
|
-
use crate::handler_trait::Handler;
|
|
11
|
-
use axum::body::Body;
|
|
12
|
-
use axum::extract::{Path, Request as AxumRequest};
|
|
13
|
-
use axum::http::Request;
|
|
14
|
-
use axum::routing::MethodRouter;
|
|
15
|
-
use bytes::Bytes;
|
|
16
|
-
use std::collections::HashMap;
|
|
17
|
-
use std::sync::Arc;
|
|
18
|
-
|
|
19
|
-
use super::lifecycle_execution;
|
|
20
|
-
use super::request_extraction;
|
|
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
|
-
req
|
|
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
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
)
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
.
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
);
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
}
|
|
479
|
-
})
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
let
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
.
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
}
|
|
599
|
-
}
|
|
1
|
+
//! Method routing factory for consolidating HTTP method handler creation.
|
|
2
|
+
//!
|
|
3
|
+
//! This module provides a factory pattern to eliminate duplication in the
|
|
4
|
+
//! create_method_router function. It handles:
|
|
5
|
+
//! - Different HTTP methods (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, TRACE)
|
|
6
|
+
//! - Both path variants (with and without path parameters)
|
|
7
|
+
//! - Handler wrapping and request data extraction
|
|
8
|
+
//! - Lifecycle hook integration
|
|
9
|
+
|
|
10
|
+
use crate::handler_trait::{Handler, HandlerResult, RequestData};
|
|
11
|
+
use axum::body::Body;
|
|
12
|
+
use axum::extract::{Path, Request as AxumRequest};
|
|
13
|
+
use axum::http::Request;
|
|
14
|
+
use axum::routing::MethodRouter;
|
|
15
|
+
use bytes::Bytes;
|
|
16
|
+
use std::collections::HashMap;
|
|
17
|
+
use std::sync::Arc;
|
|
18
|
+
|
|
19
|
+
use super::lifecycle_execution;
|
|
20
|
+
use super::request_extraction;
|
|
21
|
+
|
|
22
|
+
#[inline]
|
|
23
|
+
async fn call_with_optional_hooks(
|
|
24
|
+
req: Request<Body>,
|
|
25
|
+
request_data: RequestData,
|
|
26
|
+
handler: Arc<dyn Handler>,
|
|
27
|
+
hooks: Option<Arc<crate::LifecycleHooks>>,
|
|
28
|
+
) -> HandlerResult {
|
|
29
|
+
if hooks.as_ref().is_some_and(|h| !h.is_empty()) {
|
|
30
|
+
call_with_optional_hooks(req, request_data, handler, hooks).await
|
|
31
|
+
} else {
|
|
32
|
+
handler.call(req, request_data).await
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// HTTP method type enumeration for routing
|
|
37
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
38
|
+
pub enum HttpMethod {
|
|
39
|
+
Get,
|
|
40
|
+
Post,
|
|
41
|
+
Put,
|
|
42
|
+
Patch,
|
|
43
|
+
Delete,
|
|
44
|
+
Head,
|
|
45
|
+
Trace,
|
|
46
|
+
Options,
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
impl HttpMethod {
|
|
50
|
+
/// Parse from string representation (e.g., "GET", "POST")
|
|
51
|
+
pub fn from_str(s: &str) -> Option<Self> {
|
|
52
|
+
match s {
|
|
53
|
+
"GET" => Some(HttpMethod::Get),
|
|
54
|
+
"POST" => Some(HttpMethod::Post),
|
|
55
|
+
"PUT" => Some(HttpMethod::Put),
|
|
56
|
+
"PATCH" => Some(HttpMethod::Patch),
|
|
57
|
+
"DELETE" => Some(HttpMethod::Delete),
|
|
58
|
+
"HEAD" => Some(HttpMethod::Head),
|
|
59
|
+
"TRACE" => Some(HttpMethod::Trace),
|
|
60
|
+
"OPTIONS" => Some(HttpMethod::Options),
|
|
61
|
+
_ => None,
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/// Check if this method typically has a request body
|
|
66
|
+
pub fn expects_body(&self) -> bool {
|
|
67
|
+
matches!(self, HttpMethod::Post | HttpMethod::Put | HttpMethod::Patch)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/// Factory for creating method routers
|
|
72
|
+
pub struct MethodRouterFactory;
|
|
73
|
+
|
|
74
|
+
impl MethodRouterFactory {
|
|
75
|
+
/// Create a method router for the given HTTP method
|
|
76
|
+
///
|
|
77
|
+
/// # Arguments
|
|
78
|
+
///
|
|
79
|
+
/// * `method` - HTTP method string (e.g., "GET", "POST")
|
|
80
|
+
/// * `has_path_params` - Whether the route has path parameters
|
|
81
|
+
/// * `handler` - The request handler
|
|
82
|
+
/// * `hooks` - Optional lifecycle hooks
|
|
83
|
+
///
|
|
84
|
+
/// # Returns
|
|
85
|
+
///
|
|
86
|
+
/// A configured MethodRouter or an error if the method is unsupported
|
|
87
|
+
pub fn create(
|
|
88
|
+
method: &str,
|
|
89
|
+
has_path_params: bool,
|
|
90
|
+
handler: Arc<dyn Handler>,
|
|
91
|
+
hooks: Option<Arc<crate::LifecycleHooks>>,
|
|
92
|
+
include_raw_query_params: bool,
|
|
93
|
+
) -> Result<MethodRouter, String> {
|
|
94
|
+
let http_method = HttpMethod::from_str(method)
|
|
95
|
+
.ok_or_else(|| format!("[spikard-router] unsupported HTTP method: {}", method))?;
|
|
96
|
+
|
|
97
|
+
let include_query_params_json = !handler.prefers_parameter_extraction();
|
|
98
|
+
let include_headers = handler.wants_headers();
|
|
99
|
+
let include_cookies = handler.wants_cookies();
|
|
100
|
+
|
|
101
|
+
Ok(if has_path_params {
|
|
102
|
+
Self::create_with_path_params(
|
|
103
|
+
http_method,
|
|
104
|
+
handler,
|
|
105
|
+
hooks,
|
|
106
|
+
include_raw_query_params,
|
|
107
|
+
include_query_params_json,
|
|
108
|
+
include_headers,
|
|
109
|
+
include_cookies,
|
|
110
|
+
)
|
|
111
|
+
} else {
|
|
112
|
+
Self::create_without_path_params(
|
|
113
|
+
http_method,
|
|
114
|
+
handler,
|
|
115
|
+
hooks,
|
|
116
|
+
include_raw_query_params,
|
|
117
|
+
include_query_params_json,
|
|
118
|
+
include_headers,
|
|
119
|
+
include_cookies,
|
|
120
|
+
)
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/// Create a method router for a route with path parameters
|
|
125
|
+
fn create_with_path_params(
|
|
126
|
+
method: HttpMethod,
|
|
127
|
+
handler: Arc<dyn Handler>,
|
|
128
|
+
hooks: Option<Arc<crate::LifecycleHooks>>,
|
|
129
|
+
include_raw_query_params: bool,
|
|
130
|
+
include_query_params_json: bool,
|
|
131
|
+
include_headers: bool,
|
|
132
|
+
include_cookies: bool,
|
|
133
|
+
) -> MethodRouter {
|
|
134
|
+
let handler_clone = handler.clone();
|
|
135
|
+
let hooks_clone = hooks.clone();
|
|
136
|
+
let without_body_options = request_extraction::WithoutBodyExtractionOptions {
|
|
137
|
+
include_raw_query_params,
|
|
138
|
+
include_query_params_json,
|
|
139
|
+
include_headers,
|
|
140
|
+
include_cookies,
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
if method.expects_body() {
|
|
144
|
+
match method {
|
|
145
|
+
HttpMethod::Post => {
|
|
146
|
+
let handler_clone = handler_clone.clone();
|
|
147
|
+
let hooks_clone = hooks_clone.clone();
|
|
148
|
+
axum::routing::post(
|
|
149
|
+
move |path_params: Path<HashMap<String, String>>, req: AxumRequest| {
|
|
150
|
+
let handler = handler_clone.clone();
|
|
151
|
+
let hooks = hooks_clone.clone();
|
|
152
|
+
async move {
|
|
153
|
+
let (parts, body) = req.into_parts();
|
|
154
|
+
let request_data =
|
|
155
|
+
request_extraction::create_request_data_with_body(
|
|
156
|
+
&parts,
|
|
157
|
+
path_params.0,
|
|
158
|
+
body,
|
|
159
|
+
include_raw_query_params,
|
|
160
|
+
include_query_params_json,
|
|
161
|
+
include_headers,
|
|
162
|
+
include_cookies,
|
|
163
|
+
)
|
|
164
|
+
.await?;
|
|
165
|
+
let mut req = Request::from_parts(
|
|
166
|
+
parts,
|
|
167
|
+
Body::from(request_data.raw_body.clone().unwrap_or_else(Bytes::new)),
|
|
168
|
+
);
|
|
169
|
+
if handler.wants_request_extensions() {
|
|
170
|
+
req.extensions_mut().insert(Arc::new(request_data.clone()));
|
|
171
|
+
}
|
|
172
|
+
call_with_optional_hooks(req, request_data, handler, hooks).await
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
)
|
|
176
|
+
}
|
|
177
|
+
HttpMethod::Put => {
|
|
178
|
+
let handler_clone = handler_clone.clone();
|
|
179
|
+
let hooks_clone = hooks_clone.clone();
|
|
180
|
+
axum::routing::put(
|
|
181
|
+
move |path_params: Path<HashMap<String, String>>, req: AxumRequest| {
|
|
182
|
+
let handler = handler_clone.clone();
|
|
183
|
+
let hooks = hooks_clone.clone();
|
|
184
|
+
async move {
|
|
185
|
+
let (parts, body) = req.into_parts();
|
|
186
|
+
let request_data =
|
|
187
|
+
request_extraction::create_request_data_with_body(
|
|
188
|
+
&parts,
|
|
189
|
+
path_params.0,
|
|
190
|
+
body,
|
|
191
|
+
include_raw_query_params,
|
|
192
|
+
include_query_params_json,
|
|
193
|
+
include_headers,
|
|
194
|
+
include_cookies,
|
|
195
|
+
)
|
|
196
|
+
.await?;
|
|
197
|
+
let mut req = Request::from_parts(
|
|
198
|
+
parts,
|
|
199
|
+
Body::from(request_data.raw_body.clone().unwrap_or_else(Bytes::new)),
|
|
200
|
+
);
|
|
201
|
+
if handler.wants_request_extensions() {
|
|
202
|
+
req.extensions_mut().insert(Arc::new(request_data.clone()));
|
|
203
|
+
}
|
|
204
|
+
call_with_optional_hooks(req, request_data, handler, hooks).await
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
)
|
|
208
|
+
}
|
|
209
|
+
HttpMethod::Patch => {
|
|
210
|
+
let handler_clone = handler_clone.clone();
|
|
211
|
+
let hooks_clone = hooks_clone.clone();
|
|
212
|
+
axum::routing::patch(
|
|
213
|
+
move |path_params: Path<HashMap<String, String>>, req: AxumRequest| {
|
|
214
|
+
let handler = handler_clone.clone();
|
|
215
|
+
let hooks = hooks_clone.clone();
|
|
216
|
+
async move {
|
|
217
|
+
let (parts, body) = req.into_parts();
|
|
218
|
+
let request_data =
|
|
219
|
+
request_extraction::create_request_data_with_body(
|
|
220
|
+
&parts,
|
|
221
|
+
path_params.0,
|
|
222
|
+
body,
|
|
223
|
+
include_raw_query_params,
|
|
224
|
+
include_query_params_json,
|
|
225
|
+
include_headers,
|
|
226
|
+
include_cookies,
|
|
227
|
+
)
|
|
228
|
+
.await?;
|
|
229
|
+
let mut req = Request::from_parts(
|
|
230
|
+
parts,
|
|
231
|
+
Body::from(request_data.raw_body.clone().unwrap_or_else(Bytes::new)),
|
|
232
|
+
);
|
|
233
|
+
if handler.wants_request_extensions() {
|
|
234
|
+
req.extensions_mut().insert(Arc::new(request_data.clone()));
|
|
235
|
+
}
|
|
236
|
+
call_with_optional_hooks(req, request_data, handler, hooks).await
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
)
|
|
240
|
+
}
|
|
241
|
+
_ => MethodRouter::new(),
|
|
242
|
+
}
|
|
243
|
+
} else {
|
|
244
|
+
match method {
|
|
245
|
+
HttpMethod::Get => {
|
|
246
|
+
let handler_clone = handler_clone.clone();
|
|
247
|
+
let hooks_clone = hooks_clone.clone();
|
|
248
|
+
axum::routing::get(
|
|
249
|
+
move |path_params: Path<HashMap<String, String>>, req: AxumRequest| {
|
|
250
|
+
let handler = handler_clone.clone();
|
|
251
|
+
let hooks = hooks_clone.clone();
|
|
252
|
+
async move {
|
|
253
|
+
let request_data = request_extraction::create_request_data_without_body(
|
|
254
|
+
req.uri(),
|
|
255
|
+
req.method(),
|
|
256
|
+
req.headers(),
|
|
257
|
+
path_params.0,
|
|
258
|
+
without_body_options,
|
|
259
|
+
);
|
|
260
|
+
let mut req = req;
|
|
261
|
+
if handler.wants_request_extensions() {
|
|
262
|
+
req.extensions_mut().insert(Arc::new(request_data.clone()));
|
|
263
|
+
}
|
|
264
|
+
call_with_optional_hooks(req, request_data, handler, hooks).await
|
|
265
|
+
}
|
|
266
|
+
},
|
|
267
|
+
)
|
|
268
|
+
}
|
|
269
|
+
HttpMethod::Delete => {
|
|
270
|
+
let handler_clone = handler_clone.clone();
|
|
271
|
+
let hooks_clone = hooks_clone.clone();
|
|
272
|
+
axum::routing::delete(
|
|
273
|
+
move |path_params: Path<HashMap<String, String>>, req: AxumRequest| {
|
|
274
|
+
let handler = handler_clone.clone();
|
|
275
|
+
let hooks = hooks_clone.clone();
|
|
276
|
+
async move {
|
|
277
|
+
let request_data = request_extraction::create_request_data_without_body(
|
|
278
|
+
req.uri(),
|
|
279
|
+
req.method(),
|
|
280
|
+
req.headers(),
|
|
281
|
+
path_params.0,
|
|
282
|
+
without_body_options,
|
|
283
|
+
);
|
|
284
|
+
let mut req = req;
|
|
285
|
+
if handler.wants_request_extensions() {
|
|
286
|
+
req.extensions_mut().insert(Arc::new(request_data.clone()));
|
|
287
|
+
}
|
|
288
|
+
call_with_optional_hooks(req, request_data, handler, hooks).await
|
|
289
|
+
}
|
|
290
|
+
},
|
|
291
|
+
)
|
|
292
|
+
}
|
|
293
|
+
HttpMethod::Head => {
|
|
294
|
+
let handler_clone = handler_clone.clone();
|
|
295
|
+
let hooks_clone = hooks_clone.clone();
|
|
296
|
+
axum::routing::head(
|
|
297
|
+
move |path_params: Path<HashMap<String, String>>, req: AxumRequest| {
|
|
298
|
+
let handler = handler_clone.clone();
|
|
299
|
+
let hooks = hooks_clone.clone();
|
|
300
|
+
async move {
|
|
301
|
+
let request_data = request_extraction::create_request_data_without_body(
|
|
302
|
+
req.uri(),
|
|
303
|
+
req.method(),
|
|
304
|
+
req.headers(),
|
|
305
|
+
path_params.0,
|
|
306
|
+
without_body_options,
|
|
307
|
+
);
|
|
308
|
+
let mut req = req;
|
|
309
|
+
if handler.wants_request_extensions() {
|
|
310
|
+
req.extensions_mut().insert(Arc::new(request_data.clone()));
|
|
311
|
+
}
|
|
312
|
+
call_with_optional_hooks(req, request_data, handler, hooks).await
|
|
313
|
+
}
|
|
314
|
+
},
|
|
315
|
+
)
|
|
316
|
+
}
|
|
317
|
+
HttpMethod::Trace => {
|
|
318
|
+
let handler_clone = handler_clone.clone();
|
|
319
|
+
let hooks_clone = hooks_clone.clone();
|
|
320
|
+
axum::routing::trace(
|
|
321
|
+
move |path_params: Path<HashMap<String, String>>, req: AxumRequest| {
|
|
322
|
+
let handler = handler_clone.clone();
|
|
323
|
+
let hooks = hooks_clone.clone();
|
|
324
|
+
async move {
|
|
325
|
+
let request_data = request_extraction::create_request_data_without_body(
|
|
326
|
+
req.uri(),
|
|
327
|
+
req.method(),
|
|
328
|
+
req.headers(),
|
|
329
|
+
path_params.0,
|
|
330
|
+
without_body_options,
|
|
331
|
+
);
|
|
332
|
+
let mut req = req;
|
|
333
|
+
if handler.wants_request_extensions() {
|
|
334
|
+
req.extensions_mut().insert(Arc::new(request_data.clone()));
|
|
335
|
+
}
|
|
336
|
+
call_with_optional_hooks(req, request_data, handler, hooks).await
|
|
337
|
+
}
|
|
338
|
+
},
|
|
339
|
+
)
|
|
340
|
+
}
|
|
341
|
+
HttpMethod::Options => {
|
|
342
|
+
let handler_clone = handler_clone.clone();
|
|
343
|
+
let hooks_clone = hooks_clone.clone();
|
|
344
|
+
axum::routing::options(
|
|
345
|
+
move |path_params: Path<HashMap<String, String>>, req: AxumRequest| {
|
|
346
|
+
let handler = handler_clone.clone();
|
|
347
|
+
let hooks = hooks_clone.clone();
|
|
348
|
+
async move {
|
|
349
|
+
let request_data = request_extraction::create_request_data_without_body(
|
|
350
|
+
req.uri(),
|
|
351
|
+
req.method(),
|
|
352
|
+
req.headers(),
|
|
353
|
+
path_params.0,
|
|
354
|
+
without_body_options,
|
|
355
|
+
);
|
|
356
|
+
let mut req = req;
|
|
357
|
+
if handler.wants_request_extensions() {
|
|
358
|
+
req.extensions_mut().insert(Arc::new(request_data.clone()));
|
|
359
|
+
}
|
|
360
|
+
call_with_optional_hooks(req, request_data, handler, hooks).await
|
|
361
|
+
}
|
|
362
|
+
},
|
|
363
|
+
)
|
|
364
|
+
}
|
|
365
|
+
_ => MethodRouter::new(),
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/// Create a method router for a route without path parameters
|
|
371
|
+
fn create_without_path_params(
|
|
372
|
+
method: HttpMethod,
|
|
373
|
+
handler: Arc<dyn Handler>,
|
|
374
|
+
hooks: Option<Arc<crate::LifecycleHooks>>,
|
|
375
|
+
include_raw_query_params: bool,
|
|
376
|
+
include_query_params_json: bool,
|
|
377
|
+
include_headers: bool,
|
|
378
|
+
include_cookies: bool,
|
|
379
|
+
) -> MethodRouter {
|
|
380
|
+
let handler_clone = handler.clone();
|
|
381
|
+
let hooks_clone = hooks.clone();
|
|
382
|
+
let without_body_options = request_extraction::WithoutBodyExtractionOptions {
|
|
383
|
+
include_raw_query_params,
|
|
384
|
+
include_query_params_json,
|
|
385
|
+
include_headers,
|
|
386
|
+
include_cookies,
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
if method.expects_body() {
|
|
390
|
+
match method {
|
|
391
|
+
HttpMethod::Post => {
|
|
392
|
+
let handler_clone = handler_clone.clone();
|
|
393
|
+
let hooks_clone = hooks_clone.clone();
|
|
394
|
+
axum::routing::post(move |req: AxumRequest| {
|
|
395
|
+
let handler = handler_clone.clone();
|
|
396
|
+
let hooks = hooks_clone.clone();
|
|
397
|
+
async move {
|
|
398
|
+
let (parts, body) = req.into_parts();
|
|
399
|
+
let request_data =
|
|
400
|
+
request_extraction::create_request_data_with_body(
|
|
401
|
+
&parts,
|
|
402
|
+
HashMap::new(),
|
|
403
|
+
body,
|
|
404
|
+
include_raw_query_params,
|
|
405
|
+
include_query_params_json,
|
|
406
|
+
include_headers,
|
|
407
|
+
include_cookies,
|
|
408
|
+
)
|
|
409
|
+
.await?;
|
|
410
|
+
let mut req = Request::from_parts(
|
|
411
|
+
parts,
|
|
412
|
+
Body::from(request_data.raw_body.clone().unwrap_or_else(Bytes::new)),
|
|
413
|
+
);
|
|
414
|
+
if handler.wants_request_extensions() {
|
|
415
|
+
req.extensions_mut().insert(Arc::new(request_data.clone()));
|
|
416
|
+
}
|
|
417
|
+
call_with_optional_hooks(req, request_data, handler, hooks).await
|
|
418
|
+
}
|
|
419
|
+
})
|
|
420
|
+
}
|
|
421
|
+
HttpMethod::Put => {
|
|
422
|
+
let handler_clone = handler_clone.clone();
|
|
423
|
+
let hooks_clone = hooks_clone.clone();
|
|
424
|
+
axum::routing::put(move |req: AxumRequest| {
|
|
425
|
+
let handler = handler_clone.clone();
|
|
426
|
+
let hooks = hooks_clone.clone();
|
|
427
|
+
async move {
|
|
428
|
+
let (parts, body) = req.into_parts();
|
|
429
|
+
let request_data =
|
|
430
|
+
request_extraction::create_request_data_with_body(
|
|
431
|
+
&parts,
|
|
432
|
+
HashMap::new(),
|
|
433
|
+
body,
|
|
434
|
+
include_raw_query_params,
|
|
435
|
+
include_query_params_json,
|
|
436
|
+
include_headers,
|
|
437
|
+
include_cookies,
|
|
438
|
+
)
|
|
439
|
+
.await?;
|
|
440
|
+
let mut req = Request::from_parts(
|
|
441
|
+
parts,
|
|
442
|
+
Body::from(request_data.raw_body.clone().unwrap_or_else(Bytes::new)),
|
|
443
|
+
);
|
|
444
|
+
if handler.wants_request_extensions() {
|
|
445
|
+
req.extensions_mut().insert(Arc::new(request_data.clone()));
|
|
446
|
+
}
|
|
447
|
+
call_with_optional_hooks(req, request_data, handler, hooks).await
|
|
448
|
+
}
|
|
449
|
+
})
|
|
450
|
+
}
|
|
451
|
+
HttpMethod::Patch => {
|
|
452
|
+
let handler_clone = handler_clone.clone();
|
|
453
|
+
let hooks_clone = hooks_clone.clone();
|
|
454
|
+
axum::routing::patch(move |req: AxumRequest| {
|
|
455
|
+
let handler = handler_clone.clone();
|
|
456
|
+
let hooks = hooks_clone.clone();
|
|
457
|
+
async move {
|
|
458
|
+
let (parts, body) = req.into_parts();
|
|
459
|
+
let request_data =
|
|
460
|
+
request_extraction::create_request_data_with_body(
|
|
461
|
+
&parts,
|
|
462
|
+
HashMap::new(),
|
|
463
|
+
body,
|
|
464
|
+
include_raw_query_params,
|
|
465
|
+
include_query_params_json,
|
|
466
|
+
include_headers,
|
|
467
|
+
include_cookies,
|
|
468
|
+
)
|
|
469
|
+
.await?;
|
|
470
|
+
let mut req = Request::from_parts(
|
|
471
|
+
parts,
|
|
472
|
+
Body::from(request_data.raw_body.clone().unwrap_or_else(Bytes::new)),
|
|
473
|
+
);
|
|
474
|
+
if handler.wants_request_extensions() {
|
|
475
|
+
req.extensions_mut().insert(Arc::new(request_data.clone()));
|
|
476
|
+
}
|
|
477
|
+
call_with_optional_hooks(req, request_data, handler, hooks).await
|
|
478
|
+
}
|
|
479
|
+
})
|
|
480
|
+
}
|
|
481
|
+
_ => MethodRouter::new(),
|
|
482
|
+
}
|
|
483
|
+
} else {
|
|
484
|
+
match method {
|
|
485
|
+
HttpMethod::Get => {
|
|
486
|
+
let handler_clone = handler_clone.clone();
|
|
487
|
+
let hooks_clone = hooks_clone.clone();
|
|
488
|
+
axum::routing::get(move |req: AxumRequest| {
|
|
489
|
+
let handler = handler_clone.clone();
|
|
490
|
+
let hooks = hooks_clone.clone();
|
|
491
|
+
async move {
|
|
492
|
+
let request_data = request_extraction::create_request_data_without_body(
|
|
493
|
+
req.uri(),
|
|
494
|
+
req.method(),
|
|
495
|
+
req.headers(),
|
|
496
|
+
HashMap::new(),
|
|
497
|
+
without_body_options,
|
|
498
|
+
);
|
|
499
|
+
let mut req = req;
|
|
500
|
+
if handler.wants_request_extensions() {
|
|
501
|
+
req.extensions_mut().insert(Arc::new(request_data.clone()));
|
|
502
|
+
}
|
|
503
|
+
call_with_optional_hooks(req, request_data, handler, hooks).await
|
|
504
|
+
}
|
|
505
|
+
})
|
|
506
|
+
}
|
|
507
|
+
HttpMethod::Delete => {
|
|
508
|
+
let handler_clone = handler_clone.clone();
|
|
509
|
+
let hooks_clone = hooks_clone.clone();
|
|
510
|
+
axum::routing::delete(move |req: AxumRequest| {
|
|
511
|
+
let handler = handler_clone.clone();
|
|
512
|
+
let hooks = hooks_clone.clone();
|
|
513
|
+
async move {
|
|
514
|
+
let request_data = request_extraction::create_request_data_without_body(
|
|
515
|
+
req.uri(),
|
|
516
|
+
req.method(),
|
|
517
|
+
req.headers(),
|
|
518
|
+
HashMap::new(),
|
|
519
|
+
without_body_options,
|
|
520
|
+
);
|
|
521
|
+
let mut req = req;
|
|
522
|
+
if handler.wants_request_extensions() {
|
|
523
|
+
req.extensions_mut().insert(Arc::new(request_data.clone()));
|
|
524
|
+
}
|
|
525
|
+
call_with_optional_hooks(req, request_data, handler, hooks).await
|
|
526
|
+
}
|
|
527
|
+
})
|
|
528
|
+
}
|
|
529
|
+
HttpMethod::Head => {
|
|
530
|
+
let handler_clone = handler_clone.clone();
|
|
531
|
+
let hooks_clone = hooks_clone.clone();
|
|
532
|
+
axum::routing::head(move |req: AxumRequest| {
|
|
533
|
+
let handler = handler_clone.clone();
|
|
534
|
+
let hooks = hooks_clone.clone();
|
|
535
|
+
async move {
|
|
536
|
+
let request_data = request_extraction::create_request_data_without_body(
|
|
537
|
+
req.uri(),
|
|
538
|
+
req.method(),
|
|
539
|
+
req.headers(),
|
|
540
|
+
HashMap::new(),
|
|
541
|
+
without_body_options,
|
|
542
|
+
);
|
|
543
|
+
let mut req = req;
|
|
544
|
+
if handler.wants_request_extensions() {
|
|
545
|
+
req.extensions_mut().insert(Arc::new(request_data.clone()));
|
|
546
|
+
}
|
|
547
|
+
call_with_optional_hooks(req, request_data, handler, hooks).await
|
|
548
|
+
}
|
|
549
|
+
})
|
|
550
|
+
}
|
|
551
|
+
HttpMethod::Trace => {
|
|
552
|
+
let handler_clone = handler_clone.clone();
|
|
553
|
+
let hooks_clone = hooks_clone.clone();
|
|
554
|
+
axum::routing::trace(move |req: AxumRequest| {
|
|
555
|
+
let handler = handler_clone.clone();
|
|
556
|
+
let hooks = hooks_clone.clone();
|
|
557
|
+
async move {
|
|
558
|
+
let request_data = request_extraction::create_request_data_without_body(
|
|
559
|
+
req.uri(),
|
|
560
|
+
req.method(),
|
|
561
|
+
req.headers(),
|
|
562
|
+
HashMap::new(),
|
|
563
|
+
without_body_options,
|
|
564
|
+
);
|
|
565
|
+
let mut req = req;
|
|
566
|
+
if handler.wants_request_extensions() {
|
|
567
|
+
req.extensions_mut().insert(Arc::new(request_data.clone()));
|
|
568
|
+
}
|
|
569
|
+
call_with_optional_hooks(req, request_data, handler, hooks).await
|
|
570
|
+
}
|
|
571
|
+
})
|
|
572
|
+
}
|
|
573
|
+
HttpMethod::Options => {
|
|
574
|
+
let handler_clone = handler_clone.clone();
|
|
575
|
+
let hooks_clone = hooks_clone.clone();
|
|
576
|
+
axum::routing::options(move |req: AxumRequest| {
|
|
577
|
+
let handler = handler_clone.clone();
|
|
578
|
+
let hooks = hooks_clone.clone();
|
|
579
|
+
async move {
|
|
580
|
+
let request_data = request_extraction::create_request_data_without_body(
|
|
581
|
+
req.uri(),
|
|
582
|
+
req.method(),
|
|
583
|
+
req.headers(),
|
|
584
|
+
HashMap::new(),
|
|
585
|
+
without_body_options,
|
|
586
|
+
);
|
|
587
|
+
let mut req = req;
|
|
588
|
+
if handler.wants_request_extensions() {
|
|
589
|
+
req.extensions_mut().insert(Arc::new(request_data.clone()));
|
|
590
|
+
}
|
|
591
|
+
call_with_optional_hooks(req, request_data, handler, hooks).await
|
|
592
|
+
}
|
|
593
|
+
})
|
|
594
|
+
}
|
|
595
|
+
_ => MethodRouter::new(),
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
#[cfg(test)]
|
|
602
|
+
mod tests {
|
|
603
|
+
use super::*;
|
|
604
|
+
|
|
605
|
+
#[test]
|
|
606
|
+
fn test_http_method_from_str() {
|
|
607
|
+
assert_eq!(HttpMethod::from_str("GET"), Some(HttpMethod::Get));
|
|
608
|
+
assert_eq!(HttpMethod::from_str("POST"), Some(HttpMethod::Post));
|
|
609
|
+
assert_eq!(HttpMethod::from_str("PUT"), Some(HttpMethod::Put));
|
|
610
|
+
assert_eq!(HttpMethod::from_str("PATCH"), Some(HttpMethod::Patch));
|
|
611
|
+
assert_eq!(HttpMethod::from_str("DELETE"), Some(HttpMethod::Delete));
|
|
612
|
+
assert_eq!(HttpMethod::from_str("HEAD"), Some(HttpMethod::Head));
|
|
613
|
+
assert_eq!(HttpMethod::from_str("TRACE"), Some(HttpMethod::Trace));
|
|
614
|
+
assert_eq!(HttpMethod::from_str("OPTIONS"), Some(HttpMethod::Options));
|
|
615
|
+
assert_eq!(HttpMethod::from_str("INVALID"), None);
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
#[test]
|
|
619
|
+
fn test_http_method_expects_body() {
|
|
620
|
+
assert!(!HttpMethod::Get.expects_body());
|
|
621
|
+
assert!(HttpMethod::Post.expects_body());
|
|
622
|
+
assert!(HttpMethod::Put.expects_body());
|
|
623
|
+
assert!(HttpMethod::Patch.expects_body());
|
|
624
|
+
assert!(!HttpMethod::Delete.expects_body());
|
|
625
|
+
assert!(!HttpMethod::Head.expects_body());
|
|
626
|
+
assert!(!HttpMethod::Trace.expects_body());
|
|
627
|
+
assert!(!HttpMethod::Options.expects_body());
|
|
628
|
+
}
|
|
629
|
+
}
|