Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: candidate implementation of ELECTRIC_PORT=PORT. #1940

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/kind-lizards-work.md
Original file line number Diff line number Diff line change
@@ -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`.
26 changes: 25 additions & 1 deletion packages/sync-service/config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,30 @@ 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,
Expand All @@ -205,7 +229,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,
Expand Down
8 changes: 8 additions & 0 deletions website/docs/api/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

</EnvVarConfig>

## Caching
Expand Down
Loading