db pooling settings
This commit is contained in:
18
src/lib.rs
18
src/lib.rs
@@ -2,17 +2,31 @@ pub mod callers;
|
||||
pub mod config;
|
||||
pub mod models;
|
||||
|
||||
mod keys {
|
||||
pub const DBURL: &str = "DATABASE_URL";
|
||||
|
||||
pub mod error {
|
||||
pub const ERROR: &str = "DATABASE_URL must be set in .env";
|
||||
}
|
||||
}
|
||||
|
||||
mod connection_settings {
|
||||
pub const MAXCONN: u32 = 5;
|
||||
}
|
||||
|
||||
pub mod db_pool {
|
||||
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
use std::env;
|
||||
|
||||
use crate::{connection_settings, keys};
|
||||
|
||||
pub async fn create_pool() -> Result<sqlx::PgPool, sqlx::Error> {
|
||||
dotenv::dotenv().ok();
|
||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set in .env");
|
||||
let database_url = env::var(keys::DBURL).expect(keys::error::ERROR);
|
||||
|
||||
PgPoolOptions::new()
|
||||
.max_connections(10)
|
||||
.max_connections(connection_settings::MAXCONN)
|
||||
.connect(&database_url)
|
||||
.await
|
||||
}
|
||||
|
Reference in New Issue
Block a user