diff options
Diffstat (limited to 'service-person/src/main.rs')
-rw-r--r-- | service-person/src/main.rs | 27 |
1 files changed, 3 insertions, 24 deletions
diff --git a/service-person/src/main.rs b/service-person/src/main.rs index 2c86a37..5fd9a9d 100644 --- a/service-person/src/main.rs +++ b/service-person/src/main.rs @@ -7,37 +7,16 @@ use std::str::FromStr; use anyhow::Context; use actix_web::{web, App, HttpServer, HttpResponse, Result}; -use diesel::pg::PgConnection; -use diesel::prelude::*; -use diesel::r2d2::ConnectionManager; -use diesel::r2d2::Pool; +mod db; mod model; mod schema; use crate::model::*; +use crate::db::DbPool; embed_migrations!("migrations"); -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)) -} - async fn create_person(db: web::Data<DbPool>, person: web::Json<Person>) -> Result<HttpResponse> { log::debug!("Creating person = {:?}", person); Ok(HttpResponse::Ok().finish()) @@ -54,7 +33,7 @@ async fn main() -> anyhow::Result<()> { .expect("environment: PORT variable not set") .context("Failed to parse port as integer")?; - let db_connection_pool = establish_connection(); + let db_connection_pool = crate::db::establish_connection(); { let conn = db_connection_pool.get().expect("Failed to get connection from pool"); |