From f08def8e773a0837a05f780f90faf930c5f0403e Mon Sep 17 00:00:00 2001 From: minhtrannhat Date: Fri, 3 May 2024 15:43:34 -0400 Subject: [PATCH] fix: restructure the logic --- src/configuration.rs | 0 src/lib.rs | 31 +++---------------------------- src/main.rs | 6 +++--- src/routes/health_check.rs | 5 +++++ src/routes/mod.rs | 5 +++++ src/routes/subscriptions.rs | 11 +++++++++++ src/startup.rs | 16 ++++++++++++++++ tests/test_utils.rs | 2 +- 8 files changed, 44 insertions(+), 32 deletions(-) create mode 100644 src/configuration.rs create mode 100644 src/routes/health_check.rs create mode 100644 src/routes/mod.rs create mode 100644 src/routes/subscriptions.rs create mode 100644 src/startup.rs diff --git a/src/configuration.rs b/src/configuration.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/lib.rs b/src/lib.rs index 469f526..6a76aba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,29 +1,4 @@ -use actix_web::dev::Server; -use actix_web::{web, App, HttpResponse, HttpServer}; -use std::net::TcpListener; +pub mod configuration; +pub mod routes; +pub mod startup; -#[derive(serde::Deserialize)] -struct FormData { - email: String, - name: String, -} - -async fn healthcheck_route() -> HttpResponse { - HttpResponse::Ok().finish() -} - -async fn subscribe_route(_form: web::Form) -> HttpResponse { - HttpResponse::Ok().finish() -} - -pub fn run(listener: TcpListener) -> Result { - let server = HttpServer::new(|| { - App::new() - .route("/health_check", web::get().to(healthcheck_route)) - .route("/subscribe", web::post().to(subscribe_route)) - }) - .listen(listener)? - .run(); - - Ok(server) -} diff --git a/src/main.rs b/src/main.rs index 5cd6fd8..86176b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,12 @@ use std::net::TcpListener; -use email_newsletter_api::run; +use email_newsletter_api::startup; #[tokio::main] -async fn main() -> Result<(), std::io::Error>{ +async fn main() -> Result<(), std::io::Error> { let listener = TcpListener::bind("127.0.0.1:8000").expect("Failed to bind to port 8000"); // Move the error up the call stack // otherwise await for the HttpServer - run(listener)?.await + startup::run(listener)?.await } diff --git a/src/routes/health_check.rs b/src/routes/health_check.rs new file mode 100644 index 0000000..f1ddf6d --- /dev/null +++ b/src/routes/health_check.rs @@ -0,0 +1,5 @@ +use actix_web::HttpResponse; + +pub async fn healthcheck_route() -> HttpResponse { + HttpResponse::Ok().finish() +} diff --git a/src/routes/mod.rs b/src/routes/mod.rs new file mode 100644 index 0000000..90ffeed --- /dev/null +++ b/src/routes/mod.rs @@ -0,0 +1,5 @@ +mod health_check; +mod subscriptions; + +pub use health_check::*; +pub use subscriptions::*; diff --git a/src/routes/subscriptions.rs b/src/routes/subscriptions.rs new file mode 100644 index 0000000..75a1e10 --- /dev/null +++ b/src/routes/subscriptions.rs @@ -0,0 +1,11 @@ +use actix_web::{web, HttpResponse}; + +#[derive(serde::Deserialize)] +pub struct FormData { + email: String, + name: String, +} + +pub async fn subscribe_route(_form: web::Form) -> HttpResponse { + HttpResponse::Ok().finish() +} diff --git a/src/startup.rs b/src/startup.rs new file mode 100644 index 0000000..9d1bc2d --- /dev/null +++ b/src/startup.rs @@ -0,0 +1,16 @@ +use crate::routes::{healthcheck_route, subscribe_route}; +use actix_web::dev::Server; +use actix_web::{web, App, HttpServer}; +use std::net::TcpListener; + +pub fn run(listener: TcpListener) -> Result { + let server = HttpServer::new(|| { + App::new() + .route("/health_check", web::get().to(healthcheck_route)) + .route("/subscribe", web::post().to(subscribe_route)) + }) + .listen(listener)? + .run(); + + Ok(server) +} diff --git a/tests/test_utils.rs b/tests/test_utils.rs index 00a6bcc..111c7dc 100644 --- a/tests/test_utils.rs +++ b/tests/test_utils.rs @@ -11,7 +11,7 @@ pub fn spawn_app() -> String { let port = listener.local_addr().unwrap().port(); - let server = email_newsletter_api::run(listener).expect("Failed to bind address"); + let server = email_newsletter_api::startup::run(listener).expect("Failed to bind address"); /* `tokio::spawn(/*async task*/)` will spawn an async task to be run. We can continue executing other code concurrently while `task` runs in the background.