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:
It provides a wrapper for starting and stopping the endpoint as part of a supervision tree.
To define an initial plug pipeline where requests are sent to.
- To host web specific configuration for your application.
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
:debug_errors
- when true, usesPlug.Debugger
functionality for debugging failures in the application. Recomended to be set to true only in development as it allows listing of the application source code during debugging. Defaults to false.:render_errors
- a module representing a view to render templates whenever there is a failure in the application. For example, if the application crashes with a 500 error during a HTML request,render("500.html", assigns)
will be called in the view given to:render_errors
. The default view isMyApp.ErrorView
.
Runtime
:cache_static_lookup
- when true, static assets lookup in the filesystem via thestatic_path
function are cached. Defaults to true.:http
- the configuration for the http server. Currently uses cowboy and accepts all options as defined byPlug.Adapters.Cowboy
. Defaults to false.:https
- the configuration for the https server. Currently uses cowboy and accepts all options as defined byPlug.Adapters.Cowboy
. Defaults to false.:secret_key_base
- a secret key used as a base to generate secrets to encode cookies, session and friends. Defaults to nil as it must be set per application.:server
- when true, starts the web server when the endpoint supervision tree starts. Defaults to false. Themix phoenix.server
task automatically sets this to true.:url
- configuration for generating URLs throughout the app. Accepts the host, scheme and port. Defaults to:[host: "localhost"]
:pubsub
- configuration for this Endpoint’s pubsub adapter. Configuration either requires a:name
of the registered pubsub server or a:name
,:adapter
, and:options
which starts the adapter in the endpoint’s supervision tree. If no name is provided, the name is inflected from the endpoint module. Defaults to:[adapter: Phoenix.PubSub.PG2]
with advanced adapter configuration:
[name: :my_pubsub, adapter: Phoenix.PubSub.Redis, options: [host: "192.168.100.1"]]
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:
start_link()
- starts the Endpoint supervision tree, including its configuration cache and possibly the servers for handling requestsconfig(key, default)
- access the endpoint configuration given by keyconfig_change(changed, removed)
- reload the endpoint configuration on application upgradesurl(path)
- returns the URL for this endpoint with the given pathstatic_path(path)
- returns the static path for a given assetbroadcast_from(from, topic, event, msg)
- proxy toPhoenix.Channel.broadcast_from/4
using this endpoint’s configured pubsub serverbroadcast_from!(from, topic, event, msg)
- proxy toPhoenix.Channel.broadcast_from!/4
using this endpoint’s configured pubsub serverbroadcast(topic, event, msg)
- proxy toPhoenix.Channel.broadcast/3
using this endpoint’s configured pubsub serverbroadcast!(topic, event, msg)
- proxy toPhoenix.Channel.broadcast!/3
using this endpoint’s configured pubsub server
Besides the functions above, it defines also the API expected by Plug for serving requests:
init(opts)
- invoked when starting the endpoint servercall(conn, opts)
- invoked on every request and it simply dispatches to the defined Plug pipeline
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
Stores a plug to be executed as part of the pipeline.
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