How to plug module in controller only in production env?
Example:
plug BasicAuth, realm: "Admin Area", username: "admin", password: "pass"
While @whatyouhide's answer is correct, be careful using Mix.env
in your project outside of mix tasks. If you decide to use Exrm or relx to build OTP releases, the mix library will not be included and this will crash your app on startup.
As an alternative, you can add an :env
key to each environment's respective config file with the env value in it and then use it much like you would Mix.env
.
You can take advantage of Mix
's environments. You can selectively add the plug
call only in the production environment with something like this:
if Mix.env == :prod do
plug BasicAuth, realm: "Admin Area", username: "admin", password: "pass"
end
This line will not compile to anything (nil
) if the Mix.env
is not production.