Permalink
Cannot retrieve contributors at this time
Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign up
Fetching contributors…
| extern crate bb8; | |
| extern crate bb8_postgres; | |
| extern crate futures; | |
| extern crate futures_state_stream; | |
| extern crate tokio; | |
| extern crate tokio_postgres; | |
| use bb8::Pool; | |
| use bb8_postgres::PostgresConnectionManager; | |
| use futures::{ | |
| future::{err, lazy, Either}, | |
| Future, Stream, | |
| }; | |
| // Select some static data from a Postgres DB | |
| // | |
| // The simplest way to start the db is using Docker: | |
| // docker run --name gotham-middleware-postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres | |
| fn main() { | |
| let pg_mgr = PostgresConnectionManager::new( | |
| "postgresql://postgres:mysecretpassword@localhost:5432", | |
| tokio_postgres::NoTls, | |
| ); | |
| tokio::run(lazy(|| { | |
| Pool::builder() | |
| .build(pg_mgr) | |
| .and_then(|pool| { | |
| pool.run(|mut connection| { | |
| connection.prepare("SELECT 1").then(move |r| match r { | |
| Ok(select) => { | |
| let f = connection | |
| .query(&select, &[]) | |
| .for_each(|row| { | |
| println!("result: {}", row.get::<usize, String>(0)); | |
| Ok(()) | |
| }) | |
| .then(move |r| match r { | |
| Ok(_) => Ok(((), connection)), | |
| Err(e) => Err((e, connection)), | |
| }); | |
| Either::A(f) | |
| } | |
| Err(e) => Either::B(err((e, connection))), | |
| }) | |
| }) | |
| }) | |
| .map_err(|e| panic!("{:?}", e)) | |
| })); | |
| } |