From c67a7f1e483d16faeae6c14b4d0af268b02c3060 Mon Sep 17 00:00:00 2001 From: banditopazzo Date: Tue, 5 Sep 2023 16:54:23 +0200 Subject: [PATCH 1/4] feat: update base container --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5617764..0e89f56 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Build container -FROM debian:buster AS builder +FROM debian:bookworm AS builder RUN apt-get update && apt-get install -y curl libssl-dev libpq-dev build-essential pkg-config @@ -12,7 +12,7 @@ ADD . ./ RUN cargo build --release # Deploy container -FROM debian:buster +FROM debian:bookworm RUN apt-get update && apt-get install -y libssl-dev libpq-dev ca-certificates From 24c5c7d4ca684edd3027121958350eefd3811668 Mon Sep 17 00:00:00 2001 From: banditopazzo Date: Tue, 5 Sep 2023 16:58:34 +0200 Subject: [PATCH 2/4] feat: better configuration --- Cargo.lock | 54 +++++++++++++++++++++++++++++++++++++ docker-compose.yml | 6 ++++- kepler/Cargo.toml | 4 ++- kepler/src/api/mod.rs | 4 +-- kepler/src/configuration.rs | 45 +++++++++++++++++++++++++++++++ kepler/src/main.rs | 34 +++++++++++------------ 6 files changed, 125 insertions(+), 22 deletions(-) create mode 100644 kepler/src/configuration.rs diff --git a/Cargo.lock b/Cargo.lock index d06fc01..bfe58cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -312,6 +312,17 @@ version = "1.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b13c32d80ecc7ab747b80c3784bce54ee8a7a0cc4fbda9bf4cda2cf6fe90854" +[[package]] +name = "async-trait" +version = "0.1.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc00ceb34980c03614e35a3a4e218276a0a824e911d07651cd0d858a51e8c0f0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.26", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -455,6 +466,19 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "config" +version = "0.13.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d379af7f68bfc21714c6c7dea883544201741d2ce8274bb12fa54f89507f52a7" +dependencies = [ + "async-trait", + "lazy_static", + "nom", + "pathdiff", + "serde", +] + [[package]] name = "convert_case" version = "0.4.0" @@ -624,6 +648,12 @@ dependencies = [ "version-compare", ] +[[package]] +name = "dotenvy" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" + [[package]] name = "encoding_rs" version = "0.8.30" @@ -1014,7 +1044,9 @@ dependencies = [ "actix-web", "anyhow", "clap", + "config", "domain-db", + "dotenvy", "env_logger", "lazy_static", "log", @@ -1141,6 +1173,12 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.7.1" @@ -1180,6 +1218,16 @@ dependencies = [ "tempfile", ] +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "nu-ansi-term" version = "0.46.0" @@ -1311,6 +1359,12 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1de2e551fb905ac83f73f7aedf2f0cb4a0da7e35efa24a202a936269f1f18e1" +[[package]] +name = "pathdiff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd" + [[package]] name = "percent-encoding" version = "2.1.0" diff --git a/docker-compose.yml b/docker-compose.yml index 29d1c61..f9261ce 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -26,7 +26,11 @@ services: - RUST_LOG=info - KEPLER_ADDRESS=0.0.0.0 - KEPLER_PORT=8000 - - DATABASE_URL=postgres://kepler:kepler@db/kepler + - DB_HOST=db + - DB_PORT=5432 + - DB_USER=kepler + - DB_PASSWORD=kepler + - DB_DATABASE=kepler ports: - "8000:8000" diff --git a/kepler/Cargo.toml b/kepler/Cargo.toml index 6495df6..52dd3f4 100644 --- a/kepler/Cargo.toml +++ b/kepler/Cargo.toml @@ -9,7 +9,7 @@ license.workspace = true [dependencies] domain-db = { path = "../domain-db" } -clap = {version = "4.3.15", features = ["derive"]} +clap = { version = "4.3.15", features = ["derive"] } env_logger = "0.10.0" log = "0.4.19" serde = { version = "1.0.171", features = ["derive"] } @@ -23,3 +23,5 @@ tracing-subscriber = { version = "0.3.17 ", features = ["env-filter"] } tracing-actix-web = "0.7.5" anyhow = "1.0.72" serde_json = "1.0.103" +config = { version = "0.13.3", default-features = false } +dotenvy = "0.15.7" diff --git a/kepler/src/api/mod.rs b/kepler/src/api/mod.rs index f544506..ffa234d 100644 --- a/kepler/src/api/mod.rs +++ b/kepler/src/api/mod.rs @@ -17,7 +17,7 @@ mod telemetry; pub use telemetry::init_logger; pub struct ApiConfig { - pub host: String, + pub address: String, pub port: u16, pub repository: PostgresRepository, } @@ -44,7 +44,7 @@ pub fn run(api_config: ApiConfig) -> Result { .wrap(Cors::permissive()) .wrap(tracing_actix_web::TracingLogger::default()) }) - .bind((api_config.host, api_config.port))? + .bind((api_config.address, api_config.port))? .run(); Ok(server) } diff --git a/kepler/src/configuration.rs b/kepler/src/configuration.rs new file mode 100644 index 0000000..5e39a5b --- /dev/null +++ b/kepler/src/configuration.rs @@ -0,0 +1,45 @@ +use config::{Config, Environment}; +use serde::Deserialize; + +#[derive(Deserialize, Clone, Debug)] +pub struct ApiSettings { + pub address: String, + pub port: u16, +} + +impl ApiSettings { + pub fn try_from_env() -> Result { + Config::builder() + .set_default("address", "0.0.0.0")? + .set_default("port", 8000)? + .add_source(Environment::with_prefix("KEPLER").prefix_separator("_")) + .build()? + .try_deserialize::() + } +} + +#[derive(Deserialize, Clone, Debug)] +pub struct DatabaseSettings { + host: String, + port: u16, + user: String, + password: String, + database: String, +} + +impl DatabaseSettings { + pub fn try_from_env() -> Result { + Config::builder() + .set_default("port", 5432)? + .add_source(Environment::with_prefix("DB").prefix_separator("_")) + .build()? + .try_deserialize::() + } + + pub fn connection_string(&self) -> String { + format!( + "postgres://{}:{}@{}:{}/{}", + self.user, self.password, self.host, self.port, self.database + ) + } +} diff --git a/kepler/src/main.rs b/kepler/src/main.rs index 74c4a53..206108e 100644 --- a/kepler/src/main.rs +++ b/kepler/src/main.rs @@ -1,20 +1,28 @@ use anyhow::{bail, Context, Result}; use clap::{Parser, Subcommand}; +use configuration::DatabaseSettings; use domain_db::{cve_sources::nist, db}; +use dotenvy::dotenv; use env_logger::Env; -use std::{env, fs, path::Path}; +use std::{fs, path::Path}; + +mod configuration; use kepler::api::{self, ApiConfig}; +use crate::configuration::ApiSettings; + #[actix_web::main] async fn main() -> Result<()> { let opts = Opts::parse(); + dotenv().ok(); + // Repository let repository = { - let database_url = env::var("DATABASE_URL") - .context("DATABASE_URL environment variable has not specified.")?; - db::PostgresRepository::new(&database_url, "./migrations") + let db_settings = DatabaseSettings::try_from_env()?; + + db::PostgresRepository::new(&db_settings.connection_string(), "./migrations") .context("Cannot connect to database")? }; @@ -70,22 +78,12 @@ async fn main() -> Result<()> { } }, None => { - let host = env::var("KEPLER_ADDRESS") - .map_err(|_| "Invalid or missing custom address") - .unwrap_or_else(|err| { - log::warn!("{}. Using default 0.0.0.0", err); - "0.0.0.0".to_string() - }); - let port = env::var("KEPLER_PORT") - .map_err(|_| "Invalid or missing custom port") - .and_then(|s| s.parse::().map_err(|_| "Failed to parse custom port")) - .unwrap_or_else(|err| { - log::warn!("{}. Using default 8000", err); - 8000 - }); + let ApiSettings { address, port } = ApiSettings::try_from_env()?; + + log::info!("Start listening on {}:{}...", address, port); let api_config = ApiConfig { - host, + address, port, repository, }; From a7247431844917213c743a2c859ef1fabe4cdd14 Mon Sep 17 00:00:00 2001 From: banditopazzo Date: Tue, 5 Sep 2023 17:03:35 +0200 Subject: [PATCH 3/4] refactor: remove kepler lib --- kepler/src/lib.rs | 16 ---------------- kepler/src/main.rs | 21 +++++++++++++++++---- 2 files changed, 17 insertions(+), 20 deletions(-) delete mode 100644 kepler/src/lib.rs diff --git a/kepler/src/lib.rs b/kepler/src/lib.rs deleted file mode 100644 index 977c6e6..0000000 --- a/kepler/src/lib.rs +++ /dev/null @@ -1,16 +0,0 @@ -use lazy_static::lazy_static; - -pub mod api; - -pub fn version() -> &'static str { - #[cfg(debug_assertions)] - lazy_static! { - static ref VERSION: String = format!("{}+dev", env!("CARGO_PKG_VERSION")); - } - - #[cfg(not(debug_assertions))] - lazy_static! { - static ref VERSION: String = env!("CARGO_PKG_VERSION").to_string(); - } - &VERSION -} diff --git a/kepler/src/main.rs b/kepler/src/main.rs index 206108e..cdf43ac 100644 --- a/kepler/src/main.rs +++ b/kepler/src/main.rs @@ -1,16 +1,16 @@ use anyhow::{bail, Context, Result}; use clap::{Parser, Subcommand}; -use configuration::DatabaseSettings; use domain_db::{cve_sources::nist, db}; use dotenvy::dotenv; use env_logger::Env; +use lazy_static::lazy_static; use std::{fs, path::Path}; +mod api; mod configuration; -use kepler::api::{self, ApiConfig}; - -use crate::configuration::ApiSettings; +use crate::api::ApiConfig; +use crate::configuration::{ApiSettings, DatabaseSettings}; #[actix_web::main] async fn main() -> Result<()> { @@ -196,3 +196,16 @@ fn report_message(num_records: u32) -> String { format!("{num_records} new records created") } } + +fn version() -> &'static str { + #[cfg(debug_assertions)] + lazy_static! { + static ref VERSION: String = format!("{}+dev", env!("CARGO_PKG_VERSION")); + } + + #[cfg(not(debug_assertions))] + lazy_static! { + static ref VERSION: String = env!("CARGO_PKG_VERSION").to_string(); + } + &VERSION +} From 0a131a7f6415000584e61a25e106761a184493a1 Mon Sep 17 00:00:00 2001 From: banditopazzo Date: Tue, 5 Sep 2023 17:05:05 +0200 Subject: [PATCH 4/4] refactor: move api module up --- kepler/src/{api/mod.rs => api.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename kepler/src/{api/mod.rs => api.rs} (100%) diff --git a/kepler/src/api/mod.rs b/kepler/src/api.rs similarity index 100% rename from kepler/src/api/mod.rs rename to kepler/src/api.rs