Aino.Middleware.Routes (aino v0.1.0)
An Aino set of middleware for dealing with routes and routing
Examples
To use the routes middleware together, see the example below.
def handle(token) do
routes = [
get("/orders", &Orders.index/1),
get("/orders/:id", [&Orders.authorize/1, &Order.show/1]),
post("/orders", &Orders.create/1),
post("/orders/:id", [&Orders.authorize/1, &Order.update/1])
]
middleware = [
Aino.Middleware.common(),
&Aino.Middleware.Routes.routes(&1, routes),
&Aino.Middleware.Routes.match_route/1,
&Aino.Middleware.params/1,
&Aino.Middleware.Routes.handle_route/1
]
Aino.Token.reduce(token, middleware)
end
In the example above you can see why match_route/1
and handle_route/1
are
separate functions, you can perform other middleware in between the two. In this
example, params are merged together via Aino.Middleware.params/1
before
handling the route.
Link to this section Summary
Functions
Create a GET route
Run the matched route from match_route/1
Matches the request against routes on the token
Create a POST route
Set routes for the token
Link to this section Functions
get(path, middleware)
Create a GET route
Examples
routes = [
get("/orders", &Orders.index/1),
get("/orders/:id", [&Orders.authorize/1, &Order.show/1])
]
handle_route(token)
Run the matched route from match_route/1
If no route is present, nothing happens. If a route is present, the middleware stored on the token from the matched request is reduced over.
match_route(token)
Matches the request against routes on the token
Must have routes set via routes/2
before running this middleware.
You should run handle_route/1
after matching the route, otherwise
the route is not run.
Adds the following keys to the token [:path_params, :route_middleware]
post(path, middleware)
Create a POST route
Examples
routes = [
post("/orders", &Orders.create/1),
post("/orders/:id", [&Orders.authorize/1, &Order.update/1])
]
routes(token, routes)
Set routes for the token
Adds the following keys to the token [:routes]