Phoenix (One Giant Function)
Learn how Phoenix handles web requests.
We'll cover the following...
To understand how Phoenix handles web requests, and therefore how LiveView handles web requests, you can think of Phoenix requests as simply one big function broken down into smaller plugs. These plugs are stitched together, one after another, as if they were in one big pipeline.
The main sections of the giant Phoenix pipeline are the endpoint, the router, and the application. You can visualize any Phoenix request with this CRC pipeline:
connection_from_request|> endpoint|> router|> custom_application
Each one of these pieces is made up of tiny functions. The custom_application
can be a Phoenix controller, a Phoenix channels application, or a LiveView. We’ll spend most of the lessons on LiveView. For now, let’s discuss the first two parts of this pipeline, the endpoint and router.
The Phoenix endpoint
If Phoenix is a long chain of reducer
functions called plugs
, the endpoint is the constructor at the very beginning of that chain. The endpoint is a simple Elixir module in a file called endpoint.ex
, and it has exactly what we would expect—a pipeline of plugs.
We might ever change our endpoint.ex
file, so we won’t read through it in detail. Instead, we’ll just scan through it to confirm that every Phoenix request goes through an explicit list of functions called plugs.
To do so, we open up lib/pento_web/endpoint.ex
, and we’ll notice that it has a bit of configuration ...