Aino.Middleware.Routes (aino v0.6.0)

An Aino set of middleware for dealing with routes and routing

examples

Examples

To use the routes middleware together, see the example below.

def routes() do
  [
    get("/orders", &Orders.index/1, as: :orders),
    get("/orders/:id", [&Orders.authorize/1, &Order.show/1], as: :order),
    post("/orders", &Orders.create/1),
    post("/orders/:id", [&Orders.authorize/1, &Order.update/1])
  ]
end

def handle(token) do
  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

Configure routes for the handler

Create a DELETE route

Run the matched route from match_route/1

Matches the request against routes on the token

Set routes for the token

Link to this section Functions

Link to this macro

compile(routes)

(macro)

Configure routes for the handler

For use with a __MODULE__.Routes module to hold for route helper functions

When defining routes, provide the :as option to have _path and _url functions generated for you. E.g. as: :sign_in will generate Routes.sign_in_path/2 and Routes.sign_in_url/2.

Note that when defining routes, you must only define one :as a particular atom. For instance, if you have multiple routes pointing at the url /orders/:id, you should only add as: :order to the first route.

def routes() do
  [
    get("/", &MyApp.Web.Page.root/1, as: :root),
    get("/sign-in", &MyApp.Web.Session.show/1, as: :sign_in),
    post("/sign-in", &MyApp.Web.Session.create/1),
    delete("/sign-out", &MyApp.Web.Session.delete/1, as: :sign_out),
    get("/orders", &MyApp.Web.Orders.index/1, as: :orders),
    get("/orders/:id", &MyApp.Web.Orders.show/1, as: :order)
  ]
end

defmodule MyApp.Web.Handler.Routes do
  require Aino.Middleware.Routes

  Aino.Middleware.Routes.compile(routes())
end
Link to this function

delete(path, middleware, opts \\ [])

Create a DELETE route

examples

Examples

routes = [
  delete("/orders/:id", [&Orders.authorize/1, &Order.delete/1], as: :order)
]
Link to this function

get(path, middleware, opts \\ [])

Create a GET route

examples

Examples

routes = [
  get("/orders", &Orders.index/1, as: :orders),
  get("/orders/:id", [&Orders.authorize/1, &Order.show/1], as: :order)
]
Link to this function

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.

Link to this function

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]

Link to this function

post(path, middleware, opts \\ [])

Create a POST route

examples

Examples

routes = [
  post("/orders", &Orders.create/1, as: :orders),
  post("/orders/:id", [&Orders.authorize/1, &Order.update/1], as: :order)
]
Link to this function

put(path, middleware, opts \\ [])

Create a PUT route

examples

Examples

routes = [
  put("/orders", &Orders.create/1, as: :orders),
  post("/orders/:id", [&Orders.authorize/1, &Order.update/1], as: :order)
]
Link to this function

routes(token, routes)

Set routes for the token

Adds the following keys to the token [:routes]