Elixir/Phoenix:如何在配置文件中使用第三方模块? [英] Elixir/Phoenix: How to use third-party modules in config files?

查看:119
本文介绍了Elixir/Phoenix:如何在配置文件中使用第三方模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

config.exsdev.exs/prod.exs/test.exs中使用第三方模块时,似乎在phoenix中加载和编译配置文件的方式引起了问题.

It seems that the way configuration files in phoenix are loaded and compiled pose a problem when using third-party modules in config.exs or dev.exs/prod.exs/test.exs.

示例:要为JWT身份验证设置 Guardian ,我尝试使用

Example: To set up Guardian for JWT authentication I am trying to use the JOSE.JWK module for JWK creation / loading in my config.exs. I can use the module alright in the console with iex -S mix phoenix.server. It is of course installed as a dependency. The error I'm getting is

** (Mix.Config.LoadError) could not load config config/config.exs
    ** (UndefinedFunctionError) undefined function JOSE.JWK.from_file/2 (module JOSE.JWK is not available)

这是我的config.exs中的代码

This is the code in my config.exs

# Configure Guardian for JWT Authentication
config :guardian, Guardian,
  allowed_algos: ["HS512"], # optional
  verify_module: Guardian.JWT,  # optional
  issuer: "MyApp",
  ttl: { 30, :days },
  verify_issuer: true, # optional
  secret_key: System.get_env("GUARDIAN_KEY_PASSPHRASE") |> JOSE.JWK.from_file(System.get_env("GUARDIAN_KEY_FILE")),
  serializer: MyApp.GuardianSerializer

当我用匿名函数包装对JOSE.JWK.from_file/2的调用时,它起作用.但是,当然,Guardian.config(:secret_key)的值就是匿名函数本身,而不是其返回值:

It works when I wrap the call to JOSE.JWK.from_file/2 in an anonymous function. But of course the value of Guardian.config(:secret_key) is then the anonymous function itself and not its return value:

# Configure Guardian for JWT Authentication
config :guardian, Guardian,
  allowed_algos: ["HS512"], # optional
  verify_module: Guardian.JWT,  # optional
  issuer: "MyApp",
  ttl: { 30, :days },
  verify_issuer: true, # optional
  secret_key: fn -> System.get_env("GUARDIAN_KEY_PASSPHRASE") |> JOSE.JWK.from_file(System.get_env("GUARDIAN_KEY_FILE")) end,
  serializer: MyApp.GuardianSerializer

在此示例中可以这样做,因为Guardian接受此配置值的功能.但是我可以想象其他情况下这可能是个问题.

This is ok in this example since Guardian accepts a function for this config value. But I can imagine other situations where this could be a problem.

此限制是故意的吗?我想念什么吗?有办法解决吗?

Is this limitation on purpose? Am I missing something? Is there a way around this?

推荐答案

由于配置是在编译依赖项之前进行评估的,因此无法在配置中使用依赖项中的代码.

Since configuration is evaluated before the dependencies are compiled you can't use code from dependencies in the configuration.

原因很简单:配置可以更改依赖项的编译方式.您需要确定首先要执行的操作-进行编译以评估配置.已决定首先评估配置,因为通过配置来操纵编译比使用依赖项来配置其他应用程序要有用得多(且更频繁)-最常见的配置只是原始数据.

The reason is simple: configuration could change how a dependency is compiled. You need to decide what to do first - compile to evaluate configurations. Decision has been taken to evaluate configuration first since it's far more useful (and frequent) to manipulate compilation through configs than to use dependencies to configure other applications - most frequently configuration is just raw data.

这篇关于Elixir/Phoenix:如何在配置文件中使用第三方模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆