What is Plug?
Plug is an Elixir module that allows us to easily define the specifications of a web application. If you are from the Ruby world, you can think of Plug as somewhat like Rack, which allows us to define middleware to read and transform requests/responses in a web application.
Plug allows us to add middleware-like behavior by defining a set of composable operations on a connection struct, Plug.Conn
. This allows us to define behavior in the form of a plug, which can transform the attributes of a Conn
. Unlike in Rack, Conn
contains information related to both the request and the response. This, along with Elixir’s |>
operator, allows us to define the entire request-response cycle in a single pipeline of composable operations:
Figure 3.1 – Plug life cycle