-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
115 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,17 @@ host = "127.0.0.1:3000" | |
disable_auth = false | ||
|
||
[database] | ||
kind = "connection_string" | ||
connection_string = "postgres://postgres:[email protected]:5432/database" | ||
|
||
# [database] | ||
# kind = "parts" | ||
# host = "127.0.0.1" | ||
# port = "5432" | ||
# username = "postgres" | ||
# password = "postgres" | ||
# database = "database" | ||
|
||
[keys] | ||
kind = "local" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,12 +28,53 @@ pub struct ServerConfig { | |
pub disable_auth: bool, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
#[serde(tag = "kind", rename_all = "snake_case")] | ||
pub enum DatabaseConfig { | ||
ConnectionString(DbConnectionString), | ||
Parts(DbParts), | ||
} | ||
|
||
impl DatabaseConfig { | ||
pub fn connection_string(s: impl ToString) -> Self { | ||
Self::ConnectionString(DbConnectionString { | ||
connection_string: s.to_string(), | ||
}) | ||
} | ||
|
||
pub fn to_connection_string(&self) -> String { | ||
match self { | ||
Self::ConnectionString(s) => s.connection_string.clone(), | ||
Self::Parts(parts) => { | ||
format!( | ||
"postgres://{}:{}@{}:{}/{}", | ||
parts.username, | ||
parts.password, | ||
parts.host, | ||
parts.port, | ||
parts.database | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
#[serde(rename_all = "snake_case")] | ||
pub struct DatabaseConfig { | ||
pub struct DbConnectionString { | ||
pub connection_string: String, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
#[serde(rename_all = "snake_case")] | ||
pub struct DbParts { | ||
pub host: String, | ||
pub port: String, | ||
pub username: String, | ||
pub password: String, | ||
pub database: String, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
#[serde(rename_all = "snake_case", tag = "kind")] | ||
pub enum KeysConfig { | ||
|
@@ -55,7 +96,7 @@ mod tests { | |
|
||
use super::*; | ||
|
||
const SAMPLE: &str = indoc! {r#" | ||
const WITH_DB_CONNECTION_STRING: &str = indoc! {r#" | ||
[service] | ||
escalation_interval = "1h" | ||
|
@@ -64,14 +105,35 @@ mod tests { | |
disable_auth = false | ||
[database] | ||
kind = "connection_string" | ||
connection_string = "postgres://postgres:[email protected]:52804/database" | ||
[keys] | ||
kind = "local" | ||
"#}; | ||
|
||
const WITH_DB_PARTS: &str = indoc! {r#" | ||
[service] | ||
escalation_interval = "1h" | ||
[server] | ||
host = "127.0.0.1:3000" | ||
disable_auth = false | ||
[database] | ||
kind = "parts" | ||
host = "host" | ||
port = "5432" | ||
username = "user" | ||
password = "pass" | ||
database = "db" | ||
[keys] | ||
kind = "local" | ||
"#}; | ||
|
||
#[test] | ||
fn sample() { | ||
fn with_db_connection_string() { | ||
let config = Config { | ||
service: TxSitterConfig { | ||
escalation_interval: Duration::from_secs(60 * 60), | ||
|
@@ -80,16 +142,40 @@ mod tests { | |
host: SocketAddr::from(([127, 0, 0, 1], 3000)), | ||
disable_auth: false, | ||
}, | ||
database: DatabaseConfig { | ||
connection_string: | ||
"postgres://postgres:[email protected]:52804/database" | ||
.to_string(), | ||
database: DatabaseConfig::connection_string( | ||
"postgres://postgres:[email protected]:52804/database" | ||
.to_string(), | ||
), | ||
keys: KeysConfig::Local(LocalKeysConfig {}), | ||
}; | ||
|
||
let toml = toml::to_string_pretty(&config).unwrap(); | ||
|
||
assert_eq!(toml, WITH_DB_CONNECTION_STRING); | ||
} | ||
|
||
#[test] | ||
fn with_db_parts() { | ||
let config = Config { | ||
service: TxSitterConfig { | ||
escalation_interval: Duration::from_secs(60 * 60), | ||
}, | ||
server: ServerConfig { | ||
host: SocketAddr::from(([127, 0, 0, 1], 3000)), | ||
disable_auth: false, | ||
}, | ||
database: DatabaseConfig::Parts(DbParts { | ||
host: "host".to_string(), | ||
port: "5432".to_string(), | ||
username: "user".to_string(), | ||
password: "pass".to_string(), | ||
database: "db".to_string(), | ||
}), | ||
keys: KeysConfig::Local(LocalKeysConfig {}), | ||
}; | ||
|
||
let toml = toml::to_string_pretty(&config).unwrap(); | ||
|
||
assert_eq!(toml, SAMPLE); | ||
assert_eq!(toml, WITH_DB_PARTS); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters