Phoenix.Endpoint

Defines a Phoenix endpoint.

The endpoint is the boundary where all requests to your web application start. It is also the interface your application provides to the underlying web servers.

Overall, an endpoint has three responsibilities:

Endpoints

An endpoint is simply a module defined with the help of Phoenix.Endpoint. If you have used the phoenix.new generator, an endpoint was automatically generated as part of your application:

defmodule YourApp.Endpoint do
  use Phoenix.Endpoint, otp_app: :your_app

  # plug ...
  # plug ...

  plug :router, YourApp.Router
end

Before being used, an endpoint must be explicitly started as part of your application supervision tree too (which is again done by default in generated applications):

supervisor(YourApp.Endpoint, [])

Endpoint configuration

All endpoints are configured in your application environment. For example:

config :your_app, YourApp.Endpoint,
  secret_key_base: "kjoy3o1zeidquwy1398juxzldjlksahdk3"

Endpoint configuration is split into two categories. Compile-time configuration means the configuration is read during compilation and changing it at runtime has no effect. The compile-time configuration is mostly related to error handling.

On the other hand, runtime configuration is accessed during or after your application is started and can be read through the config/2 function:

YourApp.Endpoint.config(:port)
YourApp.Endpoint.config(:some_config, :default_value)

Compile-time

Runtime

Endpoint API

In the previous section, we have used the config/2 function which is automatically generated in your Endpoint. Here is a summary of all functions defined in your endpoint:

Besides the functions above, it defines also the API expected by Plug for serving requests:

Source

Summary

plug(plug, opts \\ [])

Stores a plug to be executed as part of the pipeline

router(conn, plug)

A macro that can be plugged in order to handle routing errors

Macros

plug(plug, opts \\ [])

Stores a plug to be executed as part of the pipeline.

Source
router(conn, plug)

A macro that can be plugged in order to handle routing errors.

By default, a Phoenix router will raise a Phoenix.Router.NoRouteError struct in case no route is found. This macro wraps the router call so the route error does not pass through.

It also wraps the router call to provide better debugger and error rendering behaviour.

Examples

plug :router, MyApp.Router
Source