@@ -325,7 +325,7 @@ Sequence
325
325
326
326
:doc: `route ` has a ``then `` function that can be used to produce to sequence
327
327
handlers for a common matcher. It can be used to create a pipeline of
328
- processing for a resource using handling middlewares.
328
+ processing for a resource using middlewares.
329
329
330
330
.. code :: vala
331
331
@@ -393,52 +393,19 @@ responses designed for non-human client.
393
393
Middleware
394
394
----------
395
395
396
- Anything that does not handle the user request, typically by invoking `` next ``,
397
- is considered to be a middleware. Two kind of middleware can coexist to provide
398
- reusable matching and handling capabilities .
396
+ Middlewares are reusable pieces of processing that can perform various work
397
+ from authentication to the delivery of a static resource. They are described in
398
+ the :doc: ` middlewares/index ` document .
399
399
400
- Matching middleware
401
- ~~~~~~~~~~~~~~~~~~~
402
-
403
- These middlewares respect the ``Route.MatcherCallback `` delegate signature.
404
-
405
- The following piece of code is a reusable and generic content negociator:
406
-
407
- .. code :: vala
408
-
409
- public MatcherCallback accept (string content_type) {
410
- return (req) => {
411
- return req.headers.get_one ("Accept") == content_type;
412
- };
413
- }
414
-
415
- It is not really powerful as it does not support fuzzy matching like
416
- ``application/* ``, but it demonstrates the potential capabilities.
417
-
418
- It can conveniently be used as a matcher callback to capture all requests that
419
- accept the ``application/json `` content type as a response.
420
-
421
- .. code :: vala
422
-
423
- app.matcher (accept ("application/json"), (req, res) => {
424
- // produce a JSON output...
425
- });
426
-
427
- Handling middleware
428
- ~~~~~~~~~~~~~~~~~~~
429
-
430
- These middlewares are reusable pieces of processing that can perform various
431
- work from authentication to the delivery of a static resource.
432
-
433
- It is possible for a handling middleware to pass a state to the next handling
434
- route, allowing them to produce content that can be consumed instead of simply
400
+ It is possible for a middleware to pass a state to the next handling route,
401
+ allowing them to produce content that can be consumed instead of simply
435
402
processing the :doc: `vsgi/request ` or :doc: `vsgi/response `.
436
403
437
- A handling middleware can also pass a filtered :doc: `vsgi/request ` or
404
+ A middleware can also pass a filtered :doc: `vsgi/request ` or
438
405
:doc: `vsgi/response ` objects using :doc: `vsgi/filters `,
439
406
440
- These middlewares can be mounted on the routing queue with ``Router.use `` or
441
- conditionally to a matching middleware .
407
+ They can be mounted on the routing queue with ``Router.use `` or conditionally
408
+ to a rule, regular expression or matching callback .
442
409
443
410
.. code :: vala
444
411
@@ -447,6 +414,17 @@ conditionally to a matching middleware.
447
414
next (req, res);
448
415
});
449
416
417
+ One typical pattern is to supply a ``HandlerCallback `` that would be invoked on
418
+ success like it's the case for ``accept ``.
419
+
420
+ .. code :: vala
421
+
422
+ app.get ("", accept ("text/xml", (req, res) => {
423
+ res.body.write_all ("<a>b</a>".data, null);
424
+ }), (req, res) => {
425
+ throw new ClientError.NOT_ACCEPTABLE ("We're only producing 'text/xml here!");
426
+ });
427
+
450
428
The following example shows a middleware that provide a compressed stream over
451
429
the :doc: `vsgi/response ` body.
452
430
@@ -477,8 +455,8 @@ used directly from the handler.
477
455
res.body.write_all ("Hello world!".data, null);
478
456
});
479
457
480
- Alternatively, a handling middleware can be used directly instead of being
481
- attached to a :doc: `route `, the processing will happen in a ``NextCallback ``.
458
+ Alternatively, a middleware can be used directly instead of being attached to
459
+ a :doc: `route `, the processing will happen in a ``NextCallback ``.
482
460
483
461
.. code :: vala
484
462
0 commit comments