Skip to content

Commit

Permalink
basepath: Provide minimal documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
arteymix committed Mar 23, 2016
1 parent e1a1e3a commit e8df8c1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
52 changes: 52 additions & 0 deletions docs/middlewares/basepath.rst
@@ -0,0 +1,52 @@
Basepath
========

The ``basepath`` middleware allow a better isolation when composing routers by
stripping a prefix on the :doc:`../vsgi/request` URI.

The middleware strips and forwards requests which match the provided base path.
If the resulting path is empty, it fallbacks to a root ``/``.

Error which use their message as a ``Location`` header are automatically
prefixed by the base path.

::

var user = new Router ();

user.get ("/<int:id>", (req, res) => {
// ...
});

user.post ("/", (req, res) => {
throw new Success.CREATED ("/5");
});

app.use (basepath ("/user", user.handle));

app.status (Soup.Status.CREATED, (req, res) => {
assert ("/user/5" == context["message"]);
});

If ``next`` is called while forwarding or an error is thrown, the original path
is restored.

::

user.get ("/<int:id>", (req, res, next) => {
return next (req, res); // path is '/5'
});

app.use (basepath ("/user", user.handle));

app.use ((req, res) => {
// path is '/user/5'
});

One common pattern is to provide a path-based fallback when using the
:doc:`subdomain` middleware.

::

app.use (subdomain ("api", api.handle));
app.use (basepath ("/api", api.handle));
1 change: 1 addition & 0 deletions docs/middlewares/index.rst
Expand Up @@ -2,5 +2,6 @@ Middlewares
===========

.. toctree::
basepath
server-sent-events
subdomain
2 changes: 1 addition & 1 deletion src/valum/valum-basepath.vala
Expand Up @@ -35,7 +35,7 @@ namespace Valum {
*
* @since 0.3
*
* @param path
* @param path path prefix stripped on forwarded requests
* @param forward callback used to forward the request
*/
public HandlerCallback basepath (string path, owned HandlerCallback forward) {
Expand Down

0 comments on commit e8df8c1

Please sign in to comment.