When registering services, one can optionally wrap these in additional middleware.
However, the only way I found to do so in separate and then aggregate was using the .scope
function.
I particularly want to be able to give different scopes different combinations of middleware but I dont actually want to separate the scopes by a prefix.
mod auth;
mod info;
use crate::auth::{api_key::api_key_middleware, bearer::jwt_middleware};
use actix_web::web::{scope, ServiceConfig};
use actix_web_lab::middleware::from_fn;
pub fn mount(actix_config: &mut ServiceConfig) {
let public = scope("").service(info::info);
let with_key = scope("")
.wrap(from_fn(api_key_middleware))
.service(auth::login)
.service(auth::register);
let with_key_and_jwt = scope("")
.wrap(from_fn(api_key_middleware))
.wrap(from_fn(jwt_middleware))
.service(auth::check);
actix_config
.service(public)
.service(with_key)
.service(with_key_and_jwt);
}
Firstly, this does not work because there cant be multiple scope("")
calls, this will collide and routing of requests wont be made properly. Additionally, I dont want to create custom scopes like scope("auth")
or scope("jwt")
.
Is there a way to do so?
Tried scoping with empty strings but it is a) ugly and b) doesnt work since scopes collide and requests wont be properly routed.