AntonRich
So I’m on page 34.
I have a question regarding that quote:
We know that the last plug in the endpoint is the router, and we know we
can find that file in lib/hello_web/router.ex .
A plug is essentially a function that comes from the plug module. If that so why router is being called a plug?
And if plugs are functions why call them plugs(my answer for convenient reasons because they come from plug module)?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kelvinst
So, basically you’re right, a plug is a function. But it’s not any function, it is a 2 arity function where the first argument is a
Plug.Connand the second is a list of options, and also, the return must be always aPlug.Conn. If the function doesn’t follow those rules, it can’t be used as a Plug, because that’s the way the plug lib handle’s them.Basically, all the stuff you put on the router is compiled to a function that follow the same rules, here is the function: phoenix/lib/phoenix/router.ex at main · phoenixframework/phoenix · GitHub.
As answered before, not all functions can be used as plugs, just functions that follow a predefined set of rules. But you are actually kind of right, it’s for convenience, but not because they come from a specific module, actually because it is convenient to have names that classify specific types of functions, “operators” for example, they are mostly functions (some are macros, but ok), but we call operators functions that can be used in the pattern
first_arg X second_arg(like1 + 2), but deep inside, they are just functions that follow a set of rules, just like plugs.In the case of plugs, a behaviour can be used to make sure you implement the correct functions.
peerreynders
The long version:
It would be more accurate to say that a
Pluginvolves some kind of function that satisfies the(Plug.Conn.t, Plug.opts) :: Plug.Conn.ttype signature.The documentation is also clear that:
From what I can tell the 2nd parameter on
(Plug.Conn.t, Plug.opts) :: Plug.Conn.tis useless (***) with a function plug - it exists only for compatibility with the module plug.The documentation’s example module plug looks like this:
There are two functions
call/2andinit/1. Again in this particular case the 2nd parameter_optsforcall/2is unused. Whileinit/1andcall/2exist in the same module there is a difference that is easy to miss:i.e.
init/1is called during compile time to generate a “compile time configuration” for thePlugcall/2is called during run time (while the request is being processed) and is passed the options that were generated byinit/1during compilation.The compilation/runtime split is sometimes used for testing but
init/1gives a module plug a standardized method for compile time configuration (or think of it as server configuration rather than request configuration).Plug.Cowboymakes use of thePlugspecification.init/1is used when the dispatch is created during compile time (or more accurately when the server is starting - but not yet processing any requests):https://github.com/elixir-plug/plug_cowboy/blob/master/lib/plug/cowboy.ex#L252
https://github.com/elixir-plug/plug_cowboy/blob/master/lib/plug/cowboy.ex#L290-L293
call/2is used at runtime in thePlug.Cowboy.Handler:https://github.com/elixir-plug/plug_cowboy/blob/master/lib/plug/cowboy/handler.ex#L6-L26
There is a lot of macro (compile time) activity in
hello_web/router.ex:First of all
use HelloWeb, :routerinjects a lot of code intoHelloWeb.Router. Looking intoHelloWeb.exthere isSo
use HelloWeb, :routerturns intoKernel.apply(HelloWeb, :router)(seeapply/3) at compile time which is equivalent toHelloWeb.router(). Looking up into theHelloWebmodule you’ll find thatrouterfunction:So
use HelloWeb, :routerturns into:The same type of thing happens recursively with
use Phoenix.Router- indeps/phoenix/lib/phoenix/router.ex:The interesting one is
match_dispatch():This is the function that turns your
hello_web/router.exinto a module plug as it provides the compile timeinit/1function and the runtimecall/2function.Edit: *** Correction - not entirely useless. For example with
Plug.Builder.plug/2you can supply the options to be passed to a function plug.benwilson512
AntonRich
@kelvinst @peerreynders Thank you very much. You both clarified things for me. I smiled when I read your answers.