Skip to content

Commit 04bc971

Browse files
committed
feat: disable HTTP API by default, make it opt-in
BREAKING CHANGE: HTTP API is now disabled by default and must be explicitly enabled in configuration. Changes: - Move HTTP config from :api_port and :api_ip to nested :http config - Add :http :enabled flag (defaults to false) - Conditionally start Web.Supervisor only when HTTP is enabled - Update all environment configs: - config.exs: HTTP disabled by default - dev.exs: HTTP enabled for development - test.exs: HTTP disabled for unit tests - e2e_test.exs: HTTP enabled for e2e tests - prod.exs: HTTP enabled by default, configurable via CONCORD_HTTP_ENABLED To enable HTTP API in production: - Set CONCORD_HTTP_ENABLED=true environment variable - Or configure in config/runtime.exs: config :concord, :http, enabled: true This change allows Concord to be used as an embedded library without the HTTP API overhead when not needed.
1 parent de3b747 commit 04bc971

File tree

7 files changed

+42
-14
lines changed

7 files changed

+42
-14
lines changed

config/config.exs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ config :concord,
2727
level: 6
2828
],
2929
# HTTP API configuration
30-
api_port: 4000,
31-
# localhost
32-
api_ip: {127, 0, 0, 1},
30+
http: [
31+
# Enable HTTP/HTTPS API server (disabled by default)
32+
enabled: false,
33+
# API server port
34+
port: 4000,
35+
# API server IP (localhost by default)
36+
ip: {127, 0, 0, 1}
37+
],
3338
# TLS/HTTPS configuration
3439
tls: [
3540
# Enable HTTPS instead of HTTP

config/dev.exs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import Config
22

33
config :concord,
44
data_dir: "./data/dev",
5-
auth_enabled: false
5+
auth_enabled: false,
6+
http: [
7+
enabled: true,
8+
port: 4000,
9+
ip: {127, 0, 0, 1}
10+
]
611

712
config :logger, level: :debug

config/e2e_test.exs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ config :libcluster,
1717
config :logger, level: :warning
1818

1919
# HTTP API configuration for e2e tests
20-
config :concord, :http,
21-
enabled: true,
22-
port: 4000
20+
config :concord, :http, enabled: true, port: 4000
2321

2422
# Telemetry configuration
2523
config :concord, :telemetry, enabled: true

config/prod.exs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ config :concord,
44
data_dir: {:system, "CONCORD_DATA_DIR", "/var/lib/concord"},
55
auth_enabled: true,
66
# Production HTTP API configuration
7-
api_port: {:system, "CONCORD_API_PORT", 8080},
8-
api_ip: {:system, "CONCORD_API_IP", {0, 0, 0, 0}}
7+
http: [
8+
enabled: {:system, "CONCORD_HTTP_ENABLED", true},
9+
port: {:system, "CONCORD_API_PORT", 8080},
10+
ip: {:system, "CONCORD_API_IP", {0, 0, 0, 0}}
11+
]
912

1013
config :logger, level: :info

config/test.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Config
22

33
config :concord,
44
data_dir: "./data/test",
5-
auth_enabled: false
5+
auth_enabled: false,
6+
http: [enabled: false]
67

78
config :logger, level: :warning

lib/concord/application.ex

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,19 @@ defmodule Concord.Application do
6060
{TTL, []},
6161
# Start multi-tenancy rate limiter
6262
MultiTenancy.RateLimiter,
63-
# Start HTTP API web server
64-
Web.Supervisor,
6563
# Start the Concord cluster after a brief delay
6664
{Task, fn -> init_cluster() end}
6765
]
6866

67+
# Add HTTP API web server if enabled
68+
children =
69+
if http_api_enabled?() do
70+
# Insert Web.Supervisor before the cluster init task
71+
List.insert_at(children, -1, Web.Supervisor)
72+
else
73+
children
74+
end
75+
6976
# Add Prometheus exporter if enabled
7077
children =
7178
if prometheus_enabled?() do
@@ -94,6 +101,11 @@ defmodule Concord.Application do
94101
Supervisor.start_link(children, opts)
95102
end
96103

104+
defp http_api_enabled? do
105+
Application.get_env(:concord, :http, [])
106+
|> Keyword.get(:enabled, false)
107+
end
108+
97109
defp prometheus_enabled? do
98110
Application.get_env(:concord, :prometheus_enabled, true)
99111
end

lib/concord/web/supervisor.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ defmodule Concord.Web.Supervisor do
4040
defp get_port do
4141
case System.get_env("CONCORD_API_PORT") do
4242
nil ->
43-
Application.get_env(:concord, :api_port, 4000)
43+
http_config = Application.get_env(:concord, :http, [])
44+
Keyword.get(http_config, :port, 4000)
4445

4546
port_str ->
4647
case Integer.parse(port_str) do
@@ -53,7 +54,10 @@ defmodule Concord.Web.Supervisor do
5354
defp get_ip do
5455
case System.get_env("CONCORD_API_IP") do
5556
nil ->
56-
case Application.get_env(:concord, :api_ip, {127, 0, 0, 1}) do
57+
http_config = Application.get_env(:concord, :http, [])
58+
default_ip = Keyword.get(http_config, :ip, {127, 0, 0, 1})
59+
60+
case default_ip do
5761
{a, b, c, d} = ip when a in 0..255 and b in 0..255 and c in 0..255 and d in 0..255 -> ip
5862
:loopback -> {127, 0, 0, 1}
5963
:any -> {0, 0, 0, 0}

0 commit comments

Comments
 (0)