Decree.Middleware behaviour (decree v0.1.0)

Copy Markdown View Source

Middleware provides an extension point to add functions that you want to be called for every command the router dispatches.

Examples include command validation, authorization, and logging.

Implement the Decree.Middleware behaviour in your module and define the before_dispatch/1, after_dispatch/1, and after_failure/1 callback functions.

Example middleware

defmodule NoOpMiddleware do
  @behaviour Decree.Middleware

  alias Decree.Middleware.Pipeline
  import Pipeline

  def before_dispatch(%Pipeline{command: command} = pipeline) do
    pipeline
  end

  def after_dispatch(%Pipeline{command: command} = pipeline) do
    pipeline
  end

  def after_failure(%Pipeline{command: command} = pipeline) do
    pipeline
  end
end

Import the Decree.Middleware.Pipeline module to access convenience functions.

  • assign/3 - puts a key and value into the assigns map
  • halt/1 - stops execution of further middleware downstream and prevents dispatch of the command when used in a before_dispatch callback

Summary

Types

pipeline()

@type pipeline() :: %Decree.Middleware.Pipeline{
  application: term(),
  assigns: term(),
  causation_id: term(),
  command: term(),
  command_uuid: term(),
  correlation_id: term(),
  halted: term(),
  identity: term(),
  identity_opaque: term(),
  identity_prefix: term(),
  metadata: term(),
  response: term()
}

Callbacks

after_dispatch(pipeline)

@callback after_dispatch(pipeline()) :: pipeline()

after_failure(pipeline)

@callback after_failure(pipeline()) :: pipeline()

before_dispatch(pipeline)

@callback before_dispatch(pipeline()) :: pipeline()