From d7810a5d028558d801e03a062f18bfac9f023017 Mon Sep 17 00:00:00 2001 From: Nathan Li Date: Sat, 30 Mar 2024 20:57:13 -0400 Subject: [PATCH] modify to change zero2prod to be a library --- src/lib.rs | 13 +++++++++++++ src/main.rs | 21 ++------------------- 2 files changed, 15 insertions(+), 19 deletions(-) create mode 100644 src/lib.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..029d005 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,13 @@ +use actix_web::dev::Server; +use actix_web::{web, App, HttpResponse, HttpServer}; + +async fn health_check() -> HttpResponse { + HttpResponse::Ok().finish() +} + +pub fn run() -> Result { + let server = HttpServer::new(|| App::new().route("/health_check", web::get().to(health_check))) + .bind("127.0.0.1:8000")? + .run(); + Ok(server) +} diff --git a/src/main.rs b/src/main.rs index 3e2cba5..9bee6b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,23 +1,6 @@ -use actix_web::{web, App, HttpResponse, HttpServer, Responder}; - -// async fn greet(req: HttpRequest) -> impl Responder { -// let name = req.match_info().get("name").unwrap_or("World"); -// format!("Hello {}!", &name) -// } - -async fn health_check() -> impl Responder { - HttpResponse::Ok() -} +use zero2prod::run; #[tokio::main] async fn main() -> Result<(), std::io::Error> { - HttpServer::new(|| { - App::new() - // .route("/", web::get().to(greet)) - // .route("/{name}", web::get().to(greet)) - .route("/health_check", web::get().to(health_check)) - }) - .bind("127.0.0.1:8000")? - .run() - .await + run()?.await }