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
See aggregate/4
.
See aggregate/4
.
Generates a route to handle a :execute
request to the given path.
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
Defines a pipeline to send the context through.
See pipeline/2
for more information.
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.
Defines a plug inside a pipeline.
See pipeline/2
for more information.
Generates a route to handle a :process
request to the given path.
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
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
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
Generates a route to handle a :trace
request to the given path.