Added config file for db #9
18
src/lib.rs
18
src/lib.rs
@@ -2,17 +2,31 @@ pub mod callers;
|
|||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod models;
|
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 {
|
pub mod db_pool {
|
||||||
|
|
||||||
use sqlx::postgres::PgPoolOptions;
|
use sqlx::postgres::PgPoolOptions;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
|
use crate::{connection_settings, keys};
|
||||||
|
|
||||||
pub async fn create_pool() -> Result<sqlx::PgPool, sqlx::Error> {
|
pub async fn create_pool() -> Result<sqlx::PgPool, sqlx::Error> {
|
||||||
dotenv::dotenv().ok();
|
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()
|
PgPoolOptions::new()
|
||||||
.max_connections(10)
|
.max_connections(connection_settings::MAXCONN)
|
||||||
.connect(&database_url)
|
.connect(&database_url)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user