Skip to content

Commit

Permalink
Add AFTER REQUEST
Browse files Browse the repository at this point in the history
  • Loading branch information
sansyrox committed Feb 9, 2022
1 parent 46b7856 commit 4a8078a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
8 changes: 8 additions & 0 deletions integration_tests/base_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ async def hello_before_request(request):
return ""


@app.after_request("/")
async def hello_after_request(request):
global callCount
callCount += 1
print(request)
return ""


@app.get("/test/:id")
async def test(request):
print(request)
Expand Down
24 changes: 24 additions & 0 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,30 @@ def inner_handler(*args):

return inner

def after_request(self, endpoint):
"""
[The @app.after_request decorator to add a get route]
:param endpoint [str]: [endpoint to server the route]
"""

def inner(handler):
# add handling for async functions
async def async_inner_handler(*args):
await handler(args)
return args

def inner_handler(*args):
handler(*args)
return args

if asyncio.iscoroutinefunction(handler):
self.add_middleware_route("AFTER_REQUEST", endpoint, async_inner_handler)
else:
self.add_middleware_route("AFTER_REQUEST", endpoint, inner_handler)

return inner

def add_directory(
self, route, directory_path, index_file=None, show_files_listing=False
):
Expand Down
27 changes: 24 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ async fn index(
mut payload: web::Payload,
req: HttpRequest,
) -> impl Responder {
// cloning hashmaps a lot here
// try reading about arc or rc
let mut queries = HashMap::new();

if req.query_string().len() > 0 {
Expand Down Expand Up @@ -346,7 +348,7 @@ async fn index(
None => {}
};

match router.get_route(req.method().clone(), req.uri().path()) {
let response = match router.get_route(req.method().clone(), req.uri().path()) {
Some(((handler_function, number_of_params), route_params)) => {
handle_request(
handler_function,
Expand All @@ -355,7 +357,7 @@ async fn index(
&mut payload,
&req,
route_params,
queries,
queries.clone(),
)
.await
}
Expand All @@ -364,5 +366,24 @@ async fn index(
apply_headers(&mut response, &headers);
response.finish()
}
}
};

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 => {}
};

response
}

0 comments on commit 4a8078a

Please sign in to comment.