How to get the current base domain of a Phoenix application?

What is the idiomatic way to get the base domain for the Phoenix / Elixir app? Not just a single request, but a base application domain, which probably depends on its current environment.

So locally it should be localhost, but on the server it could be "dev.my_domain.com", "my_domain.com" or something else that I can use in my application.

Of course, I can add a special key to config/dev.exsor config/prod.exs, but I thought that perhaps there is already such a key that I can reuse.

+5
source share
3 answers

host Plug.Conn @TheAnhLe.

, Phoenix url Endpoint :

# config/prod.exs

config :my_app, MyAppWeb.Endpoint,
  http: [port: {:system, "PORT"}],
  url: [host: "example.com", port: 80],
  # more configs...

:

MyAppWeb.Endpoint.url
# => "http://localhost:4000"

Application.get_env(:my_app, MyAppWeb.Endpoint)[:url][:host]
# => "localhost"

localhost .

: Phoenix, 1.3, MyAppWeb MyApp.

+8

host. Plug.Conn.

host - , : "www.example.com"

+5

With Phoenix 1.3.0 (I don't know about older versions) you can call url/0from the Endpoint API

For instance:

iex(1)> TestWeb.Endpoint.url
"http://localhost:4000"
+5
source

Source: https://habr.com/ru/post/1658830/


All Articles