diff --git a/Cargo.lock b/Cargo.lock index 599b85c..01832a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -285,6 +285,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi 0.1.19", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -613,6 +624,8 @@ dependencies = [ "actix-web", "chrono", "config", + "env_logger", + "log", "reqwest", "serde", "sqlx", @@ -629,6 +642,19 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "env_logger" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a12e6657c4c97ebab115a42dcee77225f7f482cdd841cf7088c657a42e9e00e7" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -899,6 +925,15 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hermit-abi" version = "0.3.9" @@ -995,6 +1030,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "1.2.0" @@ -1363,7 +1404,7 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.9", "libc", ] @@ -2332,6 +2373,15 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + [[package]] name = "thiserror" version = "1.0.59" @@ -2740,6 +2790,37 @@ dependencies = [ "wasite", ] +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-core" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index 2f6059d..e52724b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,8 @@ edition = "2021" [dependencies] actix-web = "4.5.1" reqwest = "0.12.2" +env_logger = "0.9" +log = "0.4" serde = { version = "1.0.197", features = ["derive"] } tokio = { version = "1.36.0", features = ["full"] } config = "0.13" diff --git a/docs/technical_write_up.md b/docs/technical_write_up.md index 57af713..bf80dde 100644 --- a/docs/technical_write_up.md +++ b/docs/technical_write_up.md @@ -2,8 +2,8 @@ ## Routes -- `health_check`: returns a HTTP code 200 if the server is up and running. Response body is empty. -- `subscribe` returns a HTTP code 200 if the user successfully subscribed to our email newsletter service. 400 if data is missing or invalid. +- `health_check`: GET - returns a HTTP code 200 if the server is up and running. Response body is empty. +- `subscriptions` POST - returns a HTTP code 200 if the user successfully subscribed to our email newsletter service. 400 if data is missing or invalid. ## Other topics diff --git a/src/main.rs b/src/main.rs index 2a03d61..a6c70fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,15 @@ use std::net::TcpListener; use email_newsletter_api::{configuration::get_configuration, startup}; +use env_logger::Env; use sqlx::PgPool; #[tokio::main] async fn main() -> Result<(), std::io::Error> { let configuration = get_configuration().expect("Failed to read configuration"); + env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); + let db_conn = PgPool::connect(&configuration.database.connection_string()) .await .expect("Failed to connect to PostgreSQL"); diff --git a/src/routes/subscriptions.rs b/src/routes/subscriptions.rs index 2e2ff2c..955af07 100644 --- a/src/routes/subscriptions.rs +++ b/src/routes/subscriptions.rs @@ -13,6 +13,15 @@ pub async fn subscribe_route( form: web::Form, db_conn_pool: web::Data, ) -> HttpResponse { + let request_id = Uuid::new_v4(); + + log::info!( + "request_id {} - Saving '{}' '{}' as a new subscriber in PostgreSQL", + request_id, + form.name, + form.email + ); + match sqlx::query!( r#" INSERT INTO subscriptions (id, email, name, subscribed_at) @@ -26,9 +35,19 @@ pub async fn subscribe_route( .execute(db_conn_pool.get_ref()) .await { - Ok(_) => HttpResponse::Ok().finish(), - Err(e) => { - println!("Failed to execute query: {}", e); + Ok(_) => { + log::info!( + "request_id {} - Saved new subscriber details in PostgreSQL", + request_id + ); + HttpResponse::Ok().finish() + } + Err(err) => { + log::info!( + "request_id {} - Failed to execute query: {:?}", + request_id, + err + ); HttpResponse::InternalServerError().finish() } } diff --git a/src/startup.rs b/src/startup.rs index 4633b4d..626fcb2 100644 --- a/src/startup.rs +++ b/src/startup.rs @@ -1,5 +1,6 @@ use crate::routes::{healthcheck_route, subscribe_route}; use actix_web::dev::Server; +use actix_web::middleware::Logger; use actix_web::{web, App, HttpServer}; use sqlx::PgPool; use std::net::TcpListener; @@ -11,8 +12,9 @@ pub fn run(listener: TcpListener, db_conn_pool: PgPool) -> Result