Skip to content

Commit 4a8078a

Browse files
committed
Add AFTER REQUEST
1 parent 46b7856 commit 4a8078a

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

integration_tests/base_routes.py

+8
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ async def hello_before_request(request):
5555
return ""
5656

5757

58+
@app.after_request("/")
59+
async def hello_after_request(request):
60+
global callCount
61+
callCount += 1
62+
print(request)
63+
return ""
64+
65+
5866
@app.get("/test/:id")
5967
async def test(request):
6068
print(request)

robyn/__init__.py

+24
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,30 @@ def inner_handler(*args):
108108

109109
return inner
110110

111+
def after_request(self, endpoint):
112+
"""
113+
[The @app.after_request decorator to add a get route]
114+
115+
:param endpoint [str]: [endpoint to server the route]
116+
"""
117+
118+
def inner(handler):
119+
# add handling for async functions
120+
async def async_inner_handler(*args):
121+
await handler(args)
122+
return args
123+
124+
def inner_handler(*args):
125+
handler(*args)
126+
return args
127+
128+
if asyncio.iscoroutinefunction(handler):
129+
self.add_middleware_route("AFTER_REQUEST", endpoint, async_inner_handler)
130+
else:
131+
self.add_middleware_route("AFTER_REQUEST", endpoint, inner_handler)
132+
133+
return inner
134+
111135
def add_directory(
112136
self, route, directory_path, index_file=None, show_files_listing=False
113137
):

src/server.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ async fn index(
319319
mut payload: web::Payload,
320320
req: HttpRequest,
321321
) -> impl Responder {
322+
// cloning hashmaps a lot here
323+
// try reading about arc or rc
322324
let mut queries = HashMap::new();
323325

324326
if req.query_string().len() > 0 {
@@ -346,7 +348,7 @@ async fn index(
346348
None => {}
347349
};
348350

349-
match router.get_route(req.method().clone(), req.uri().path()) {
351+
let response = match router.get_route(req.method().clone(), req.uri().path()) {
350352
Some(((handler_function, number_of_params), route_params)) => {
351353
handle_request(
352354
handler_function,
@@ -355,7 +357,7 @@ async fn index(
355357
&mut payload,
356358
&req,
357359
route_params,
358-
queries,
360+
queries.clone(),
359361
)
360362
.await
361363
}
@@ -364,5 +366,24 @@ async fn index(
364366
apply_headers(&mut response, &headers);
365367
response.finish()
366368
}
367-
}
369+
};
370+
371+
let _ = match middleware_router.get_route("AFTER_REQUEST", req.uri().path()) {
372+
Some(((handler_function, number_of_params), route_params)) => {
373+
let x = handle_middleware_request(
374+
handler_function,
375+
number_of_params,
376+
&headers,
377+
&mut payload,
378+
&req,
379+
route_params,
380+
queries.clone(),
381+
)
382+
.await;
383+
println!("{:?}", x.to_string());
384+
}
385+
None => {}
386+
};
387+
388+
response
368389
}

0 commit comments

Comments
 (0)