具有多个/动态配置文件的Elixir应用程序 [英] Elixir application with multiple/dynamic config files

查看:97
本文介绍了具有多个/动态配置文件的Elixir应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个elixir应用程序,一个应用程序服务器,将从启动参数中受益。此应用程序使用ecto存储库,因此我可以在其中存储应用程序服务器的配置,但是我仍然需要一个配置密钥才能从db中检索内容。

I have an elixir application, an application server, that would benefit from start up parameters. This application uses an ecto repository, so I can store an app server's configuration there, but I'd still need a configuration key for what to retrieve from the db.

目前我一直在为服务器应用程序使用config.exs(整个应用程序是一个伞形项目),但是显然,这只能处理一个静态配置。

Presently I've been using config.exs for the server application (the whole application is an umbrella project), but obviously this only handles one static configuration.

我的问题是:
我可以使用mix指定我要使用的配置文件吗?我知道Mix库中有几个函数,但是据我所读,它们都是在应用程序启动后可以使用的所有函数。
同样,我可以使用mix为任何子应用程序加载配置文件吗?

My question is: Can I use mix to specify what config file I'd like to use? I know that there are several functions in the Mix library, but from what I've read they're all functions that can be used after the application has started. And, similarly, could I use mix to load configuration files for any of the child applications?

感谢提供的任何帮助。

编辑:
根据要求...
主伞项目启动后(不知道伞项目的所有知识,我假设孩子应用程序启动顺序无关紧要;稍后要解决的细节)服务器子应用程序,使用其启动参数,查询整个应用程序的子应用程序存储库(Config.Query,包含要针对查询表运行的查询)服务器配置:侦听ipAddress和端口,代码目录,最大连接数等。此配置由genServer维护,可以根据需要由其他进程查询。

As requested... Once the main umbrella project has been started (not knowing everything there is to know about umbrella projects, I'll assume that child application start up order won't matter; details to work out later) the server child application, using its start up arguments, queries the child application repository (Config.Query, contain queries to be run against the query table) for the complete application server configuration: listen ipAddress and port, code directory, max number of connections, etc. This configuration is maintained by a genServer which can be queried by other processes as necessary.

defmodule Hermes.Server.Info do
    use GenServer

    def start_link() do
        GenServer.start_link(__MODULE__, :ok, [name: :hermes_server_configuration])
    end

    def init(:ok) do
        system        = Application.get_env(:hermes_server, :system, "dev")
        client        = Application.get_env(:hermes_server, :client, "testClient")
        appServerName = Application.get_env(:hermes_server, :appServername, "testAppServerOne")
        config        = Config.Query.get_config(system, client, appServerName)
        {:ok, config}
    end
end

因此,即使我可以做类似于 elixir --detached -S mix run --config pathToConfigFile 的事情,即使那意味着创建自己的bash脚本以获取正确的目录,这是我认为的最佳选择。但是,在阅读了Patrick的回答后,这似乎是不可能的。还没读懂配置文件是在编译时处理的。

So, if I could do something similar to elixir --detached -S mix run --config pathToConfigFile, even if that means creating my own bash script to get to the correct directory, that would be the best option in my opinion. But, having read Patrick's answer, that doesn't look possible; hadn't read that config files are something dealt with at compile time.

推荐答案

内置配置在编译时进行评估。要获取运行时配置,您需要使用第三方库,例如符合或推出自己的解决方案。

The built in configuration is evaluated at compile time. To get runtime configuration you need to use a third party library such as conform or roll your own solution.

这篇关于具有多个/动态配置文件的Elixir应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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