Commit Diff


commit - 6af1d49af84a21038707eebabb3b716454f9834f
commit + 661cdd753622c141165f3d41c58290f370eb7f82
blob - 8ff9a8a44696d65fa44e018d298f8819ed48536f
blob + bc6cff4c1baef67c58d544d64561664a311e9fbd
--- config/runtime.exs
+++ config/runtime.exs
@@ -1,10 +1,42 @@
 import Config
 
 if config_env() == :prod do
+  conf =
+    case File.read("/etc/shirts.conf") do
+      {:ok, contents} ->
+        contents
+        |> String.split("\n")
+        |> Enum.reduce(%{}, fn line, acc ->
+          line = String.trim(line)
+
+          case String.split(line, "=", parts: 2) do
+            [k, v] when byte_size(k) > 0 ->
+              if String.contains?(v, "$"),
+                do: acc,
+                else: Map.put(acc, k, v |> String.trim("\"") |> String.trim("'"))
+
+            _ ->
+              acc
+          end
+        end)
+
+      {:error, _} ->
+        %{}
+    end
+
+  get = fn key, default ->
+    case System.get_env(key) do
+      nil -> Map.get(conf, key, default)
+      val -> val
+    end
+  end
+
   config :shortener,
-    port: System.get_env("PORT") |> then(&if &1, do: String.to_integer(&1), else: 4000),
-    host: System.get_env("HOST") || "127.0.0.1",
-    auth_token: System.fetch_env!("AUTH_TOKEN"),
-    db_path: System.get_env("DB_PATH") || "data.db",
-    base_url: System.get_env("BASE_URL") || "http://localhost"
+    port: get.("PORT", "4000") |> String.to_integer(),
+    host: get.("HOST", "127.0.0.1"),
+    auth_token:
+      System.get_env("AUTH_TOKEN") || Map.get(conf, "AUTH_TOKEN") ||
+        raise("AUTH_TOKEN not set in environment or /etc/shirts.conf"),
+    db_path: get.("DB_PATH", "data.db"),
+    base_url: get.("BASE_URL", "http://localhost")
 end