From ab0fe722aa1dfdf2429357e32d8ac5cd8a87b467 Mon Sep 17 00:00:00 2001 From: James Arthur Date: Wed, 6 Nov 2024 11:30:12 +0100 Subject: [PATCH 1/2] config: candidate implementation of `ELECTRIC_PORT=PORT`. --- .changeset/kind-lizards-work.md | 5 +++++ packages/sync-service/config/runtime.exs | 25 +++++++++++++++++++++++- website/docs/api/config.md | 8 ++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 .changeset/kind-lizards-work.md diff --git a/.changeset/kind-lizards-work.md b/.changeset/kind-lizards-work.md new file mode 100644 index 0000000000..0a2d0632eb --- /dev/null +++ b/.changeset/kind-lizards-work.md @@ -0,0 +1,5 @@ +--- +"@core/sync-service": patch +--- + +Enable binding to a system provided `PORT` environment variable by setting the newly namespaced `ELECTRIC_PORT` to the special string "PORT", i.e.: `ELECTRIC_PORT=PORT`. diff --git a/packages/sync-service/config/runtime.exs b/packages/sync-service/config/runtime.exs index 41d1684e3b..b9b3cd45bf 100644 --- a/packages/sync-service/config/runtime.exs +++ b/packages/sync-service/config/runtime.exs @@ -192,6 +192,29 @@ storage = {storage_mod, storage_opts} prometheus_port = env!("ELECTRIC_PROMETHEUS_PORT", :integer, nil) +# If `ELECTRIC_PORT` is set to the special string "PORT" then +# try to read the service port from the standard `PORT` env var. +# +# Otherwise, set the service port to the equivalent of +# `env!("ELECTRIC_PORT", :integer, 3000)`. +# +# This enables support for deployment environments that expect +# services to bind to `PORT`, whilst still using the namespaced +# `ELECTRIC_PORT` env var by default and only reading `PORT` if +# explicitly configured to (to avoid unexpected behaviour). +default_service_port = 3000 +service_port = + case env!("ELECTRIC_PORT", :string, :not_set) do + :not_set -> + default_service_port + + "PORT" -> + env!("PORT", :integer, default_service_port) + + value when is_binary(value) -> + Dotenvy.Transformer.to!(value, :integer) + end + config :electric, allow_shape_deletion: enable_integration_testing, cache_max_age: cache_max_age, @@ -205,7 +228,7 @@ config :electric, db_pool_size: env!("ELECTRIC_DB_POOL_SIZE", :integer, 20), replication_stream_id: replication_stream_id, replication_slot_temporary?: env!("CLEANUP_REPLICATION_SLOTS_ON_SHUTDOWN", :boolean, false), - service_port: env!("ELECTRIC_PORT", :integer, 3000), + service_port: service_port, prometheus_port: prometheus_port, storage: storage, persistent_kv: persistent_kv, diff --git a/website/docs/api/config.md b/website/docs/api/config.md index 5d99c03ca5..cef036eb45 100644 --- a/website/docs/api/config.md +++ b/website/docs/api/config.md @@ -165,6 +165,14 @@ Enable [OTP SASL](https://www.erlang.org/doc/apps/sasl/sasl_app.html) reporting Port that the [HTTP API](/docs/api/http) is exposed on. +> ![Tip] Binding to `PORT` +> Some deployment environments expect web services to bind to a system provided +> `PORT` environment variable. +> +> You can enable support for this by explicitly setting `ELECTRIC_PORT=PORT`. +> This tells Electric to read the port from the `PORT` variable. (Still falling +> back on the default value of `3000` if `PORT` is not set). + ## Caching From 576e3ad1692c6bbdc8a16be863de1b7b92338e60 Mon Sep 17 00:00:00 2001 From: James Arthur Date: Wed, 6 Nov 2024 11:54:03 +0100 Subject: [PATCH 2/2] config: mix format. --- packages/sync-service/config/runtime.exs | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/sync-service/config/runtime.exs b/packages/sync-service/config/runtime.exs index b9b3cd45bf..ca1ddeffcb 100644 --- a/packages/sync-service/config/runtime.exs +++ b/packages/sync-service/config/runtime.exs @@ -203,6 +203,7 @@ prometheus_port = env!("ELECTRIC_PROMETHEUS_PORT", :integer, nil) # `ELECTRIC_PORT` env var by default and only reading `PORT` if # explicitly configured to (to avoid unexpected behaviour). default_service_port = 3000 + service_port = case env!("ELECTRIC_PORT", :string, :not_set) do :not_set ->