commit 0657f5cc0aad0d823bc840e17fb0fb14bc11723f
parent 257dc428bd5d7a6c2466aa22ae8c7105f0321b4e
Author: Alberto Lerda <albertolerda97@gmail.com>
Date: Mon, 9 Jan 2023 10:34:01 +0100
Zenflows.{DID, Restroom}: instance URI as env var
Diffstat:
5 files changed, 38 insertions(+), 32 deletions(-)
diff --git a/conf/.env.templ b/conf/.env.templ
@@ -27,8 +27,7 @@
#export DB_PORT=5432
## restroom
-#export ROOM_HOST=
-#export ROOM_PORT=
+#export ROOM_URI=
export ROOM_SALT=@ROOM_SALT
## admin
@@ -40,6 +39,4 @@ export GQL_DEF_PAGE_SIZE=50
export GQL_MAX_PAGE_SIZE=100
#export DID_KEYRING
-#export DID_HOST
-#export DID_PORT
-#export DID_SCHEME
+#export DID_URI
diff --git a/conf/runtime.exs b/conf/runtime.exs
@@ -27,6 +27,15 @@ get_env_int = fn varname, int ->
end
end
+get_env_url = fn varname, default ->
+ with {:ok, %{scheme: scheme, host: host, port: port}} when not is_nil(host) and scheme in ["http", "https"]
+ <- URI.new(get_env(varname, default)) do
+ %{scheme: :"#{scheme}", host: host, port: port}
+ else
+ err -> raise err
+ end
+end
+
#
# database
#
@@ -68,18 +77,18 @@ config :zenflows, Zenflows.DB.Repo, db_conf
#
# restroom
#
+room_uri = get_env_url.("ROOM_URI", "http://localhost")
config :zenflows, Zenflows.Restroom,
- room_host: get_env("ROOM_HOST", "localhost"),
- room_port: get_env_int.("ROOM_PORT", 3000),
+ room_uri: room_uri,
room_salt: fetch_env!("ROOM_SALT")
+
#
# did
#
did_keyring = Base.decode64!(get_env("DID_KEYRING", ""))
+did_uri = get_env_url.("DID_URI", "http://did.dyne.org")
config :zenflows, Zenflows.DID,
- did_scheme: if(get_env("DID_SCHEME", "http") == "http", do: :http, else: :https),
- did_host: get_env("DID_HOST", "did.dyne.org"),
- did_port: get_env_int.("DID_PORT", 80),
+ did_uri: did_uri,
did_keyring: if(did_keyring == "", do: nil, else: Jason.decode!(did_keyring))
#
diff --git a/docs/configuration-guide.md b/docs/configuration-guide.md
@@ -36,17 +36,11 @@ also see the [Required Options](#required-options).
This option should be used if extended configuration is desired (using the
options mention in the link above).
-* `ROOM_HOST`: The hostname or IP address of the Restroom instance. Defaults to `localhost`.
-* `ROOM_PORT`: The port number of the Restroom instance. It must be an integer
- between `0` and `65535`, inclusive. Defaults to `3000`.
+* `ROOM_URI`: The URI of the Restroom instance. Defaults to `http://localhost`.
* `ROOM_SALT`: The base64-encoded salt to be used with Restroom's
keypairoomServer call.
-* `DID_HOST`: The hostname or IP address of the DID controller instance.
- Defaults to `did.dyne.org`.
-* `DID_PORT`: The port number of the Restroom instance. It must be an integer
- between `0` and `65535`, inclusive. Defaults to `80`.
-* `DID_SCHEME`: Protocol to be used, either `http` or `https`.
+* `DID_URI`: The URI of the DID controller instance. Defaults to `http://did.dyne.org`.
* `DID_KEYRING`: Keyring (identity) of the server, it is not defined
communication with DID controller is disabled.
diff --git a/src/zenflows/did.ex b/src/zenflows/did.ex
@@ -115,25 +115,25 @@ def claim(_repo, %{person: person}) do
end
end
-# Return the hostname of restroom from the configs.
+# Return the scheme of did from the configs.
+@spec scheme() :: :http | :https
+defp scheme() do
+ Keyword.fetch!(conf(), :did_uri).scheme
+end
+
+# Return the hostname of did from the configs.
@spec host() :: String.t()
defp host() do
- Keyword.fetch!(conf(), :did_host)
+ Keyword.fetch!(conf(), :did_uri).host
end
-# Return the port of restroom from the configs.
+# Return the port of did from the configs.
@spec port() :: non_neg_integer()
defp port() do
- Keyword.fetch!(conf(), :did_port)
-end
-
-# http/https.
-@spec scheme() :: :http | :https
-defp scheme() do
- Keyword.fetch!(conf(), :did_scheme)
+ Keyword.fetch!(conf(), :did_uri).port
end
-# Keyring (private keys) for the server
+# Return the private keyring of the server from the configs.
@spec keyring() :: nil | map()
defp keyring() do
Keyword.fetch!(conf(), :did_keyring)
diff --git a/src/zenflows/restroom.ex b/src/zenflows/restroom.ex
@@ -24,7 +24,7 @@ def child_spec(_) do
Supervisor.child_spec(
{Zenflows.HTTPC,
name: __MODULE__,
- scheme: :http,
+ scheme: scheme(),
host: host(),
port: port(),
},
@@ -113,16 +113,22 @@ defp salt() do
Keyword.fetch!(conf(), :room_salt)
end
+# Return the scheme of restroom from the configs.
+@spec scheme() :: :http | :https
+defp scheme() do
+ Keyword.fetch!(conf(), :room_uri).scheme
+end
+
# Return the hostname of restroom from the configs.
@spec host() :: String.t()
defp host() do
- Keyword.fetch!(conf(), :room_host)
+ Keyword.fetch!(conf(), :room_uri).host
end
# Return the port of restroom from the configs.
@spec port() :: non_neg_integer()
defp port() do
- Keyword.fetch!(conf(), :room_port)
+ Keyword.fetch!(conf(), :room_uri).port
end
# Return the application configurations of this module.