Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
VishnuSanal committed Oct 18, 2024
1 parent ac17c3e commit bacc27d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,24 @@ impl Server {
&self,
middleware_type: &MiddlewareType,
route: &str,
http_method: HttpMethod,
function: FunctionInfo,
) {
let mut endpoint = http_method.to_string().to_owned();

if !route.starts_with('/') {
endpoint.push('/');
}

endpoint.push_str(route);

debug!(
"MiddleWare Route added for {:?} {} ",
middleware_type, route
middleware_type, &endpoint
);

self.middleware_router
.add_route(middleware_type, route, function, None)
.add_route(middleware_type, &endpoint, function, None)
.unwrap();
}

Expand Down Expand Up @@ -420,13 +430,16 @@ async fn index(
) -> impl Responder {
let mut request = Request::from_actix_request(&req, payload, &global_request_headers).await;

let mut route = req.method().as_str().to_owned();
route.push_str(req.uri().path());

// Before middleware
// Global
let mut before_middlewares =
middleware_router.get_global_middlewares(&MiddlewareType::BeforeRequest);
// Route specific
if let Some((function, route_params)) =
middleware_router.get_route(&MiddlewareType::BeforeRequest, req.uri().path())
middleware_router.get_route(&MiddlewareType::BeforeRequest, &route)
{
before_middlewares.push(function);
request.path_params = route_params;
Expand Down
7 changes: 7 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ impl HttpMethod {
}
}

// for: https://stackoverflow.com/a/32712140/9652621
impl std::fmt::Display for HttpMethod {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}

#[pyclass]
#[derive(Default, Debug, Clone)]
pub struct Url {
Expand Down

0 comments on commit bacc27d

Please sign in to comment.