Skip to content

Commit

Permalink
Accept a Config instead of a string by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
khuey committed Jul 31, 2019
1 parent 134d04b commit c406159
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
4 changes: 2 additions & 2 deletions postgres/examples/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ fn main() {

hyper::rt::run(lazy(move || {
// First start the bb8 pool.
let pg_mgr = PostgresConnectionManager::new(
let pg_mgr = PostgresConnectionManager::new_from_stringlike(
"postgresql://postgres:mysecretpassword@localhost:5432",
tokio_postgres::NoTls,
);
).unwrap();

Pool::builder()
.build(pg_mgr)
Expand Down
5 changes: 4 additions & 1 deletion postgres/examples/static_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ use futures::{
Future, Stream,
};

use std::str::FromStr;

// 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 config = tokio_postgres::config::Config::from_str("postgresql://postgres:mysecretpassword@localhost:5432").unwrap();
let pg_mgr = PostgresConnectionManager::new(
"postgresql://postgres:mysecretpassword@localhost:5432",
config,
tokio_postgres::NoTls,
);

Expand Down
4 changes: 2 additions & 2 deletions postgres/examples/txn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ use futures::{
// 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(
let pg_mgr = PostgresConnectionManager::new_from_stringlike(
"postgresql://postgres:mysecretpassword@localhost:5432",
tokio_postgres::NoTls,
);
).unwrap();

tokio::run(lazy(|| {
Pool::builder()
Expand Down
25 changes: 17 additions & 8 deletions postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,44 @@ pub extern crate tokio_postgres;
extern crate futures;

use futures::prelude::*;
use tokio_postgres::config::Config;
use tokio_postgres::tls::{MakeTlsConnect, TlsConnect};
use tokio_postgres::{Client, Error, Socket};

use std::fmt;
use std::str::FromStr;

/// A `bb8::ManageConnection` for `tokio_postgres::Connection`s.
#[derive(Clone)]
pub struct PostgresConnectionManager<Tls>
where
Tls: MakeTlsConnect<Socket>,
{
params: String,
config: Config,
tls: Tls,
}

impl<Tls> PostgresConnectionManager<Tls>
where
Tls: MakeTlsConnect<Socket>,
{
/// Create a new `PostgresConnectionManager`.
pub fn new<T>(params: T, tls: Tls) -> PostgresConnectionManager<Tls>
where
T: ToString,
/// Create a new `PostgresConnectionManager` with the specified `config`.
pub fn new(config: Config, tls: Tls) -> PostgresConnectionManager<Tls>
{
PostgresConnectionManager {
params: params.to_string(),
config: config,
tls: tls,
}
}

/// Create a new `PostgresConnectionManager`, parsing the config from `params`.
pub fn new_from_stringlike<T>(params: T, tls: Tls) -> Result<PostgresConnectionManager<Tls>, Error>
where T: ToString
{
let stringified_params = params.to_string();
let config = Config::from_str(&stringified_params)?;
Ok(Self::new(config, tls))
}
}

impl<Tls> bb8::ManageConnection for PostgresConnectionManager<Tls>
Expand All @@ -51,7 +60,7 @@ where
fn connect(
&self,
) -> Box<Future<Item = Self::Connection, Error = Self::Error> + Send + 'static> {
Box::new(tokio_postgres::connect(&self.params, self.tls.clone()).map(
Box::new(self.config.connect(self.tls.clone()).map(
|(client, connection)| {
// The connection object performs the actual communication with the database,
// so spawn it off to run on its own.
Expand Down Expand Up @@ -84,7 +93,7 @@ where
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("PostgresConnectionManager")
.field("params", &self.params)
.field("config", &self.config)
.finish()
}
}

0 comments on commit c406159

Please sign in to comment.