defmodule Phoenix.Endpoint do @moduledoc ~S""" 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 provide a wrapper for starting and stopping the endpoint as part of a supervision tree * to define an initial plug pipeline for requests to pass through * 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 `mix phx.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 Endpoints must be explicitly started as part of your application supervision tree. Endpoints are added by default to the supervision tree in generated applications. Endpoints can be added to the supervision tree as follows: children = [ 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 and instrumentation. Runtime configuration, instead, is accessed during or after your application is started and can be read through the `c:config/2` function: YourApp.Endpoint.config(:port) YourApp.Endpoint.config(:some_config, :default_value) ### Dynamic configuration For dynamically configuring the endpoint, such as loading data from environment variables or configuration files, Phoenix invokes the `init/2` callback on the endpoint, passing a `:supervisor` atom as first argument and the endpoint configuration as second. All of Phoenix configuration, except the Compile-time configuration below can be set dynamically from the `c:init/2` callback. ### Compile-time configuration * `:code_reloader` - when `true`, enables code reloading functionality. For code the list of code reloader configuration options see `Phoenix.CodeReloader.reload!/1` * `:debug_errors` - when `true`, uses `Plug.Debugger` functionality for debugging failures in the application. Recommended to be set to `true` only in development as it allows listing of the application source code during debugging. Defaults to `false` * `:render_errors` - responsible for rendering 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`. Defaults to: [view: MyApp.ErrorView, accepts: ~w(html), layout: false] The default format is used when none is set in the connection * `:instrumenters` - a list of instrumenter modules whose callbacks will be fired on instrumentation events. Read more on instrumentation in the "Instrumentation" section below ### Runtime configuration * `:cache_static_manifest` - a path to a json manifest file that contains static files and their digested version. This is typically set to "priv/static/cache_manifest.json" which is the file automatically generated by `mix phx.digest` * `:check_origin` - configure transports to check `origin` header or not. May be `false`, `true`, a list of hosts that are allowed, or a function provided as MFA tuple. Hosts also support wildcards. For example, using a list of hosts: check_origin: ["//phoenixframework.org", "//*.example.com"] or a custom MFA function: check_origin: {MyAppWeb.Auth, :my_check_origin?, []} The MFA is invoked with the request `%URI{}` as the first argument, followed by arguments in the MFA list Defaults to `true`. * `:http` - the configuration for the HTTP server. Currently uses Cowboy and accepts all options as defined by [`Plug.Cowboy`](https://hexdocs.pm/plug_cowboy/). Defaults to `false` * `:https` - the configuration for the HTTPS server. Currently uses Cowboy and accepts all options as defined by [`Plug.Cowboy`](https://hexdocs.pm/plug_cowboy/). Defaults to `false` * `:force_ssl` - ensures no data is ever sent via HTTP, always redirecting to HTTPS. It expects a list of options which are forwarded to `Plug.SSL`. By default it sets the "strict-transport-security" header in HTTPS requests, forcing browsers to always use HTTPS. If an unsafe request (HTTP) is sent, it redirects to the HTTPS version using the `:host` specified in the `:url` configuration. To dynamically redirect to the `host` of the current request, set `:host` in the `:force_ssl` configuration to `nil` * `:secret_key_base` - a secret key used as a base to generate secrets for encrypting and signing data. For example, cookies and tokens are signed by default, but they may also be encrypted if desired. 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`. The `mix phx.server` task automatically sets this to `true` * `:url` - configuration for generating URLs throughout the app. Accepts the `:host`, `:scheme`, `:path` and `:port` options. All keys except `:path` can be changed at runtime. Defaults to: [host: "localhost", path: "/"] The `:port` option requires either an integer, string, or `{:system, "ENV_VAR"}`. When given a tuple like `{:system, "PORT"}`, the port will be referenced from `System.get_env("PORT")` at runtime as a workaround for releases where environment specific information is loaded only at compile-time. The `:host` option requires a string or `{:system, "ENV_VAR"}`. Similar to `:port`, when given a tuple like `{:system, "HOST"}`, the host will be referenced from `System.get_env("HOST")` at runtime. The `:scheme` option accepts `"http"` and `"https"` values. Default value is infered from top level `:http` or `:https` option. It is useful when hosting Phoenix behind a load balancer or reverse proxy and terminating SSL there. The `:path` option can be used to override root path. Useful when hosting Phoenix behind a reverse proxy with URL rewrite rules * `:static_url` - configuration for generating URLs for static files. It will fallback to `url` if no option is provided. Accepts the same options as `url` * `:watchers` - a set of watchers to run alongside your server. It expects a list of tuples containing the executable and its arguments. Watchers are guaranteed to run in the application directory, but only when the server is enabled. For example, the watcher below will run the "watch" mode of the webpack build tool when the server starts. You can configure it to whatever build tool or command you want: [node: ["node_modules/webpack/bin/webpack.js", "--mode", "development", "--watch-stdin"]] The `:cd` option can be used on a watcher to override the folder from which the watcher will run. By default this will be the project's root: `File.cwd!()` [node: ["node_modules/webpack/bin/webpack.js", "--mode", "development", "--watch-stdin"], cd: "my_frontend"] * `:live_reload` - configuration for the live reload option. Configuration requires a `:patterns` option which should be a list of file patterns to watch. When these files change, it will trigger a reload. If you are using a tool like [pow](http://pow.cx) in development, you may need to set the `:url` option appropriately. live_reload: [ url: "ws://localhost:4000", patterns: [ ~r{priv/static/.*(js|css|png|jpeg|jpg|gif)$}, ~r{web/views/.*(ex)$}, ~r{web/templates/.*(eex)$} ] ] * `:pubsub` - configuration for this endpoint's pubsub adapter. Configuration either requires a `:name` of the registered pubsub server or a `:name` and `:adapter` pair. The pubsub name and adapter are compile time configuration, while the remaining options are runtime. The given adapter and name pair will be started as part of the supervision tree. If no adapter is specified, the pubsub system will work by sending events and subscribing to the given name. Defaults to: [adapter: Phoenix.PubSub.PG2, name: MyApp.PubSub] It also supports custom adapter configuration: [name: :my_pubsub, adapter: Phoenix.PubSub.Redis, host: "192.168.100.1"] ## Endpoint API In the previous section, we have used the `c:config/2` function that is automatically generated in your endpoint. Here's a list of all the functions that are automatically defined in your endpoint: * for handling paths and URLs: `c:struct_url/0`, `c:url/0`, `c:path/1`, `c:static_url/0`, and `c:static_path/1` * for handling channel subscriptions: `c:subscribe/2` and `c:unsubscribe/1` * for broadcasting to channels: `c:broadcast/3`, `c:broadcast!/3`, `c:broadcast_from/4`, and `c:broadcast_from!/4` * for configuration: `c:start_link/0`, `c:config/2`, and `c:config_change/2` * for instrumentation: `c:instrument/3` * as required by the `Plug` behaviour: `c:Plug.init/1` and `c:Plug.call/2` ## Instrumentation Phoenix supports instrumentation through an extensible API. Each endpoint defines an `c:instrument/3` macro that both users and Phoenix internals can call to instrument generic events. This macro is responsible for measuring the time it takes for the event to be processed and for notifying a list of interested instrumenter modules of this measurement. You can configure this list of instrumenter modules in the compile-time configuration of your endpoint. (see the `:instrumenters` option above). The way these modules express their interest in events is by exporting public functions where the name of each function is the name of an event. For example, if someone instruments the `:render_view` event, then each instrumenter module interested in that event will have to export `render_view/3`. ### Callbacks cycle The event callback sequence is: 1. The event callback is called *before* the event happens (in this case, before the view is rendered) with the atom `:start` as the first argument; see the "Before clause" section below 2. The event occurs (in this case, the view is rendered) 3. The same event callback is called again, this time with the atom `:stop` as the first argument; see the "After clause" section below The second and third argument that each event callback takes depends on the callback being an "after" or a "before" callback i.e. it depends on the value of the first argument, `:start` or `:stop`. For this reason, most of the time you will want to define (at least) two separate clauses for each event callback, one for the "before" and one for the "after" callbacks. All event callbacks are run in the same process that calls the `c:instrument/3` macro; hence, instrumenters should be careful to avoid performing blocking actions. If an event callback fails in any way (exits, throws, or raises), it won't affect anything as the error is caught, but the failure will be logged. Note that "after" callbacks are not guaranteed to be called as, for example, a link may break before they've been called. #### "Before" clause When the first argument to an event callback is `:start`, the signature of that callback is: event_callback(:start, compile_metadata, runtime_metadata) where: * `compile_metadata` is a map of compile-time metadata about the environment where `instrument/3` has been called. It contains the module where the instrumentation is happening (under the `:module` key), the file and line (`:file` and `:line`), and the function inside which the instrumentation is happening (under `:function`). This information can be used arbitrarily by the callback * `runtime_metadata` is a map of runtime data that the instrumentation passes to the callbacks. This can be used for any purposes: for example, when instrumenting the rendering of a view, the name of the view could be passed in these runtime data so that instrumenters know which view is being rendered (`instrument(:view_render, %{view: "index.html"}, fn ...)`) #### "After" clause When the first argument to an event callback is `:stop`, the signature of that callback is: event_callback(:stop, time_diff, result_of_before_callback) where: * `time_diff` is an integer representing the time it took to execute the instrumented function **in native units** * `result_of_before_callback` is the return value of the "before" clause of the same `event_callback`. This is a means of passing data from the "before" clause to the "after" clause when instrumenting The return value of each "before" event callback will be stored and passed to the corresponding "after" callback. ### Using instrumentation Each Phoenix endpoint defines its own `instrument/3` macro. This macro is called like this: require MyApp.Endpoint MyApp.Endpoint.instrument(:render_view, %{view: "index.html"}, fn -> # actual view rendering end) All the instrumenter modules that export a `render_view/3` function will be notified of the event so that they can perform their respective actions. ### Phoenix default events By default, Phoenix instruments the following events: * `:phoenix_controller_call` - the entire controller pipeline. The `%Plug.Conn{}` is passed as runtime metadata * `:phoenix_controller_render` - the rendering of a view from a controller. The map of runtime metadata passed to instrumentation callbacks has the `:view` key - for the name of the view, e.g. `HexWeb.ErrorView`, the `:template` key - for the name of the template, e.g., `"index.html"`, the `:format` key - for the format of the template, and the `:conn` key - containing the `%Plug.Conn{}` * `:phoenix_error_render` - the rendering of an error view when an exception, throw, or exit is caught. The map of runtime metadata contains the `:status` key of the error's HTTP status code, the `:conn` key containg the `%Plug.Conn{}`, as well as the `:kind`, `:reason`, and `:stacktrace` of the caught error * `:phoenix_channel_join` - the joining of a channel. The `%Phoenix.Socket{}` and join params are passed as runtime metadata via `:socket` and `:params` * `:phoenix_channel_receive` - the receipt of an incoming message over a channel. The `%Phoenix.Socket{}`, payload, event, and ref are passed as runtime metadata via `:socket`, `:params`, `:event`, and `:ref` * `:phoenix_socket_connect` - the connection of the user socket transport. The map of runtime metadata contains the `:transport`, `:params`, a map of `connect_info`, and the `:user_socket` module. ### Dynamic instrumentation If you want to instrument a piece of code, but the endpoint that should instrument it (the one that contains the `c:instrument/3` macro you want to use) is not known at compile time, only at runtime, you can use the `Phoenix.Endpoint.instrument/4` macro. Refer to its documentation for more information. """ @type topic :: String.t @type event :: String.t @type msg :: map require Logger # Configuration @doc """ Starts the endpoint supervision tree. Starts endpoint's configuration cache and possibly the servers for handling requests. """ @callback start_link() :: Supervisor.on_start @doc """ Access the endpoint configuration given by key. """ @callback config(key :: atom, default :: term) :: term @doc """ Reload the endpoint configuration on application upgrades. """ @callback config_change(changed :: term, removed :: term) :: term @doc """ Initialize the endpoint configuration. Invoked when the endpoint supervisor starts, allows dynamically configuring the endpoint from system environment or other runtime sources. """ @callback init(:supervisor, config :: Keyword.t) :: {:ok, Keyword.t} # Paths and URLs @doc """ Generates the endpoint base URL, but as a `URI` struct. """ @callback struct_url() :: URI.t @doc """ Generates the endpoint base URL without any path information. """ @callback url() :: String.t @doc """ Generates the path information when routing to this endpoint. """ @callback path(path :: String.t) :: String.t @doc """ Generates the static URL without any path information. """ @callback static_url() :: String.t @doc """ Generates a route to a static file in `priv/static`. """ @callback static_path(path :: String.t) :: String.t # Channels @doc """ Subscribes the caller to the given topic. See `Phoenix.PubSub.subscribe/3` for options. """ @callback subscribe(topic, opts :: Keyword.t) :: :ok | {:error, term} @doc """ Unsubscribes the caller from the given topic. """ @callback unsubscribe(topic) :: :ok | {:error, term} @doc """ Broadcasts a `msg` as `event` in the given `topic`. """ @callback broadcast(topic, event, msg) :: :ok | {:error, term} @doc """ Broadcasts a `msg` as `event` in the given `topic`. Raises in case of failures. """ @callback broadcast!(topic, event, msg) :: :ok | no_return @doc """ Broadcasts a `msg` from the given `from` as `event` in the given `topic`. """ @callback broadcast_from(from :: pid, topic, event, msg) :: :ok | {:error, term} @doc """ Broadcasts a `msg` from the given `from` as `event` in the given `topic`. Raises in case of failures. """ @callback broadcast_from!(from :: pid, topic, event, msg) :: :ok | no_return # Instrumentation @doc """ Allows instrumenting operation defined by `function`. `runtime_metadata` may be omitted and defaults to `nil`. Read more about instrumentation in the "Instrumentation" section. """ @macrocallback instrument(instrument_event :: Macro.t, runtime_metadata :: Macro.t, function :: Macro.t) :: Macro.t @doc false defmacro __using__(opts) do quote do @behaviour Phoenix.Endpoint unquote(config(opts)) unquote(pubsub()) unquote(plug()) unquote(server()) end end defp config(opts) do quote do @otp_app unquote(opts)[:otp_app] || raise "endpoint expects :otp_app to be given" var!(config) = Phoenix.Endpoint.Supervisor.config(@otp_app, __MODULE__) var!(code_reloading?) = var!(config)[:code_reloader] # Avoid unused variable warnings _ = var!(code_reloading?) @doc false def init(_key, config) do {:ok, config} end defoverridable init: 2 end end @doc false def __pubsub_server__!(module) do if server = module.__pubsub_server__() do server else raise ArgumentError, """ no :pubsub server configured at, please setup :pubsub in your config. By default this looks like: config :my_app, MyApp.PubSub, ..., pubsub: [name: MyApp.PubSub, adapter: Phoenix.PubSub.PG2] """ end end defp pubsub() do quote do def __pubsub_server__ do Phoenix.Config.cache(__MODULE__, :__phoenix_pubsub_server__, &Phoenix.Endpoint.Supervisor.pubsub_server/1) end # TODO v2: Remove pid version @doc false def subscribe(pid, topic) when is_pid(pid) and is_binary(topic) do IO.warn "#{__MODULE__}.subscribe/2 is deprecated, please use subscribe/1" Phoenix.PubSub.subscribe(Phoenix.Endpoint.__pubsub_server__!(__MODULE__), pid, topic, []) end def subscribe(pid, topic, opts) when is_pid(pid) and is_binary(topic) and is_list(opts) do Phoenix.PubSub.subscribe(Phoenix.Endpoint.__pubsub_server__!(__MODULE__), pid, topic, opts) end def subscribe(topic) when is_binary(topic) do Phoenix.PubSub.subscribe(Phoenix.Endpoint.__pubsub_server__!(__MODULE__), topic, []) end def subscribe(topic, opts) when is_binary(topic) and is_list(opts) do Phoenix.PubSub.subscribe(Phoenix.Endpoint.__pubsub_server__!(__MODULE__), topic, opts) end # TODO v2: Remove pid version @doc false def unsubscribe(pid, topic) do IO.warn "#{__MODULE__}.unsubscribe/2 is deprecated, please use unsubscribe/1" Phoenix.PubSub.unsubscribe(Phoenix.Endpoint.__pubsub_server__!(__MODULE__), topic) end def unsubscribe(topic) do Phoenix.PubSub.unsubscribe(Phoenix.Endpoint.__pubsub_server__!(__MODULE__), topic) end def broadcast_from(from, topic, event, msg) do Phoenix.Channel.Server.broadcast_from(Phoenix.Endpoint.__pubsub_server__!(__MODULE__), from, topic, event, msg) end def broadcast_from!(from, topic, event, msg) do Phoenix.Channel.Server.broadcast_from!(Phoenix.Endpoint.__pubsub_server__!(__MODULE__), from, topic, event, msg) end def broadcast(topic, event, msg) do Phoenix.Channel.Server.broadcast(Phoenix.Endpoint.__pubsub_server__!(__MODULE__), topic, event, msg) end def broadcast!(topic, event, msg) do Phoenix.Channel.Server.broadcast!(Phoenix.Endpoint.__pubsub_server__!(__MODULE__), topic, event, msg) end end end defp plug() do quote location: :keep do use Plug.Builder import Phoenix.Endpoint Module.register_attribute(__MODULE__, :phoenix_sockets, accumulate: true) if force_ssl = Phoenix.Endpoint.__force_ssl__(__MODULE__, var!(config)) do plug Plug.SSL, force_ssl end if var!(config)[:debug_errors] do use Plug.Debugger, otp_app: @otp_app, banner: {Phoenix.Endpoint.RenderErrors, :__debugger_banner__, []}, style: [ primary: "#EB532D", logo: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJEAAABjCAYAAACbguIxAAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACxMBAJqcGAAAHThJREFUeAHtPWlgVOW197vbLNkTFoFQlixAwpIVQZ8ooE+tRaBWdoK4VF5tfe2r1tb2ta611r6n9b1Xd4GETRGxIuJSoKACAlkIkD0hsiRoIHtmues7J3LpOJ2Z3Jm5yUxi5s+991vOOd+5Z777fWf7CGXA79Ct46ZGmyPnshw9WaX5qTSlJBCKjqU51aoohKVUivaIRqUUmlactEK3iCp1gablTztsnZ9kbK16w2P7wcKw5AAJhKqiBWlzIyIjVrKsnKtQ7HiiqiaGZQOC5Qm/JAkiUekqSha2X7/x2JP1FOXw1G6wLDw4oPvFl94+ZVmkib9HJnQuy7MRfUW+qoqSLMtHWi60PzB9Z+2BvsI7iEc/B3wK0d8Wjk8dHRX7B5hjbqBZU6R+sMa3VBWFUiSxqLmh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SEJvzU5oaWcUOEtKwUOBARPtWUOCRuTGppYeoyQ0+vv7dUAIketLQNeFyLj4H0Es2NUwNyX6sxDH0GnI5iECU2yQ//AcIVKjSHO1YofzJMU4K+0XhJb2aKoN8VkddERUNDuUoUgyy/LZkBA9FRIjTwJfnTjNxbe1SViU+W7hVlf6BuL9gBMi95eEXpR8FD+NIfRkQaFHw0vvTkNM06pNoZmLquxophWqrl2mz3W22o7pTeLgjkd7xoxoIybHrDHxzI8hiDGq9VzzNdN31x3R6gfidcALkZEv7cDNyZmxUZbrBNXZ8Pmxzt095QlAAcazWXsK/jOSxlDAGhQiP7iOkaSWePOdRGZmghfBKAJZrWSacmBKOzgbsxFcaY/YHLZ39WZd8wN1WDcdFKIAX0/Zooz7OAv7EHgJjnYHAX5P7USRPty3t3qN5gjm3mYgPQ8KUZBvs2hB2tzouIh1kIE80R0UhiBDvNnatM3F97jXDaTnQSEy6G1WrMh43WSyrPYEDqMsxhcUTvJUNxDKBoXIwLdYsnTyimizeb2nJBGSIJxKKSgcbyC6sAE1KEQGvwp0gh86JOEouOh2qxJcwQuiUDIhvzDTtWwg3HtWuQ6EkYVoDJjw4PyZC9PRQOtOAs/xGRXLpv3Bvby/Pw8KUS+8was/ri+52NW+UJHAPuL2482mhzAixa24Xz8OClEvvT605jd3tS6ApKHfOGKCEIaaM3NkUS+hDQnYQSHqRbajIH1WeCZRFaVvhCujbqlmdc5LvYi6T0EPLqz7iN14Wjdtivg1C0eha9Z/OB/x0P49lbf0d4XkoBD1kRBpaNChLiYhYY2JUufIrDpCEkkR5FrE3No9ZmnVYITb9f8BhSZnYemqCy4AAAAASUVORK5CYII=" ] end # Compile after the debugger so we properly wrap it. @before_compile Phoenix.Endpoint @phoenix_render_errors var!(config)[:render_errors] end end defp server() do quote location: :keep, unquote: false do @doc """ Returns the child specification to start the endpoint under a supervision tree. """ def child_spec(opts) do %{ id: __MODULE__, start: {__MODULE__, :start_link, [opts]}, type: :supervisor } end @doc """ Starts the endpoint supervision tree. """ def start_link(_opts \\ []) do Phoenix.Endpoint.Supervisor.start_link(@otp_app, __MODULE__) end @doc """ Returns the endpoint configuration for `key` Returns `default` if the key does not exist. """ def config(key, default \\ nil) do case :ets.lookup(__MODULE__, key) do [{^key, val}] -> val [] -> default end end @doc """ Reloads the configuration given the application environment changes. """ def config_change(changed, removed) do Phoenix.Endpoint.Supervisor.config_change(__MODULE__, changed, removed) end @doc """ Generates the endpoint base URL without any path information. It uses the configuration under `:url` to generate such. """ def url do Phoenix.Config.cache(__MODULE__, :__phoenix_url__, &Phoenix.Endpoint.Supervisor.url/1) end @doc """ Generates the static URL without any path information. It uses the configuration under `:static_url` to generate such. It falls back to `:url` if `:static_url` is not set. """ def static_url do Phoenix.Config.cache(__MODULE__, :__phoenix_static_url__, &Phoenix.Endpoint.Supervisor.static_url/1) end @doc """ Generates the endpoint base URL but as a `URI` struct. It uses the configuration under `:url` to generate such. Useful for manipulating the URL data and passing it to URL helpers. """ def struct_url do Phoenix.Config.cache(__MODULE__, :__phoenix_struct_url__, &Phoenix.Endpoint.Supervisor.struct_url/1) end @doc """ Returns the host for the given endpoint. """ def host do Phoenix.Config.cache(__MODULE__, :__phoenix_host__, &Phoenix.Endpoint.Supervisor.host/1) end @doc """ Generates the path information when routing to this endpoint. """ def path(path) do Phoenix.Config.cache(__MODULE__, :__phoenix_path__, &Phoenix.Endpoint.Supervisor.path/1) <> path end @doc """ Generates the script name. """ def script_name do Phoenix.Config.cache(__MODULE__, :__phoenix_script_name__, &Phoenix.Endpoint.Supervisor.script_name/1) end @doc """ Generates a route to a static file in `priv/static`. """ def static_path(path) do Phoenix.Config.cache(__MODULE__, :__phoenix_static__, &Phoenix.Endpoint.Supervisor.static_path/1) <> Phoenix.Config.cache(__MODULE__, {:__phoenix_static__, path}, &Phoenix.Endpoint.Supervisor.static_path(&1, path)) end end end @doc false def __force_ssl__(module, config) do if force_ssl = config[:force_ssl] do Keyword.put_new(force_ssl, :host, {module, :host, []}) end end @doc false defmacro __before_compile__(%{module: module}) do sockets = Module.get_attribute(module, :phoenix_sockets) otp_app = Module.get_attribute(module, :otp_app) instrumentation = Phoenix.Endpoint.Instrument.definstrument(otp_app, module) dispatches = for {path, socket, socket_opts} <- sockets, {path, type, conn_ast, socket, opts} <- socket_paths(module, path, socket, socket_opts) do quote do defp do_handler(unquote(path), conn, _opts) do {unquote(type), unquote(conn_ast), unquote(socket), unquote(Macro.escape(opts))} end end end quote do defoverridable [call: 2] # Inline render errors so we set the endpoint before calling it. def call(conn, opts) do conn = put_in conn.secret_key_base, config(:secret_key_base) conn = put_in conn.script_name, script_name() conn = Plug.Conn.put_private(conn, :phoenix_endpoint, __MODULE__) try do super(conn, opts) rescue e in Plug.Conn.WrapperError -> %{conn: conn, kind: kind, reason: reason, stack: stack} = e Phoenix.Endpoint.RenderErrors.__catch__(conn, kind, reason, stack, @phoenix_render_errors) catch kind, reason -> stack = System.stacktrace() Phoenix.Endpoint.RenderErrors.__catch__(conn, kind, reason, stack, @phoenix_render_errors) end end @doc false def __sockets__, do: unquote(Macro.escape(sockets)) @doc false def __handler__(%{path_info: path} = conn, opts), do: do_handler(path, conn, opts) unquote(instrumentation) unquote(dispatches) defp do_handler(_path, conn, opts), do: {:plug, conn, __MODULE__, opts} end end defp socket_paths(endpoint, path, socket, opts) do paths = [] websocket = Keyword.get(opts, :websocket, true) longpoll = Keyword.get(opts, :longpoll, false) paths = if websocket do config = socket_config(websocket, Phoenix.Transports.WebSocket) {conn_ast, match_path} = socket_path(path, config) [{match_path, :websocket, conn_ast, socket, config} | paths] else paths end paths = if longpoll do config = socket_config(longpoll, Phoenix.Transports.LongPoll) plug_init = {endpoint, socket, config} {conn_ast, match_path} = socket_path(path, config) [{match_path, :plug, conn_ast, Phoenix.Transports.LongPoll, plug_init} | paths] else paths end paths end defp socket_path(path, config) do end_path_fragment = Keyword.fetch!(config, :path) {vars, path} = String.split(path <> "/" <> end_path_fragment, "/", trim: true) |> Enum.join("/") |> Plug.Router.Utils.build_path_match() conn_ast = if vars == [] do quote do conn end else params_map = {:%{}, [], Plug.Router.Utils.build_path_params_match(vars)} quote do params = unquote(params_map) %{conn | path_params: params, params: params} end end {conn_ast, path} end defp socket_config(true, module), do: module.default_config() defp socket_config(config, module), do: Keyword.merge(module.default_config(), config) ## API @doc """ Defines a websocket/longpoll mount-point for a socket. Note: for backwards compatibility purposes, the `:websocket` and `:longpoll` options only have an effect if the socket given as argument has no `transport` declarations in it. ## Options * `:websocket` - controls the websocket configuration. Defaults to `true`. May be false or a keyword list of options. See "Shared configuration" and "WebSocket configuration" for the whole list * `:longpoll` - controls the longpoll configuration. Defaults to `false`. May be true or a keyword list of options. See "Shared configuration" and "Longpoll configuration" for the whole list * `:shutdown` - the maximum shutdown time of each channel when the endpoint is shutting down. Applies only to channel-based sockets ## Examples socket "/ws", MyApp.UserSocket socket "/ws/admin", MyApp.AdminUserSocket, longpoll: true, websocket: [compress: true] ## Path params It is possible to include variables in the path, these will be available in the `params` that are passed to the socket. socket "/ws/:user_id", MyApp.UserSocket, websocket: [path: "/project/:project_id"] Note: This feature is not supported with the Cowboy 1 adapter. ## Shared configuration The configuration below can be given to both `:websocket` and `:longpoll` keys: * `:path` - the path to use for the transport. Will default to the transport name ("/websocket" or "/longpoll") * `:serializer` - a list of serializers for messages. See `Phoenix.Socket` for more information * `:transport_log` - if the transport layer itself should log and, if so, the level * `:check_origin` - if we should check the origin of requests when the origin header is present. It defaults to true and, in such cases, it will check against the host value in `YourApp.Endpoint.config(:url)[:host]`. It may be set to `false` (not recommended) or to a list of explicitly allowed origins. check_origin: ["https://example.com", "//another.com:888", "//other.com"] Note: To connect from a native app be sure to either have the native app set an origin or allow any origin via `check_origin: false` * `:code_reloader` - enable or disable the code reloader. Defaults to your endpoint configuration * `:connect_info` - a list of keys that represent data to be copied from the transport to be made available in the user socket `connect/3` callback The valid keys are: * `:peer_data` - the result of `Plug.Conn.get_peer_data/1` * `:x_headers` - all request headers that have an "x-" prefix * `:uri` - a `%URI{}` with information from the conn Arbitrary keywords may also appear following the above valid keys, which is useful for passing custom connection information to the socket. For example: socket "/socket", AppWeb.UserSocket, websocket: [ connect_info: [:peer_data, :x_headers, :uri] ] With arbitrary keywords: socket "/socket", AppWeb.UserSocket, websocket: [ connect_info: [:uri, signing_salt: "NZIguRPO"] ] ## Websocket configuration The following configuration applies only to `:websocket`. * `:timeout` - the timeout for keeping websocket connections open after it last received data, defaults to 60_000ms ## Longpoll configuration The following configuration applies only to `:longpoll`: * `:window_ms` - how long the client can wait for new messages in its poll request * `:pubsub_timeout_ms` - how long a request can wait for the pubsub layer to respond * `:crypto` - options for verifying and signing the token, accepted by `Phoenix.Token`. By default tokens are valid for 2 weeks """ defmacro socket(path, module, opts \\ []) do # Tear the alias to simply store the root in the AST. # This will make Elixir unable to track the dependency # between endpoint <-> socket and avoid recompiling the # endpoint (alongside the whole project ) whenever the # socket changes. module = tear_alias(module) quote do @phoenix_sockets {unquote(path), unquote(module), unquote(opts)} end end @doc """ Instruments the given function using the instrumentation provided by the given endpoint. To specify the endpoint that will provide instrumentation, the first argument can be: * a module name - the endpoint itself * a `Plug.Conn` struct - this macro will look for the endpoint module in the `:private` field of the connection; if it's not there, `fun` will be executed with no instrumentation * a `Phoenix.Socket` struct - this macro will look for the endpoint module in the `:endpoint` field of the socket; if it's not there, `fun` will be executed with no instrumentation Usually, users should prefer to instrument events using the `c:instrument/3` macro defined in every Phoenix endpoint. This macro should only be used for cases when the endpoint is dynamic and not known at compile time. ## Examples endpoint = MyApp.Endpoint Phoenix.Endpoint.instrument endpoint, :render_view, fn -> ... end """ defmacro instrument(endpoint_or_conn_or_socket, event, runtime \\ Macro.escape(%{}), fun) do compile = Phoenix.Endpoint.Instrument.strip_caller(__CALLER__) |> Macro.escape() quote do case Phoenix.Endpoint.Instrument.extract_endpoint(unquote(endpoint_or_conn_or_socket)) do nil -> unquote(fun).() endpoint -> endpoint.instrument(unquote(event), unquote(compile), unquote(runtime), unquote(fun)) end end end @doc """ Checks if Endpoint's web server has been configured to start. * `otp_app` - The OTP app running the endpoint, for example `:my_app` * `endpoint` - The endpoint module, for example `MyApp.Endpoint` ## Examples iex> Phoenix.Endpoint.server?(:my_app, MyApp.Endpoint) true """ def server?(otp_app, endpoint) when is_atom(otp_app) and is_atom(endpoint) do Phoenix.Endpoint.Supervisor.server?(otp_app, endpoint) end defp tear_alias({:__aliases__, meta, [h|t]}) do alias = {:__aliases__, meta, [h]} quote do Module.concat([unquote(alias)|unquote(t)]) end end defp tear_alias(other), do: other end