From: Martin Pitt Date: Tue, 13 Dec 2022 13:29:05 +0000 (+0100) Subject: actix-server: Factorize App creation X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=commitdiff_plain;h=121e3e9e83ec2bba3f0 actix-server: Factorize App creation This is too hard as a proper function, as App is too template-y. Use a macro instead. Thanks to Aravinth Manivannan for the suggestion! --- diff --git a/actix-server/src/main.rs b/actix-server/src/main.rs index af63efd..44ed45c 100644 --- a/actix-server/src/main.rs +++ b/actix-server/src/main.rs @@ -100,17 +100,24 @@ async fn ws_rev(req: HttpRequest, stream: web::Payload) -> Result std::io::Result<()> { - env_logger::init_from_env(env_logger::Env::default().default_filter_or("info")); - - HttpServer::new(|| { +// App is a template soup, too hard as a proper function +macro_rules! get_app { + () => { App::new() .service(hello) .service(static_file) .service(Files::new("/dir", "../static")) .service(ws_echo) .service(ws_rev) + } +} + +#[actix_web::main] +async fn main() -> std::io::Result<()> { + env_logger::init_from_env(env_logger::Env::default().default_filter_or("info")); + + HttpServer::new(|| { + get_app!() .wrap(Logger::default()) }) .bind(("127.0.0.1", 3030))? @@ -123,6 +130,7 @@ mod tests { use actix_web::{App, body, test, web}; use actix_web::http::{header, StatusCode}; use actix_web_actors::ws; + use actix_files::Files; use futures_util::sink::SinkExt; use futures_util::StreamExt; @@ -131,8 +139,7 @@ mod tests { #[actix_web::test] async fn test_hello() { - // FIXME: duplicating the .service() call from main() here is super ugly, but it's hard to move that into a fn - let app = test::init_service(App::new().service(hello)).await; + let app = test::init_service(get_app!()).await; // no user-agent let req = test::TestRequest::get().uri("/hello/rust").to_request(); @@ -154,8 +161,7 @@ mod tests { #[actix_web::test] async fn test_static_dir() { - // FIXME: duplicating the .service() call from main() here is super ugly, but it's hard to move that into a fn - let app = test::init_service(App::new().service(actix_files::Files::new("/dir", "../static"))).await; + let app = test::init_service(get_app!()).await; let req = test::TestRequest::get().uri("/dir/plain.txt").to_request(); let res = test::call_service(&app, req).await; @@ -179,8 +185,7 @@ mod tests { #[actix_web::test] async fn test_static_file() { - // FIXME: duplicating the .service() call from main() here is super ugly, but it's hard to move that into a fn - let app = test::init_service(App::new().service(static_file)).await; + let app = test::init_service(get_app!()).await; // uncompressed let req = test::TestRequest::get().uri("/file/dir1/optzip.txt").to_request(); @@ -204,8 +209,7 @@ mod tests { #[actix_web::test] async fn test_ws_echo() { - // FIXME: duplicating the .service() call from main() here is super ugly, but it's hard to move that into a fn - let mut srv = actix_test::start(|| App::new().service(ws_echo)); + let mut srv = actix_test::start(|| get_app!()); let mut client = srv.ws_at("/ws-echo").await.unwrap(); // text echo @@ -221,8 +225,7 @@ mod tests { #[actix_web::test] async fn test_ws_rev() { - // FIXME: duplicating the .service() call from main() here is super ugly, but it's hard to move that into a fn - let mut srv = actix_test::start(|| App::new().service(ws_rev)); + let mut srv = actix_test::start(|| get_app!()); let mut client = srv.ws_at("/ws-rev").await.unwrap(); // text reversed