selective_middleware

Crates.ioselective_middleware
lib.rsselective_middleware
version
sourcesrc
created_at2015-09-08 09:46:02.669007
updated_at2015-12-11 23:55:55.315022
descriptionSelective middleware for Iron applications
homepagehttps://github.com/gchp/selective_middleware
repositoryhttps://github.com/gchp/selective_middleware
max_upload_size
id3002
Cargo.toml error:TOML parse error at line 8, column 1 | 8 | licence = "MIT" | ^^^^^^^ unknown field `licence`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Greg Chapple (gchp)

documentation

README

Iron Selective Middleware

This crate allows you to selectively add middleware to specific handlers in an Iron application.

Motivation

I needed a way to add middleware to specific Iron routes. Using the Iron Router and Chain, it was only possible to add middleware to the entire Router as a whole. I created this crate to allow middleware to be added to any handler in the application.

The use case I had was authentication middleware. I needed to add this middleware to every route which required the user to be authenticated. I didn't want to add it to the login handler, though. SelectiveMiddleWare solves this problem for me.

Installation

Add the following to the [dependencies] section of you Cargo.toml:

selective_middleware = "*"

Usage

extern crate iron;
extern crate router;
extern crate selective_middleware;

use iron::{Iron, status, IronResult, Request};
use router::Router;
use selective_middleware::SelectiveMiddleWare;

fn handler(req: &mut Request) -> IronResult<()> {
    Ok(Response::with((status::Ok, "Hello"))
}

fn main() {
    let mut router = Router::new();

    router.get("/", handler);
    router.get("/foo", SelectiveMiddleWare::new(handler, MyMiddleware));

    Iron::new(router).http("localhost:3000").unwrap();
}

With the above example, visiting http://localhost:3000/ will invoke the handler method. Visiting http://localhost:3000/foo will invoke MyMiddleware then the handler method.

There is also the with_middleware! convenience macro:

#[macro_use(with_middleware)]
extern crate selective_middleware;

fn main() {
    let mut router = Router::new();

    router.get("/", handler);
    router.get("/foo", with_middleware!(handler, MyMiddleware));

    Iron::new(router).http("localhost:3000").unwrap();
}
Commit count: 3

cargo fmt