-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inject dependency into a filter using openapi macro #102
Comments
After digging into the source code a bit, I found that the filters don't take any parameters. fn token_header() -> impl Clone + Filter<Extract = (String,), Error = Rejection> {
rweb::header::<String>("authorization")
}
#[get("/products")]
#[openapi(id = "products.get")]
#[openapi(summary = "List products")]
#[openapi(response(code = "200", description = "OK"))]
#[openapi(response(code = "401", description = "Unauthorized"))]
pub fn list_products(
#[data] db_service: DbService,
#[filter = "token_header"] token: String,
) -> Json<Vec<Product>> {
**// Call to authenticate(db_service, token)**
Json::from(db_service.list_products().unwrap())
} However, I would like to call the authenticate method from a filter and be able to reuse it in the other handlers. |
See Lines 48 to 49 in 624d66e
|
@kdy1 afaiu The question is about using
|
In fact, I am looking for filters where we can inject any kind of dependencies and do some middleware tasks like logging, tracing, authentication and authorization. I know this is possible with the normal handlers, but I have not seen how to do it through the openapi macros In this tutorial there is an example of a filter which receives three dependencies, I'm trying to achieve the same with openapi |
What if you build a function that returns a function ? That is, you could potentially make a function that receives dependencies and returns a function. But the problem will remain, because it will not be possible to inject this filter using a macro :/ |
Yea and I think using the macro is essential because makes the code more ergonomic |
Hello,
I'm using the openapi feature and trying to pass the database service to the filter, is this possible?
I need to implement a custom filter to compare the header content against the database records.
This is the code I have:
When I run this code it returns the following error:
The text was updated successfully, but these errors were encountered: