Skip to content

Middleware that provides diesel connections for the Iron web framework

License

Notifications You must be signed in to change notification settings

darayus/iron-diesel-middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages