Elixir/Phoenix: How to use third-party modules in config files? -
it seems way configuration files in phoenix loaded , compiled pose problem when using third-party modules in config.exs
or dev.exs/prod.exs/test.exs
.
example: set guardian jwt authentication trying use jose.jwk
module jwk creation / loading in config.exs
. can use module alright in console iex -s mix phoenix.server
. of course installed dependency. error i'm getting
** (mix.config.loaderror) not load config config/config.exs ** (undefinedfunctionerror) undefined function jose.jwk.from_file/2 (module jose.jwk not available)
this code in config.exs
# configure guardian 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
it works when wrap call jose.jwk.from_file/2
in anonymous function. of course value of guardian.config(:secret_key) anonymous function , not return value:
# configure guardian 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
this ok in example since guardian accepts function config value. can imagine other situations problem.
is limitation on purpose? missing something? there way around this?
since configuration evaluated before dependencies compiled can't use code dependencies in configuration.
the reason simple: configuration change how dependency compiled. need decide first - compile evaluate configurations. decision has been taken evaluate configuration first since it's far more useful (and frequent) manipulate compilation through configs use dependencies configure other applications - configuration raw data.
Comments
Post a Comment