diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2017-10-01 22:04:47 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2017-10-01 22:04:47 +0200 |
commit | 370790ab51a542cf9b56d21548451c84e741701e (patch) | |
tree | e0f3dc621020f6043337548ec2cf6385a366de9e /src/lib.rs | |
parent | e44bd988930babed7de59d78ad570b1b0a6e1cf0 (diff) | |
download | rocket-blog-370790ab51a542cf9b56d21548451c84e741701e.tar.gz rocket-blog-370790ab51a542cf9b56d21548451c84e741701e.tar.bz2 rocket-blog-370790ab51a542cf9b56d21548451c84e741701e.zip |
Move database setup to lib.rs.
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..5152ae5 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,27 @@ +#[macro_use] +extern crate diesel; +#[macro_use] +extern crate diesel_codegen; +extern crate dotenv; +extern crate r2d2_diesel; +extern crate r2d2; + +use diesel::pg::PgConnection; +use r2d2_diesel::ConnectionManager; +use dotenv::dotenv; +use std::env; + +// An alias to the type for a pool of Diesel PostgreSql connections. +type Pool = r2d2::Pool<ConnectionManager<PgConnection>>; + +/// Initializes a database pool. +pub fn init_db_pool() -> Pool { + dotenv().ok(); + + let config = r2d2::Config::default(); + let dburl = env::var("DATABASE_URL") + .expect("DATABASE_URL environment variable must be set"); + + let manager = ConnectionManager::<PgConnection>::new(dburl); + r2d2::Pool::new(config, manager).expect("db pool") +} |