From cb84f9a03b2154ffde3e3dd26695c9662d1bfd08 Mon Sep 17 00:00:00 2001 From: Christopher Date: Sat, 21 Mar 2020 20:03:32 -0500 Subject: [PATCH] make database configurable with env vars --- config/config.exs | 4 --- .../authentication/ensure_default_admin.ex | 12 ++++++- lib/router/authentication/repo.ex | 35 +++++++++++++++++++ mix.exs | 3 +- 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/config/config.exs b/config/config.exs index 1321bb3..e9dd402 100644 --- a/config/config.exs +++ b/config/config.exs @@ -18,7 +18,3 @@ config :wampex_router, list_nodes: {:erlang, :nodes, [:connected]} ] ] - -config :wampex_router, - ecto_repos: [Wampex.Router.Authentication.Repo], - keylen: 32 diff --git a/lib/router/authentication/ensure_default_admin.ex b/lib/router/authentication/ensure_default_admin.ex index 6a72096..423b02c 100644 --- a/lib/router/authentication/ensure_default_admin.ex +++ b/lib/router/authentication/ensure_default_admin.ex @@ -3,17 +3,27 @@ defmodule Wampex.Router.Authentication.EnsureDefaultAdmin do require Logger use GenServer - alias Wampex.Router.Authentication.{Realm, Peer} + alias Wampex.Router.Authentication.{Peer, Realm, Repo} + @ecto_repos [Repo] def start_link(uri: uri, authid: authid, password: password) do GenServer.start_link(__MODULE__, {uri, authid, password}) end def init({uri, authid, password}) do + ensure_migrations() %Realm{} = realm = Realm.create(uri: uri) %Peer{} = user = Peer.create(authid: authid, password: password, realm: realm) Logger.debug("Realm: #{inspect(realm)}") Logger.debug("Peer: #{inspect(user)}") :ignore end + + defp ensure_migrations do + Logger.info("Ensuring tables have been migrated") + + for repo <- @ecto_repos() do + {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true)) + end + end end diff --git a/lib/router/authentication/repo.ex b/lib/router/authentication/repo.ex index e423625..44fac5f 100644 --- a/lib/router/authentication/repo.ex +++ b/lib/router/authentication/repo.ex @@ -2,4 +2,39 @@ defmodule Wampex.Router.Authentication.Repo do use Ecto.Repo, otp_app: :wampex_router, adapter: Ecto.Adapters.Postgres + + alias __MODULE__ + require Logger + + def init(:runtime, config) do + config = update_config(config, :database, "AUTH_DATABASE_NAME", nil) + config = update_config(config, :hostname, "AUTH_DATABASE_HOSTAME", nil) + config = update_config(config, :port, "AUTH_DATABASE_PORT", &String.to_integer/1) + config = update_config(config, :username, "AUTH_DATABASE_USERNAME", nil) + {:ok, config} + end + + def init(:supervisor, config) do + ensure_db_created(config) + {:ok, config} + end + + def ensure_db_created(config) do + Logger.info("Ensuring DB exists") + r = Repo.__adapter__().storage_up(config) + Logger.info("DB Creation: #{inspect(r)}") + end + + def update_config(config, key, env, format) do + case System.get_env(env) do + nil -> + config + + val -> + Keyword.put(config, key, do_format(val, format)) + end + end + + def do_format(val, nil), do: val + def do_format(val, fmt), do: fmt.(val) end diff --git a/mix.exs b/mix.exs index d4bffb3..b52e3e7 100644 --- a/mix.exs +++ b/mix.exs @@ -31,7 +31,8 @@ defmodule Wampex.Router.MixProject do def application do [ - extra_applications: [:logger] + extra_applications: [:logger], + env: [keylen: 32] ] end -- 2.45.3