raxx v0.17.4 Raxx.Router behaviour

Routing for Raxx applications.

Routes are defined as a match and an action module. Standard Elixir pattern matching is used to apply the match to an incoming request. An action module another implementation of Raxx.Server

Sections group routes that all have the same middleware. Middleware in a section maybe defined as a list, this is useful when all configuration is known at compile-time. Alternativly an arity 1 function can be used. This can be used when middleware require runtime configuration. The argument passed to this function is server initial state.

Examples

defmodule MyRouter do
  use Raxx.Router

  section [{Raxx.Logger, level: :debug}], [
    {%{method: :GET, path: ["ping"]}, Ping},
  ]

  section &web/1, [
    {%{method: :GET, path: []}, HomePage},
    {%{method: :GET, path: ["users"]}, UsersPage},
    {%{method: :GET, path: ["users", _id]}, UserPage},
    {%{method: :POST, path: ["users"]}, CreateUser},
    {_, NotFoundPage}
  ]

  def web(state) do
    [
      {Raxx.Logger, level: state.log_level},
      {MyMiddleware, foo: state.foo}
    ]
  end
end

The original API is kept for backwards compatibility. See previous docs for details.

If the sections DSL does not work for an application it is possible to instead just implement a route/2 function.

Link to this section Summary

Functions

Define a set of routes with a common set of middlewares applied to them

Link to this section Functions

Link to this macro section(middlewares, routes) (macro)

Define a set of routes with a common set of middlewares applied to them.

The first argument may be a list of middlewares; or a function that accepts one argument, the initial state, and returns a list of middleware.

If all settings for a middleware can be decided at compile-time then a list is preferable.

Link to this section Callbacks

Link to this callback route(arg0, term)
route(Raxx.Request.t(), term()) :: Raxx.Stack.t()