Skip to content

Instantly share code, notes, and snippets.

@keathley
Created October 22, 2019 14:29
Show Gist options
  • Save keathley/82dd0da5f16cf1459ed6203c2c4b7604 to your computer and use it in GitHub Desktop.
Save keathley/82dd0da5f16cf1459ed6203c2c4b7604 to your computer and use it in GitHub Desktop.
Example of elixir runtime configuration
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
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
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