From 975c86365d90ece0e53267aa64c4ca7adffbef22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Mon, 3 Jun 2024 11:49:19 +0200 Subject: [PATCH] fix(generator): spawn tokio task for mt responses --- projekt-primer/generator/src/main.rs | 81 +++++++++++++++------------- 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/projekt-primer/generator/src/main.rs b/projekt-primer/generator/src/main.rs index 76b7e0d..e609345 100644 --- a/projekt-primer/generator/src/main.rs +++ b/projekt-primer/generator/src/main.rs @@ -14,8 +14,8 @@ use serde::{Deserialize, Serialize}; const PORT: u16 = 12345; -pub mod expression; -pub mod sequence; +// pub mod expression; +// pub mod sequence; #[derive(Serialize, Deserialize, Debug)] pub struct Project { @@ -167,47 +167,52 @@ async fn main() -> Result<(), Box> { let (stream, _) = listener.accept().await?; let io = TokioIo::new(stream); - let service = service_fn(move |req| { - async move { - match (req.method(), req.uri().path()) { - (&Method::GET, "/ping") => Ok::<_, Error>(Response::new(full( - serde_json::to_string(&get_project()).unwrap(), - ))), - (&Method::GET, "/sequence") => { - // - let sequences = sequences(); - Ok(Response::new(full( - serde_json::to_string(&sequences).unwrap(), - ))) - } - (&Method::POST, r) => { - let seqs = sequences(); - let sequences = seqs - // .iter() - .find(|&x| ("/sequence/".to_string() + &x.name) == r); - match sequences { - None => create_404(), - Some(s) if *s.name == "Arithmetic".to_string() => { - let body = collect_body(req).await?; - let request: SequenceRequest = serde_json::from_str(&body).unwrap(); - let range = request.range; - let seq = - Arithmetic::new(request.parameters[0], request.parameters[1]); - Ok(Response::new(full( - serde_json::to_string(&seq.range(range)).unwrap(), - ))) + tokio::task::spawn(async move { + let service = service_fn(move |req| { + async move { + match (req.method(), req.uri().path()) { + (&Method::GET, "/ping") => Ok::<_, Error>(Response::new(full( + serde_json::to_string(&get_project()).unwrap(), + ))), + (&Method::GET, "/sequence") => { + // + let sequences = sequences(); + Ok(Response::new(full( + serde_json::to_string(&sequences).unwrap(), + ))) + } + (&Method::POST, r) => { + let seqs = sequences(); + let sequences = seqs + .iter() + .find(|&x| ("/sequence/".to_string() + &x.name) == r); + match sequences { + None => create_404(), + Some(s) if *s.name == "Arithmetic".to_string() => { + let body = collect_body(req).await?; + let request: SequenceRequest = + serde_json::from_str(&body).unwrap(); + let range = request.range; + let seq = Arithmetic::new( + request.parameters[0], + request.parameters[1], + ); + Ok(Response::new(full( + serde_json::to_string(&seq.range(range)).unwrap(), + ))) + } + _ => panic!("Not implemented"), } - _ => panic!("Not implemented"), } - } - _ => create_404(), + _ => create_404(), + } } + }); + + if let Err(err) = http1::Builder::new().serve_connection(io, service).await { + println!("Error serving connection: {:?}", err); } }); - - if let Err(err) = http1::Builder::new().serve_connection(io, service).await { - println!("Error serving connection: {:?}", err); - } } }