Helios v0.2.2 Helios.Router View Source

Helios router module. When used in onther module, developer can define mappings for paths that are assigned to specific aggregate implementation. This mapping will be translated to routing logic that will help execute aggregate commands in Helios.Pipeline.

As in case of Helios.Aggregate, router too can have plugs, that will be executed in specified order before or after aggregate pipeline is executed.

Example

defmodule MyApp.Router do
  use Helios.Router

  pipeline :secured do
    plug MyApp.Plugs.Authotization
  end

  aggregate "/users", MyApp.Aggregates.UserAggregate, only: [
    :create_user,
    :enable_login,
    :set_password
  ]

  aggregate "/registrations", MyApp.Aggregates.Registration,
    param: :correlation_id,
    only: [
      :create,
      :confirm_email,
      :complete_registration
    ]

  subscribe MyApp.ProcessManagers.UserRegistration, to: [
            {MyApp.Events.UserCreated, :registration_id},
            {MyApp.Events.RegistrationStarted, :registration_id},
            {MyApp.Events.EmailConfirmed, :registration_id},
            {MyApp.Events.LoginEnabled, :correlation_id}
            {MyApp.Events.PasswordInitialized, :correlation_id}
          ]
  end

  scope "/", MyApp.Aggregates do
    pipe_through :secured

    aggregate "/users", UserAggregate, only: [
      :reset_password,
      :change_profile
    ]
  end
end

Link to this section Summary

Functions

Generates a route to handle a :execute request to the given path

Forwards a request at the given path to a plug

Defines a pipeline to send the context through

Defines a context pipeline

Defines a plug inside a pipeline

Generates a route to handle a :process request to the given path

Defines a scope in which routes can be nested

Define a scope with the given path

Defines a scope with the given path and alias

Generates a route to handle a :trace request to the given path

Link to this section Functions

Link to this macro aggregate(path, aggregate) View Source (macro)

See aggregate/4.

Link to this macro aggregate(path, aggregate, opts) View Source (macro)

See aggregate/4.

Link to this macro aggregate(path, aggregate, opts, list) View Source (macro)
Link to this macro execute(path, plug, plug_opts, options \\ []) View Source (macro)

Generates a route to handle a :execute request to the given path.

Link to this macro forward(path, plug, plug_opts \\ [], router_opts \\ []) View Source (macro)

Forwards a request at the given path to a plug.

All paths that match the forwarded prefix will be sent to the forwarded plug. This is useful for sharing a router between applications or even breaking a big router into smaller ones. The router pipelines will be invoked prior to forwarding the connection.

The forwarded plug will be initialized at compile time.

Note, however, that we don’t advise forwarding to another endpoint. The reason is that plugs defined by your app and the forwarded endpoint would be invoked twice, which may lead to errors.

Examples

scope "/", MyApp do
  pipe_through [:secured, :admin]

  forward "/admin", SomeLib.SomePlug
  forward "/transactions", TransactionsRouter
end
Link to this macro match(verb, path, plug, plug_opts, options \\ []) View Source (macro)
Link to this macro pipe_through(pipes) View Source (macro)

Defines a pipeline to send the context through.

See pipeline/2 for more information.

Link to this macro pipeline(plug, list) View Source (macro)

Defines a context pipeline.

Pipelines are defined at the router root and can be used from any scope.

Examples

pipeline :secured do
  plug :token_authentication
  plug :dispatch
end

A scope may then use this pipeline as:

scope "/" do
  pipe_through :secured
end

Every time pipe_through/1 is called, the new pipelines are appended to the ones previously given.

Link to this macro plug(plug, opts \\ []) View Source (macro)

Defines a plug inside a pipeline.

See pipeline/2 for more information.

Link to this macro process(path, plug, plug_opts, options \\ []) View Source (macro)

Generates a route to handle a :process request to the given path.

Link to this macro scope(options, list) View Source (macro)

Defines a scope in which routes can be nested.

Examples

scope path: "/api/v1", as: :api_v1, alias: API.V1 do
  aggregate "/users", UserAggregate
end

The generated route above will match on the path "/api/v1/users/:id/*" and will dispatch to any command to API.V1.UserAggregate.

Options

The supported options are:

  • :path - a string containing the path scope
  • :as - a string or atom containing the named helper scope
  • :alias - an alias (atom) containing the module namespace scope
  • :private - a map of private data to merge into the context when a route matches
  • :assigns - a map of data to merge into the context when a route matches
Link to this macro scope(path, options, list) View Source (macro)

Define a scope with the given path.

This function is a shortcut for:

scope path: path do
  ...
end

Examples

scope "/api/v1", as: :api_v1, alias: API.V1 do
  aggregate "/users", UserAggregate
end
Link to this macro scope(path, alias, options, list) View Source (macro)

Defines a scope with the given path and alias.

This function is a shortcut for:

scope path: path, alias: alias do
  ...
end

Examples

scope "/api/v1", API.V1, as: :api_v1 do
  aggregate "/users", UserAggregate
end
Link to this macro trace(path, plug, plug_opts, options \\ []) View Source (macro)

Generates a route to handle a :trace request to the given path.