Skip to content

Commit e4b9f03

Browse files
fix(config): add gateway subdomain configuration for database connect… (#77)
Co-authored-by: Robert Yan <mstr.raphael@gmail.com>
1 parent 768103b commit e4b9f03

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed

crates/config/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub struct DatabaseConfig {
1212
pub tls_enabled: bool,
1313
pub tls_ca_cert_path: Option<String>,
1414
pub primary_app_id: String,
15+
pub gateway_subdomain: String,
1516
pub refresh_interval: u64,
1617
pub mock: bool,
1718
}
@@ -45,6 +46,7 @@ impl Default for DatabaseConfig {
4546
.unwrap_or(false),
4647
tls_ca_cert_path: std::env::var("DATABASE_TLS_CA_CERT_PATH").ok(),
4748
primary_app_id: std::env::var("DATABASE_PRIMARY_APP_ID").unwrap_or_default(),
49+
gateway_subdomain: std::env::var("GATEWAY_SUBDOMAIN").unwrap_or_default(),
4850
refresh_interval: std::env::var("DATABASE_REFRESH_INTERVAL")
4951
.ok()
5052
.and_then(|v| v.parse().ok())

crates/database/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl Database {
8383
// Create Patroni discovery
8484
let discovery = Arc::new(PatroniDiscovery::new(
8585
config.primary_app_id.clone(),
86+
config.gateway_subdomain.clone(),
8687
config.refresh_interval,
8788
));
8889

crates/database/src/patroni_discovery.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,24 @@ pub struct ClusterState {
3737
pub struct PatroniDiscovery {
3838
client: Client,
3939
postgres_app_id: String,
40+
gateway_subdomain: String,
4041
cluster_state: Arc<RwLock<Option<ClusterState>>>,
4142
refresh_interval: Duration,
4243
}
4344

4445
impl PatroniDiscovery {
45-
pub fn new(postgres_app_id: String, refresh_interval_secs: u64) -> Self {
46+
pub fn new(
47+
postgres_app_id: String,
48+
gateway_subdomain: String,
49+
refresh_interval_secs: u64,
50+
) -> Self {
4651
Self {
4752
client: Client::builder()
4853
.timeout(Duration::from_secs(10))
4954
.build()
5055
.expect("Failed to create HTTP client"),
5156
postgres_app_id,
57+
gateway_subdomain,
5258
cluster_state: Arc::new(RwLock::new(None)),
5359
refresh_interval: Duration::from_secs(refresh_interval_secs),
5460
}
@@ -61,14 +67,14 @@ impl PatroniDiscovery {
6167
self.postgres_app_id
6268
);
6369

64-
let url = "http://dstack-service/cluster";
70+
let url = format!(
71+
"https://{}-8008.{}/cluster",
72+
self.postgres_app_id, self.gateway_subdomain
73+
);
74+
6575
let response = self
6676
.client
6777
.get(url)
68-
.header("x-dstack-target-app", &self.postgres_app_id)
69-
.header("x-dstack-target-port", "8008")
70-
.header("x-dstack-target-use-tls", "false")
71-
.header("Host", "vpc-server")
7278
.send()
7379
.await
7480
.map_err(|e| anyhow!("Failed to connect to Patroni API: {e}"))?;
@@ -282,7 +288,8 @@ mod tests {
282288

283289
#[tokio::test]
284290
async fn test_get_least_lag_replica() {
285-
let discovery = PatroniDiscovery::new("test-app".to_string(), 30);
291+
let discovery =
292+
PatroniDiscovery::new("test-app".to_string(), "dstack.internal".to_string(), 30);
286293

287294
let replica1 = create_test_member("replica1", "replica", Some(100));
288295
let replica2 = create_test_member("replica2", "replica", Some(10));
@@ -312,7 +319,8 @@ mod tests {
312319

313320
#[tokio::test]
314321
async fn test_get_replicas_by_lag() {
315-
let discovery = PatroniDiscovery::new("test-app".to_string(), 30);
322+
let discovery =
323+
PatroniDiscovery::new("test-app".to_string(), "dstack.internal".to_string(), 30);
316324

317325
let replica1 = create_test_member("replica1", "replica", Some(100));
318326
let replica2 = create_test_member("replica2", "replica", Some(10));
@@ -331,7 +339,8 @@ mod tests {
331339

332340
#[tokio::test]
333341
async fn test_round_robin_selection() {
334-
let discovery = PatroniDiscovery::new("test-app".to_string(), 30);
342+
let discovery =
343+
PatroniDiscovery::new("test-app".to_string(), "dstack.internal".to_string(), 30);
335344

336345
let replica1 = create_test_member("replica1", "replica", None);
337346
let replica2 = create_test_member("replica2", "replica", None);

crates/database/src/pool.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ mod tests {
102102
let config = config::DatabaseConfig {
103103
host: Some("localhost".to_string()),
104104
port: 5432,
105+
gateway_subdomain: "dstack.internal".to_string(),
105106
database: "test_db".to_string(),
106107
username: "postgres".to_string(),
107108
password: "postgres".to_string(),
@@ -121,6 +122,7 @@ mod tests {
121122
let config = config::DatabaseConfig {
122123
host: Some("remote.example.com".to_string()),
123124
port: 5432,
125+
gateway_subdomain: "dstack.internal".to_string(),
124126
database: "prod_db".to_string(),
125127
username: "user".to_string(),
126128
password: "pass".to_string(),
@@ -142,6 +144,7 @@ mod tests {
142144
host: Some("localhost".to_string()),
143145
port: 5432,
144146
database: "cloud_api".to_string(),
147+
gateway_subdomain: "dstack.internal".to_string(),
145148
username: "postgres".to_string(),
146149
password: "postgres".to_string(),
147150
max_connections: 5,
@@ -160,6 +163,7 @@ mod tests {
160163
let remote_config = config::DatabaseConfig {
161164
host: Some("prod-db.example.com".to_string()),
162165
port: 5432,
166+
gateway_subdomain: "dstack.internal".to_string(),
163167
database: "cloud_api_prod".to_string(),
164168
username: "app_user".to_string(),
165169
password: "secure_password".to_string(),

env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,6 @@ CORS_ALLOWED_ORIGINS=http://localhost:3000
6767
# NEAR Authentication (NEP-413)
6868
# Expected recipient for signed messages (defaults to private.near.ai)
6969
NEAR_EXPECTED_RECIPIENT=private.near.ai
70+
71+
# Dstack Gateway Subdomain
72+
GATEWAY_SUBDOMAIN=cvm1.near.ai

0 commit comments

Comments
 (0)