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 mix 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 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.

Runtime configuration, instead, is accessed during or after your application is started and can be read and written through the config/2 function:

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

Compile-time configuration

Runtime configuration

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 the functions that are automatically defined in your endpoint.

Paths and URLs

Channels

Endpoint configuration

Plug API

Source

Summary

plug(plug, opts \\ [])

Stores a plug to be executed as part of the pipeline

socket(path, module)

Defines a mount-point for a Socket module to handle channel definitions

Macros

plug(plug, opts \\ [])

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

Source
socket(path, module)

Defines a mount-point for a Socket module to handle channel definitions.

Examples

socket "/ws", MyApp.UserSocket
socket "/ws/admin", MyApp.AdminUserSocket

By default, the given path is a websocket upgrade endpoint, with long-polling fallback. The transports can be configured within the Socket handler. See Phoenix.Socket for more information on defining socket handlers.

Source