View Source Orbit.Router (Orbit v0.3.0)

Sort out incoming requests.

Usage

use Orbit.Router injects the following into the module:

import Orbit.Router

def call(req, arg)

Example

defmodule MyAppCapsule.Endpoint do
  use Orbit.Endpoint, otp_app: :my_app
  use Orbit.Router

  pipe &Orbit.Controller.put_layout/2, &MyAppCapsule.LayoutView.main/1
  pipe MyAppCapsule.SetCurrentUser

  route "/static/*path", Orbit.Static, from: :my_app

  group do
    pipe MyAppCapsule.RequireCurrentUser

    route "/messages", MyAppCapsule.MessageController, :index
    route "/messages/:id", MyAppCapsule.MessageController, :show
  end
end

Summary

Functions

Defines a group of routes with a shared pipeline.

Defines a pipe through which requests are processed.

Defines a route that sends a request to a designated pipe.

Functions

Defines a group of routes with a shared pipeline.

Groups have their own pipelines that append any existing pipes from parent groups, or from the router. Groups can be nested.

Example

pipe SetCurrentUser

route "/", HomeController, :show
# ...more routes for all users...

group do
  pipe RequireUser

  route "/profile", ProfileController, :show
  # ...more routes for authenticated users...

  group do
    pipe RequireAdminRole

    route "/admin/users", UserController, :index
    # ...more routes for authenticated admin users...
  end
end
Link to this macro

pipe(pipe, arg \\ nil)

View Source (macro)

Defines a pipe through which requests are processed.

The pipe argument may be either:

  • a module that implements Orbit.Pipe
  • a function capture of a 2-arity function

If the pipe halts the request, the router does not process any further pipes or route matches.

Link to this macro

route(path, pipe, arg \\ nil)

View Source (macro)

Defines a route that sends a request to a designated pipe.

Path segments can contain parameters which are merged into the params field of the request. A wildcard parameter can exist at the very end of a path match.

route "/users/:id/edit", UserController, :edit # => %{"id" => "123"}
route "/posts/*slug", PostController, :show # => %{"slug" => "favorite/cat/pictures"}

The pipe argument may be either:

  • a module that implements the Orbit.Pipe behaviour
  • a 2-arity function capture that accepts the request and an argument

If no route matches the request path, the router responds with a :not_found status.