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:
To define an initial plug pipeline where requests are sent to.
To host web specific configuration for your application.
- It provides a wrapper for starting and stopping the endpoint in a specific web server.
Endpoint configuration
All endpoints are configured directly in the Phoenix application environment. For example:
config :phoenix, YourApp.Endpoint,
secret_key_base: "kjoy3o1zeidquwy1398juxzldjlksahdk3"
Phoenix configuration is split in 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.Debuggerfunctionality 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
: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 base to generate secrets to encode cookies, session and friends. Defaults to nil as it must be set per application.:url- configuration for generating URLs throughout the app. Accepts the host, scheme and port. Defaults to:[host: "localhost"]
Web server
Starting an endpoint as part of a web server can be done by invoking
YourApp.Endpoint.start/0. Stopping the endpoint is done with
YourApp.Endpoint.stop/0. The web server is configured with the
:http and :https options defined above.
The endpoint also provides a url/1 function that generates a url
with the given path according to the :url configuration above:
MyApp.Endpoint.url("/foo/bar")
#=> "http://example.com/foo/bar"
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
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