Understanding the Plug.Builder module
To add a similar plug pipeline to our controller, we will need to keep track of all the plug/3
calls in the controller and apply those plugs in the correct order whenever the controller receives a request. This sounds like a lot of work to do from scratch, but fortunately, the Plug package provides a module that could help – the Plug.Builder
module.
The Plug.Builder
module can be used to define a pipeline of plugs that are executed one after the other. It defines a plug/2
macro, which is the main interface to build a pipeline by adding a plug to the list of already registered plugs. The plugs in the pipeline are executed in the order of their definition, and the connection returned by the final plug is then passed along to the main plug call.
Here’s an example of this:
defmodule ExamplePlugPipeline do use Plug.Builder plug :plug1 plug :plug2 def plug1(conn, _opts) do ...