Skip to content
Permalink
Browse files

## Introducing the Endpoint

Now that we’ve installed our dependencies on the Phoenix framework and the web server it uses under the hood, it’s time to define how that web server incorporates into our application.

We do this by defining an “endpoint”, which is our application’s interface into the underlying HTTP web server, and our clients’ interface into our web application.

Following Phoenix conventions, we define our endpoint by creating a `MinimalWeb.Endpoint` module that uses `Phoenix.Endpoint` and specifies the `:name` of our OTP application (`:minimal`):

	defmodule MinimalWeb.Endpoint do
	  use Phoenix.Endpoint, otp_app: :minimal
	end

The [`__using__/1` macro](https://github.com/phoenixframework/phoenix/blob/v1.4/lib/phoenix/endpoint.ex#L488-L497) in `Phoenix.Endpoint` does quite a bit of heaving lifting. Among many other things, it loads the endpoint’s [initial configuration](https://github.com/phoenixframework/phoenix/blob/v1.4/lib/phoenix/endpoint.ex#L499-L515), sets up a [plug pipeline](https://github.com/phoenixframework/phoenix/blob/v1.4/lib/phoenix/endpoint.ex#L588-L613) using [`Plug.Builder`](https://github.com/phoenixframework/phoenix/blob/v1.4/lib/phoenix/endpoint.ex#L590), and [defines helper functions](https://github.com/phoenixframework/phoenix/blob/v1.4/lib/phoenix/endpoint.ex#L621-L634) to describe our endpoint as an OTP process. If you’re curious about how Phoenix works at a low level, start your search here.

`Phoenix.Endpoint` uses the value we provide in `:otp_app` to look up configuration values for our application. Phoenix will complain if we don’t provide a bare minimum configuration entry for our endpoint, so we’ll add that to our `config/config.exs` file:

	config :minimal, MinimalWeb.Endpoint, []

But there are a few configuration values we want to pass to our endpoint, like the host and port we want to serve from. These values are usually environment-dependent, so we’ll add a line at the bottom of our `config/config.exs` to load another configuration file based on our current environment:

	import_config "#{Mix.env()}.exs"

Next, we’ll create a new `config/dev.exs` file that specifies the `:host` and `:port` we’ll serve from during development:

	use Mix.Config

	config :minimal, MinimalWeb.Endpoint,
	  url: [host: "localhost"],
	  http: [port: 4000]

If we were to start our application at this point, we’d still be greeted with `Hello.` printed to the console, rather than a running Phoenix server. We still need to incorporate our Phoenix endpoint into our application.

We do this by turning our `Minimal.Application` into a proper supervisor and instructing it to load our endpoint as a supervised child:

	use Application

	def start(:normal, []) do
	  Supervisor.start_link(
	    [
	      MinimalWeb.Endpoint
	    ],
	    strategy: :one_for_one
	  )
	end

Once we’ve done that, we can fire up our application using `mix phx.server` or `iex -S mix phx.server` and see that our endpoint is listening on `localhost` port `4000`.

Alternatively, if you want to use our old standby of `mix run`, either configure Phoenix to serve all endpoints on startup, [which is what `mix phx.server` does under the hood](https://github.com/phoenixframework/phoenix/blob/v1.4/lib/mix/tasks/phx.server.ex#L31):

	config :phoenix, :serve_endpoints, true

Or configure your application’s endpoint specifically:

	config :minimal, MinimalWeb.Endpoint, server: true
  • Loading branch information...
pcorey committed May 8, 2019
1 parent b5dd4d3 commit 6e654aa2b5ea693e3cd28fbb4cfd46049a6cf852
Showing with 20 additions and 2 deletions.
  1. +4 −0 config/config.exs
  2. +5 −0 config/dev.exs
  3. +8 −2 lib/minimal/application.ex
  4. +3 −0 lib/minimal_web/endpoint.ex
@@ -1,3 +1,7 @@
use Mix.Config

config :minimal, MinimalWeb.Endpoint, []

config :phoenix, :json_library, Jason

import_config "#{Mix.env()}.exs"
@@ -0,0 +1,5 @@
use Mix.Config

config :minimal, MinimalWeb.Endpoint,
url: [host: "localhost"],
http: [port: 4000]
@@ -1,6 +1,12 @@
defmodule Minimal.Application do
use Application

def start(:normal, []) do
IO.puts("Hello.")
{:ok, self()}
Supervisor.start_link(
[
MinimalWeb.Endpoint
],
strategy: :one_for_one
)
end
end
@@ -0,0 +1,3 @@
defmodule MinimalWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :minimal
end

0 comments on commit 6e654aa

Please sign in to comment.
You can’t perform that action at this time.