Skip to content

Commit 518d484

Browse files
committed
config support for DB_CONNECT_PARAMS_JSON
1 parent 41587ab commit 518d484

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

url_finder/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ chrono = { version = "0.4.38", features = ["serde"] }
4242
regex = "1.11.1"
4343
moka = { version = "0.12.10", default-features = false, features = ["future"] }
4444
dotenvy = "0.15.7"
45+
urlencoding = "2.1.3"

url_finder/src/config.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use std::env;
33
use color_eyre::Result;
44
use once_cell::sync::Lazy;
55

6+
use crate::types::DbConnectParams;
7+
68
pub static CONFIG: Lazy<Config> = Lazy::new(|| Config::new_from_env().unwrap());
79

810
#[derive(Debug)]
@@ -13,8 +15,18 @@ pub struct Config {
1315
}
1416
impl Config {
1517
pub fn new_from_env() -> Result<Self> {
18+
let db_url = env::var("DATABASE_URL").unwrap_or_else(|_| {
19+
let json_params = env::var("DB_CONNECT_PARAMS_JSON")
20+
.expect("DB_CONNECT_PARAMS_JSON environment variable not set");
21+
22+
let params: DbConnectParams =
23+
serde_json::from_str(&json_params).expect("Invalid JSON in DB_CONNECT_PARAMS_JSON");
24+
25+
params.to_url()
26+
});
27+
1628
Ok(Self {
17-
db_url: env::var("DATABASE_URL").expect("DATABASE_URL must be set"),
29+
db_url,
1830
dmob_db_url: env::var("DMOB_DATABASE_URL").expect("DMOB_DATABASE_URL must be set"),
1931
log_level: env::var("LOG_LEVEL").unwrap_or("info".to_string()),
2032
})

url_finder/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ mod provider_endpoints;
3838
mod repository;
3939
mod routes;
4040
mod services;
41+
mod types;
4142
mod url_tester;
4243

4344
pub struct AppState {

url_finder/src/types.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use serde::Deserialize;
2+
3+
#[derive(Deserialize)]
4+
pub(super) struct DbConnectParams {
5+
password: String,
6+
dbname: String,
7+
engine: String,
8+
port: u16,
9+
host: String,
10+
username: String,
11+
}
12+
13+
impl DbConnectParams {
14+
pub fn to_url(&self) -> String {
15+
format!(
16+
"{}://{}:{}@{}:{}/{}?{}",
17+
self.engine,
18+
self.username,
19+
urlencoding::encode(&self.password),
20+
self.host,
21+
self.port,
22+
self.dbname,
23+
std::env::var("DB_OPTIONS").unwrap_or_default(),
24+
)
25+
}
26+
}

0 commit comments

Comments
 (0)