From e3c66338c5c855a0eee254887def320d66e214f1 Mon Sep 17 00:00:00 2001 From: Suhail Malik Date: Sat, 29 Oct 2022 12:52:17 +0530 Subject: [PATCH] update enum name and location --- src/routers/middleware_router.rs | 15 ++++++----- src/routers/mod.rs | 1 + src/routers/router.rs | 16 ------------ src/routers/types.rs | 15 +++++++++++ src/server.rs | 43 ++++++++++++++++---------------- 5 files changed, 47 insertions(+), 43 deletions(-) create mode 100644 src/routers/types.rs diff --git a/src/routers/middleware_router.rs b/src/routers/middleware_router.rs index f7e8462fe..53199869f 100644 --- a/src/routers/middleware_router.rs +++ b/src/routers/middleware_router.rs @@ -9,7 +9,7 @@ use matchit::Node; use anyhow::{bail, Error, Result}; -use super::router::RouteType; +use super::types::MiddlewareRouteType; /// Contains the thread safe hashmaps of different routes @@ -27,10 +27,13 @@ impl MiddlewareRouter { } #[inline] - fn get_relevant_map(&self, route: RouteType) -> Option<&RwLock>> { + fn get_relevant_map( + &self, + route: MiddlewareRouteType, + ) -> Option<&RwLock>> { match route { - RouteType::BeforeRequest => Some(&self.before_request), - RouteType::AfterRequest => Some(&self.after_request), + MiddlewareRouteType::BeforeRequest => Some(&self.before_request), + MiddlewareRouteType::AfterRequest => Some(&self.after_request), } } @@ -38,7 +41,7 @@ impl MiddlewareRouter { // Inserts them in the router according to their nature(CoRoutine/SyncFunction) pub fn add_route( &self, - route_type: RouteType, // we can just have route type as WS + route_type: MiddlewareRouteType, // we can just have route type as WS route: &str, handler: Py, is_async: bool, @@ -65,7 +68,7 @@ impl MiddlewareRouter { pub fn get_route( &self, - route_method: RouteType, + route_method: MiddlewareRouteType, route: &str, // check for the route method here ) -> Option<((PyFunction, u8), HashMap)> { // need to split this function in multiple smaller functions diff --git a/src/routers/mod.rs b/src/routers/mod.rs index 8dff051de..77dd7c416 100644 --- a/src/routers/mod.rs +++ b/src/routers/mod.rs @@ -1,4 +1,5 @@ pub mod const_router; pub mod middleware_router; pub mod router; +pub mod types; pub mod web_socket_router; diff --git a/src/routers/router.rs b/src/routers/router.rs index 97a521d38..b75aef09d 100644 --- a/src/routers/router.rs +++ b/src/routers/router.rs @@ -10,22 +10,6 @@ use matchit::Node; use anyhow::{bail, Error, Result}; -#[derive(Debug)] -pub enum RouteType { - BeforeRequest, - AfterRequest, -} - -impl RouteType { - pub fn from_str(input: &str) -> RouteType { - match input { - "BEFORE_REQUEST" => RouteType::BeforeRequest, - "AFTER_REQUEST" => RouteType::AfterRequest, - _ => panic!("Invalid route type enum."), - } - } -} - /// Contains the thread safe hashmaps of different routes pub struct Router { diff --git a/src/routers/types.rs b/src/routers/types.rs new file mode 100644 index 000000000..c49774dd9 --- /dev/null +++ b/src/routers/types.rs @@ -0,0 +1,15 @@ +#[derive(Debug)] +pub enum MiddlewareRouteType { + BeforeRequest, + AfterRequest, +} + +impl MiddlewareRouteType { + pub fn from_str(input: &str) -> MiddlewareRouteType { + match input { + "BEFORE_REQUEST" => MiddlewareRouteType::BeforeRequest, + "AFTER_REQUEST" => MiddlewareRouteType::AfterRequest, + _ => panic!("Invalid route type enum."), + } + } +} diff --git a/src/server.rs b/src/server.rs index 76ad567d8..5a2a85fa5 100644 --- a/src/server.rs +++ b/src/server.rs @@ -4,7 +4,8 @@ use crate::request_handler::{handle_http_middleware_request, handle_http_request use crate::routers::const_router::ConstRouter; -use crate::routers::router::{RouteType, Router}; +use crate::routers::router::Router; +use crate::routers::types::MiddlewareRouteType; use crate::routers::{middleware_router::MiddlewareRouter, web_socket_router::WebSocketRouter}; use crate::shared_socket::SocketHeld; use crate::types::{Headers, PyFunction}; @@ -302,7 +303,7 @@ impl Server { ) { debug!("MiddleWare Route added for {} {} ", route_type, route); - let route_type = RouteType::from_str(route_type); + let route_type = MiddlewareRouteType::from_str(route_type); self.middleware_router .add_route(route_type, route, handler, is_async, number_of_params) @@ -397,24 +398,24 @@ async fn index( let headers = merge_headers(&global_headers, req.headers()).await; // need a better name for this - let tuple_params = match middleware_router.get_route(RouteType::BeforeRequest, req.uri().path()) - { - Some(((handler_function, number_of_params), route_params)) => { - let x = handle_http_middleware_request( - handler_function, - number_of_params, - &headers, - &mut payload, - &req, - route_params, - queries.clone(), - ) - .await; - debug!("Middleware contents {:?}", x); - x - } - None => HashMap::new(), - }; + let tuple_params = + match middleware_router.get_route(MiddlewareRouteType::BeforeRequest, req.uri().path()) { + Some(((handler_function, number_of_params), route_params)) => { + let x = handle_http_middleware_request( + handler_function, + number_of_params, + &headers, + &mut payload, + &req, + route_params, + queries.clone(), + ) + .await; + debug!("Middleware contents {:?}", x); + x + } + None => HashMap::new(), + }; debug!("These are the tuple params {:?}", tuple_params); @@ -460,7 +461,7 @@ async fn index( }; if let Some(((handler_function, number_of_params), route_params)) = - middleware_router.get_route(RouteType::AfterRequest, req.uri().path()) + middleware_router.get_route(MiddlewareRouteType::AfterRequest, req.uri().path()) { let x = handle_http_middleware_request( handler_function,