1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use diesel::pg::PgConnection;
use diesel::r2d2::ConnectionManager;
use dotenv::dotenv;
use log::error;
use r2d2::{Pool, PooledConnection};
use std::env;
pub type PostgresPool = Pool<ConnectionManager<PgConnection>>;
pub type PgConnect = PooledConnection<ConnectionManager<PgConnection>>;
pub fn get_pool(url: &str) -> PostgresPool {
let mng = ConnectionManager::<PgConnection>::new(url);
r2d2::Pool::builder()
.build(mng)
.expect("could not build connection pool")
}
pub fn env_db() -> String {
dotenv().ok();
match env::var("DATABASE_URL") {
Ok(url) => url,
Err(_) => {
error!("A 'DATABASE_URL' environment variable is required.");
panic!()
}
}
}