Created
October 22, 2019 14:29
-
-
Save keathley/82dd0da5f16cf1459ed6203c2c4b7604 to your computer and use it in GitHub Desktop.
Example of elixir runtime configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Huddle.Application do | |
@moduledoc false | |
use Application | |
def start(_type, _args) do | |
children = [ | |
Endpoint, | |
Repo, | |
{RedisClient, redis_config()} | |
] | |
opts = [strategy: :one_for_one, name: Huddle.Supervisor] | |
Supervisor.start_link(children, opts) | |
end | |
def redis_config() do | |
env = System.get_env() | |
[ | |
pool_size: env["APP_REDIS_POOL_SIZE"] || 20, | |
host: env["APP_REDIS_HOST"] || "localhost", | |
port: env["APP_REDIS_PORT"] || "5432", | |
database: env["APP_REDIS_DATABASE"] || 1, | |
] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Endpoint do | |
use Phoenix.Endpoint, otp_app: :app_name | |
def init(_, config) do | |
http_config = | |
config[:http] | |
|> Enum.map(&update_port/1) | |
config = put_in(config, [:http], http_config) | |
{:ok, config} | |
end | |
defp update_port({:port, _}), do: {:port, port()} | |
defp update_port(config), do: config | |
defp port do | |
name = | |
Node.self() | |
|> Atom.to_string() | |
|> String.replace(~r/@.*$/, "") | |
|> String.upcase() | |
env = System.get_env() | |
String.to_integer(env["#{name}_PORT"] || env["PORT"] || "4000") | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Repo do | |
@moduledoc false | |
use Ecto.Repo, | |
otp_app: :app_name, | |
adapter: Ecto.Adapters.Postgres | |
def init(_type, config) do | |
config = Keyword.merge(config, runtime_config(), &update_config/3) | |
{:ok, config} | |
end | |
defp runtime_config do | |
env = System.get_env() | |
[ | |
hostname: env["APP_PG_REPLICA_HOST"], | |
database: env["APP_PG_REPLICA_DB_NAME"], | |
username: env["APP_PG_REPLICA_DB_USER"], | |
password: env["APP_PG_REPLICA_DB_PASSWORD"], | |
pool_size: String.to_integer(env["APP_PG_REPLICA_POOL_SIZE"] || "30") | |
] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment