Middleware that provides diesel database connections (currently PostgreSQL only) within iron requests. This is a port of iron-postgres-middleware.
Documentation can be found here.
-
Add the following to
Cargo.toml
:[dependencies.iron_diesel_middleware] git = "https://github.com/darayus/iron-diesel-middleware"
-
Include the crate and import the middleware:
extern crate iron_diesel_middleware; use iron_diesel_middleware::{DieselMiddleware, DieselReqExt};
-
Setup and add the middleware to iron:
let diesel_middleware = DieselMiddleware::new("postgresql://localhost/example_middleware").unwrap(); let mut chain = Chain::new(example_handler); chain.link_before(diesel_middleware); Iron::new(chain).http("127.0.0.1:8000").unwrap();
-
Use the diesel connection in requests:
// Requires that the DieselReqExt trait is included (for db_conn) fn example_handler(req: &mut Request) -> IronResult<Response> { let con = req.db_conn(); let response_str = do_something_with(&*con); return Ok(Response::with((status::Ok, response_str))); }
A working example can be found in examples/basic.rs.