Skip to content

Commit 5b46316

Browse files
committed
Use the term 'middleware' exclusively for 'HandlerCallback'
1 parent ae29c42 commit 5b46316

File tree

1 file changed

+22
-44
lines changed

1 file changed

+22
-44
lines changed

docs/router.rst

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ Sequence
325325

326326
:doc:`route` has a ``then`` function that can be used to produce to sequence
327327
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.
329329

330330
.. code:: vala
331331
@@ -393,52 +393,19 @@ responses designed for non-human client.
393393
Middleware
394394
----------
395395

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.
399399

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
435402
processing the :doc:`vsgi/request` or :doc:`vsgi/response`.
436403

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
438405
:doc:`vsgi/response` objects using :doc:`vsgi/filters`,
439406

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.
442409

443410
.. code:: vala
444411
@@ -447,6 +414,17 @@ conditionally to a matching middleware.
447414
next (req, res);
448415
});
449416
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+
450428
The following example shows a middleware that provide a compressed stream over
451429
the :doc:`vsgi/response` body.
452430

@@ -477,8 +455,8 @@ used directly from the handler.
477455
res.body.write_all ("Hello world!".data, null);
478456
});
479457
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``.
482460

483461
.. code:: vala
484462

0 commit comments

Comments
 (0)