为Phoenix/Elixir应用程序获取基域的惯用方法是什么?不是单个请求,而是应用程序的基域,这可能取决于其当前环境。
所以在本地应该是localhost
,但在服务器上,它可以是"dev.my_domain.com“、"my_domain.com”或其他我可以在我的应用程序中使用的东西。
当然,我可以在config/dev.exs
或config/prod.exs
中添加一个特殊的键,但我认为在某个地方可能已经有这样的键可供重用。
发布于 2016-10-26 17:59:48
对于请求,您应该使用Plug.Conn
中的host
字段,就像@TheAnhLe建议的那样。
但是如果你想获取域名,Phoenix允许你在你的应用程序Endpoint
配置中指定url
参数:
# 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"
MyAppWeb.Endpoint.host()
# => "localhost"
如果未指定,则该值默认为localhost
。
更新:对于菲尼克斯1.3之前的版本,请将MyAppWeb
替换为MyApp
。
发布于 2017-11-07 17:50:02
在phoenix 1.3.0 (不知道旧版本)中,您可以从调用url/0
例如:
iex(1)> TestWeb.Endpoint.url
"http://localhost:4000"
https://stackoverflow.com/questions/40243229
复制相似问题