~mna/tulip

The tulip.pkg.routes module exports the routes package. It registers a route multiplexer where each request is routed to a specific handler based on the method and path, with optional route-specific middleware applied.

#Dependencies

  • tulip.pkg.middleware

#Configuration

  • Array of tables: the configuration is an array of route tables where each table can have the following fields:
    • middleware: array of string|function = the middleware to apply to this route
    • handler: string|function = the final middleware to apply to this route
    • method: string = the http method that matches this route
    • pattern: string = the Lua pattern that the path must match for this route
    • any other field on the route will be stored on the Request instance under the routeargs field.

The middleware handlers receive the Request and Response instances as arguments as well as a next function to call the next middleware. The pattern does not have to be anchored, and if it contains any captures, those are provided on the Request object in the pathargs field, as an array of values.

The configuration table can also have the following non-array fields:

  • no_such_method (function): handler to call if no route matches the request, but only due to the http method. The not_found handler is called if this field is not set. In addition to the Request and Response arguments, a 3rd table argument is passed, which is the array of http methods supported for this path.
  • not_found (function): handler to call if no route matches the request. The default not found handler is called if this field is not set, which returns 404 with a plain text body.

#Registered Middleware

The following middleware are registered and can be referenced by name in the configuration.

#tulip.pkg.routes

Routes the request to the matching route, or the not_found handler (or no_such_method if it is set and applies to the request). If the request is a HEAD and there is no route found, it tries to find and call a match for a GET and the same path before giving up.

Back to index

About this wiki

commit 3ebfbd288b8e5c95fdf8ce2027a0e94cfa1c8976
Author: Martin Angers <martin.n.angers@gmail.com>
Date:   2021-02-25T14:07:12-05:00

Update to reflect Request:validate_body
Clone this wiki
https://git.sr.ht/~mna/tulip-wiki (read-only)
git@git.sr.ht:~mna/tulip-wiki (read/write)