The Anatomy of a Plug

Before we build our plug, let’s take a deep dive into the Plug library and learn how plugs work from the inside. There are two kinds of plugs: module plugs and function plugs. A function plug is a single function. A module plug is a module that provides two functions with some configuration details. Either way, they work the same.

We have seen both kinds of plugs in use. From the endpoint module in lib/rumbl/endpoint.ex, you can see an example of a module plug:

 plug Plug.Logger

You specify a module plug by providing the module name. In the router, you can see an example of a function plug:

 plug ​:protect_from_forgery

You specify a function plug with the name of the function as an atom. Because a module is just a collection ...

Get Programming Phoenix now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.