darayus / iron-diesel-middleware

Middleware that provides diesel connections for the Iron web framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Iron Diesel Middleware

Build Status

Middleware that provides diesel database connections within iron requests. This is a port of iron-postgres-middleware.

Documentation can be found here.

Usage

  1. Add the following to Cargo.toml:

    diesel = { version = ">= 1.0", features = ["postgres"] }
    [dependencies.iron_diesel_middleware]
    git = "https://github.com/darayus/iron-diesel-middleware"
  2. Include the crate and import the middleware:

    extern crate diesel;
    extern crate iron_diesel_middleware;
    use iron_diesel_middleware::{DieselMiddleware, DieselPooledConnection, DieselReqExt};
  3. Setup and add the middleware to iron:

    let diesel_middleware: DieselMiddleware<diesel::pg::PgConnection> = 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();
  4. 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: DieselPooledConnection<diesel::pg::PgConnection> = 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.

About

Middleware that provides diesel connections for the Iron web framework

License:MIT License


Languages

Language:Rust 100.0%