diff --git a/src/processor.rs b/src/processor.rs index 425449ffe..5c2fde649 100644 --- a/src/processor.rs +++ b/src/processor.rs @@ -258,12 +258,9 @@ async fn execute_http_function( request.insert("queries", queries.into_py(py)); request.insert("headers", headers_python.into_py(py)); - match data { - Some(res) => { - let data = res.into_py(py); - request.insert("body", data); - } - None => {} + if let Some(res) = data { + let data = res.into_py(py); + request.insert("body", data); }; // this makes the request object to be accessible across every route @@ -288,7 +285,7 @@ async fn execute_http_function( if response_type == "static_file" { let file_path = res.get("file_path").unwrap(); let contents = read_file(file_path); - res.insert("body".to_owned(), contents.to_owned()); + res.insert("body".to_owned(), contents); } Ok(res) })?; @@ -302,12 +299,9 @@ async fn execute_http_function( let handler = handler.as_ref(py); request.insert("params", route_params.into_py(py)); request.insert("headers", headers_python.into_py(py)); - match data { - Some(res) => { - let data = res.into_py(py); - request.insert("body", data); - } - None => {} + if let Some(res) = data { + let data = res.into_py(py); + request.insert("body", data); }; let output: PyResult<&PyAny> = match number_of_params { @@ -331,25 +325,22 @@ pub async fn execute_event_handler( event_handler: Option>, event_loop: Arc>, ) { - match event_handler { - Some(handler) => match &(*handler) { - PyFunction::SyncFunction(function) => { - println!("Startup event handler"); - Python::with_gil(|py| { - function.call0(py).unwrap(); - }); - } - PyFunction::CoRoutine(function) => { - let future = Python::with_gil(|py| { - println!("Startup event handler async"); - - let coroutine = function.as_ref(py).call0().unwrap(); - pyo3_asyncio::into_future_with_loop((*event_loop).as_ref(py), coroutine) - .unwrap() - }); - future.await.unwrap(); - } - }, - None => {} - } + if let Some(handler) = event_handler { match &(*handler) { + PyFunction::SyncFunction(function) => { + println!("Startup event handler"); + Python::with_gil(|py| { + function.call0(py).unwrap(); + }); + } + PyFunction::CoRoutine(function) => { + let future = Python::with_gil(|py| { + println!("Startup event handler async"); + + let coroutine = function.as_ref(py).call0().unwrap(); + pyo3_asyncio::into_future_with_loop((*event_loop).as_ref(py), coroutine) + .unwrap() + }); + future.await.unwrap(); + } + } } } diff --git a/src/routers/router.rs b/src/routers/router.rs index ebf0ead11..01a95c97b 100644 --- a/src/routers/router.rs +++ b/src/routers/router.rs @@ -61,9 +61,9 @@ impl Router { Err(_) => return None, }; - return self.get_relevant_map(method); + self.get_relevant_map(method) } else { - return None; + None } } diff --git a/src/routers/web_socket_router.rs b/src/routers/web_socket_router.rs index e1c6632f1..2596f98da 100644 --- a/src/routers/web_socket_router.rs +++ b/src/routers/web_socket_router.rs @@ -7,8 +7,10 @@ use pyo3::types::PyAny; /// Contains the thread safe hashmaps of different routes +type WebSocketRoutes = RwLock>>; + pub struct WebSocketRouter { - web_socket_routes: RwLock>>, + web_socket_routes: WebSocketRoutes, } impl WebSocketRouter { @@ -21,7 +23,7 @@ impl WebSocketRouter { #[inline] pub fn get_web_socket_map( &self, - ) -> &RwLock>> { + ) -> &WebSocketRoutes { &self.web_socket_routes } diff --git a/src/server.rs b/src/server.rs index f43093de4..7b1925a75 100644 --- a/src/server.rs +++ b/src/server.rs @@ -311,29 +311,28 @@ async fn index( // try reading about arc or rc let mut queries = HashMap::new(); - if req.query_string().len() > 0 { - let split = req.query_string().split("&"); + if !req.query_string().is_empty() { + let split = req.query_string().split('&'); for s in split { let params = s.split_once("=").unwrap_or((s, "")); queries.insert(params.0.to_string(), params.1.to_string()); } } - let _ = match middleware_router.get_route("BEFORE_REQUEST", req.uri().path()) { - Some(((handler_function, number_of_params), route_params)) => { - let x = handle_middleware_request( - handler_function, - number_of_params, - &headers, - &mut payload, - &req, - route_params, - queries.clone(), - ) - .await; - println!("{:?}", x.to_string()); - } - None => {} + let _ = if let Some(((handler_function, number_of_params), route_params)) = + middleware_router.get_route("BEFORE_REQUEST", req.uri().path()) + { + let x = handle_middleware_request( + handler_function, + number_of_params, + &headers, + &mut payload, + &req, + route_params, + queries.clone(), + ) + .await; + println!("{:?}", x.to_string()); }; let response = match router.get_route(req.method().clone(), req.uri().path()) { @@ -356,21 +355,20 @@ async fn index( } }; - let _ = match middleware_router.get_route("AFTER_REQUEST", req.uri().path()) { - Some(((handler_function, number_of_params), route_params)) => { - let x = handle_middleware_request( - handler_function, - number_of_params, - &headers, - &mut payload, - &req, - route_params, - queries.clone(), - ) - .await; - println!("{:?}", x.to_string()); - } - None => {} + let _ = if let Some(((handler_function, number_of_params), route_params)) = + middleware_router.get_route("AFTER_REQUEST", req.uri().path()) + { + let x = handle_middleware_request( + handler_function, + number_of_params, + &headers, + &mut payload, + &req, + route_params, + queries.clone(), + ) + .await; + println!("{:?}", x.to_string()); }; response diff --git a/src/web_socket_connection.rs b/src/web_socket_connection.rs index a450e1c5d..bb1845729 100644 --- a/src/web_socket_connection.rs +++ b/src/web_socket_connection.rs @@ -62,7 +62,7 @@ impl Actor for MyWs { fn started(&mut self, ctx: &mut WebsocketContext) { let handler_function = &self.router.get("connect").unwrap().0; let _number_of_params = &self.router.get("connect").unwrap().1; - execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, &self); + execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, self); println!("Actor is alive"); } @@ -70,7 +70,7 @@ impl Actor for MyWs { fn stopped(&mut self, ctx: &mut WebsocketContext) { let handler_function = &self.router.get("close").expect("No close function").0; let _number_of_params = &self.router.get("close").unwrap().1; - execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, &self); + execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, self); println!("Actor is dead"); } @@ -89,7 +89,7 @@ impl StreamHandler> for MyWs { let handler_function = &self.router.get("connect").unwrap().0; let _number_of_params = &self.router.get("connect").unwrap().1; println!("{:?}", handler_function); - execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, &self); + execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, self); ctx.pong(&msg) } @@ -102,7 +102,7 @@ impl StreamHandler> for MyWs { // need to also passs this text as a param let handler_function = &self.router.get("message").unwrap().0; let _number_of_params = &self.router.get("message").unwrap().1; - execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, &self); + execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, self); } Ok(ws::Message::Binary(bin)) => ctx.binary(bin), @@ -110,7 +110,7 @@ impl StreamHandler> for MyWs { println!("Socket was closed"); let handler_function = &self.router.get("close").expect("No close function").0; let _number_of_params = &self.router.get("close").unwrap().1; - execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, &self); + execute_ws_functionn(handler_function, self.event_loop.clone(), ctx, self); } _ => (), } @@ -127,7 +127,7 @@ pub async fn start_web_socket( let resp = ws::start( MyWs { router, - event_loop: event_loop.clone(), + event_loop, }, &req, stream,