[英]Elixir's module attributes in Phoenix production
As far as I know module attributes evaluated during compilation time. 据我所知,模块属性是在编译时评估的。 I was trying to follow this post about mocking API in Elixir:
我试图关注有关Elixir中的模拟API的帖子 :
defmodule Example.User do
@github_api Application.get_env(:example, :github_api)
def get(username) when is_binary(username) do
@github_api.make_request(:get, "/users/#{username}")
end
end
And I'm wondering if that's gonna work in production at all. 我想知道这是否真的可以在生产中使用。 As far as I understand when this module is compiled there's no access to the Application.
据我了解,编译该模块时无法访问该应用程序。 So my question is: can I use module attributes to store some config values that come from
Application.get_env
? 所以我的问题是:我可以使用模块属性存储一些来自
Application.get_env
配置值吗?
You absolutely can. 你绝对可以。 As long as the application was compiled using
MIX_ENV
specified to environment you want the application running under, and as long as that call evaluates to what you expect for that environment, it'll all work fine. 只要使用指定给环境的
MIX_ENV
编译了应用程序,您就希望该应用程序在该环境下运行,并且只要该调用的评估结果符合该环境的期望,它就可以正常工作。
For a deeper look at how module attributes are affected by compilation for an almost identical case as what you've described, take a look at this blog post here . 要更深入地了解在与您所描述的几乎相同的情况下编译如何影响模块属性, 请在此处查看此博客文章 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.