Skip to content

Different middleware for different groups #292

Answered by davidpdrsn
cemoktra asked this question in Q&A
Discussion options

You must be logged in to vote

While it is valid it probably doesn't do what you want. layer applies to all routes above it, meaning auth is applied twice to the first two routes.

You can do something like this instead:

let foo = Router::new()
    .route("/", get(root))
    .route("/some", get(some))
    .layer(auth);

let bar = Router::new()
    .route("/", post(root))
    .layer(auth)
    .layer(db);

let baz = Router::new()
    .route("/login", post(login));

let app = foo.or(bar).or(baz);

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@cemoktra
Comment options

@davidpdrsn
Comment options

@hkaiser25
Comment options

@jplatte
Comment options

Answer selected by cemoktra
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants