Introducing iron-middlefiddle: Route specific middleware made easy in Iron

Route specific middleware is a fairly common need in web apps. As soon as you introduce something like authentication into the mix, you’ll probably want a way to apply middleware to routes you only want users to be able to access if they’re logged in.

As things stand, route specific middleware in Iron is a repetitive and messy business, which is where iron_middlefiddle comes in. It provides a macro which turns the tricky into the trivial, allowing you to add middleware to any number of routes within a Router easily.

1let mut frontend_router = Router::new();
3// Add some `frontend_router` routes and the middleware they should use:
5middlefiddle! {
6 router => frontend_router,
7 routes => {
8 lorem: get "/lorem" => controllers::lorem::index,
9 ipsum: get "/ipsum" => controllers::ipsum::index,
10 dolor: get "/dolor" => controllers::dolor::index,
11 },
12 middleware => {
13 Middleware::BeforeMiddleware => middleware::auth::TokenValidity,
14 },
17// Add some more `frontend_router` routes that aren't going to need the middleware:
19frontend_router.get("/amet", controllers::amet::index, "amet");

iron_middlefiddle is well documented and available on Naturally, you’ll find its source code in the repo on GitHub.