summaryrefslogtreecommitdiffstats
path: root/service-person/src/db.rs
diff options
context:
space:
mode:
Diffstat (limited to 'service-person/src/db.rs')
-rw-r--r--service-person/src/db.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/service-person/src/db.rs b/service-person/src/db.rs
new file mode 100644
index 0000000..1094233
--- /dev/null
+++ b/service-person/src/db.rs
@@ -0,0 +1,24 @@
+use std::str::FromStr;
+
+use diesel::pg::PgConnection;
+use diesel::r2d2::ConnectionManager;
+use diesel::r2d2::Pool;
+
+pub type DbPool = Pool<ConnectionManager<PgConnection>>;
+
+pub fn establish_connection() -> DbPool {
+ let database_url = std::env::var("DATABASE_URL")
+ .expect("DATABASE_URL must be set");
+
+ let pool_max_size = std::env::var("DATABASE_MAX_CONNECTIONS")
+ .as_ref()
+ .map(|s| u32::from_str(s))
+ .expect("DATABASE_URL must be set")
+ .expect("Failed to parse string into integer for DATABASE_MAX_CONNECTIONS");
+
+ let manager = diesel::r2d2::ConnectionManager::new(&database_url);
+ diesel::r2d2::Pool::builder()
+ .max_size(pool_max_size)
+ .build(manager)
+ .expect(&format!("Error connection to {}", database_url))
+}