From 5c549b80fde17f2e776a1a2695d834be2994e84d Mon Sep 17 00:00:00 2001 From: Kirill Fedoseev Date: Tue, 19 Dec 2023 12:29:41 +0400 Subject: [PATCH 1/3] chore: common db settings --- .../src/launcher/settings.rs | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/libs/blockscout-service-launcher/src/launcher/settings.rs b/libs/blockscout-service-launcher/src/launcher/settings.rs index 31a10947d..2d38cefb1 100644 --- a/libs/blockscout-service-launcher/src/launcher/settings.rs +++ b/libs/blockscout-service-launcher/src/launcher/settings.rs @@ -137,3 +137,59 @@ impl Default for MetricsSettings { } } } + +#[derive(Debug, Clone, Deserialize, PartialEq, Eq)] +#[serde(deny_unknown_fields)] +pub struct DatabaseSettings { + pub connect: DatabaseConnectSettings, + #[serde(default)] + pub create_database: bool, + #[serde(default)] + pub run_migrations: bool, +} + +#[derive(Debug, Clone, Deserialize, PartialEq, Eq)] +#[serde(deny_unknown_fields, rename_all = "lowercase")] +pub enum DatabaseConnectSettings { + Url(String), + Kv(DatabaseKvConnection), +} + +impl DatabaseConnectSettings { + pub fn url(self) -> String { + match self { + DatabaseConnectSettings::Url(s) => s, + DatabaseConnectSettings::Kv(kv) => kv.url(), + } + } +} + +#[derive(Debug, Clone, Deserialize, PartialEq, Eq)] +#[serde(deny_unknown_fields)] +pub struct DatabaseKvConnection { + pub host: String, + pub port: u16, + pub user: String, + pub password: String, + #[serde(default)] + pub dbname: Option, + #[serde(default)] + pub options: Option, +} + +impl DatabaseKvConnection { + pub fn url(self) -> String { + let dbname = self + .dbname + .map(|dbname| format!("/{dbname}")) + .unwrap_or_default(); + let options = self + .options + .map(|options| format!("?{options}")) + .unwrap_or_default(); + format!( + "postgresql://{}:{}@{}:{}{}{}", + self.user, self.password, self.host, self.port, dbname, options + ) + } +} From 50ddc73602cd981567d64403ff63295cb2873e8c Mon Sep 17 00:00:00 2001 From: Kirill Fedoseev Date: Tue, 19 Dec 2023 15:06:23 +0400 Subject: [PATCH 2/3] chore: move to database.rs --- .../src/database.rs | 57 +++++++++++++++++++ .../src/launcher/settings.rs | 56 ------------------ 2 files changed, 57 insertions(+), 56 deletions(-) diff --git a/libs/blockscout-service-launcher/src/database.rs b/libs/blockscout-service-launcher/src/database.rs index 95534618e..0d8c9fcf2 100644 --- a/libs/blockscout-service-launcher/src/database.rs +++ b/libs/blockscout-service-launcher/src/database.rs @@ -1,4 +1,5 @@ use anyhow::Context; +use serde::Deserialize; use std::str::FromStr; cfg_if::cfg_if! { @@ -102,3 +103,59 @@ fn with_connect_options(url: impl Into, source_options: &ConnectOptions) options.sqlx_logging_level(source_options.get_sqlx_logging_level()); options } + +#[derive(Debug, Clone, Deserialize, PartialEq, Eq)] +#[serde(deny_unknown_fields)] +pub struct DatabaseSettings { + pub connect: DatabaseConnectSettings, + #[serde(default)] + pub create_database: bool, + #[serde(default)] + pub run_migrations: bool, +} + +#[derive(Debug, Clone, Deserialize, PartialEq, Eq)] +#[serde(deny_unknown_fields, rename_all = "lowercase")] +pub enum DatabaseConnectSettings { + Url(String), + Kv(DatabaseKvConnection), +} + +impl DatabaseConnectSettings { + pub fn url(self) -> String { + match self { + DatabaseConnectSettings::Url(s) => s, + DatabaseConnectSettings::Kv(kv) => kv.url(), + } + } +} + +#[derive(Debug, Clone, Deserialize, PartialEq, Eq)] +#[serde(deny_unknown_fields)] +pub struct DatabaseKvConnection { + pub host: String, + pub port: u16, + pub user: String, + pub password: String, + #[serde(default)] + pub dbname: Option, + #[serde(default)] + pub options: Option, +} + +impl DatabaseKvConnection { + pub fn url(self) -> String { + let dbname = self + .dbname + .map(|dbname| format!("/{dbname}")) + .unwrap_or_default(); + let options = self + .options + .map(|options| format!("?{options}")) + .unwrap_or_default(); + format!( + "postgresql://{}:{}@{}:{}{}{}", + self.user, self.password, self.host, self.port, dbname, options + ) + } +} diff --git a/libs/blockscout-service-launcher/src/launcher/settings.rs b/libs/blockscout-service-launcher/src/launcher/settings.rs index 2d38cefb1..31a10947d 100644 --- a/libs/blockscout-service-launcher/src/launcher/settings.rs +++ b/libs/blockscout-service-launcher/src/launcher/settings.rs @@ -137,59 +137,3 @@ impl Default for MetricsSettings { } } } - -#[derive(Debug, Clone, Deserialize, PartialEq, Eq)] -#[serde(deny_unknown_fields)] -pub struct DatabaseSettings { - pub connect: DatabaseConnectSettings, - #[serde(default)] - pub create_database: bool, - #[serde(default)] - pub run_migrations: bool, -} - -#[derive(Debug, Clone, Deserialize, PartialEq, Eq)] -#[serde(deny_unknown_fields, rename_all = "lowercase")] -pub enum DatabaseConnectSettings { - Url(String), - Kv(DatabaseKvConnection), -} - -impl DatabaseConnectSettings { - pub fn url(self) -> String { - match self { - DatabaseConnectSettings::Url(s) => s, - DatabaseConnectSettings::Kv(kv) => kv.url(), - } - } -} - -#[derive(Debug, Clone, Deserialize, PartialEq, Eq)] -#[serde(deny_unknown_fields)] -pub struct DatabaseKvConnection { - pub host: String, - pub port: u16, - pub user: String, - pub password: String, - #[serde(default)] - pub dbname: Option, - #[serde(default)] - pub options: Option, -} - -impl DatabaseKvConnection { - pub fn url(self) -> String { - let dbname = self - .dbname - .map(|dbname| format!("/{dbname}")) - .unwrap_or_default(); - let options = self - .options - .map(|options| format!("?{options}")) - .unwrap_or_default(); - format!( - "postgresql://{}:{}@{}:{}{}{}", - self.user, self.password, self.host, self.port, dbname, options - ) - } -} From f8d39bc444ebbe54d583a9d60ec4f5b920236c5c Mon Sep 17 00:00:00 2001 From: Kirill Fedoseev Date: Tue, 19 Dec 2023 15:06:43 +0400 Subject: [PATCH 3/3] chore: bump launcher version --- libs/blockscout-service-launcher/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/blockscout-service-launcher/Cargo.toml b/libs/blockscout-service-launcher/Cargo.toml index 0683abee5..8b60f80eb 100644 --- a/libs/blockscout-service-launcher/Cargo.toml +++ b/libs/blockscout-service-launcher/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "blockscout-service-launcher" -version = "0.9.0" +version = "0.10.0" description = "Allows to launch blazingly fast blockscout rust services" license = "MIT" repository = "https://github.com/blockscout/blockscout-rs"