Migrated project into separate directories

This commit is contained in:
2023-09-07 21:05:32 -04:00
parent 359c6f0ac5
commit 17d7407f1b
52 changed files with 88 additions and 8987 deletions

37
weather-service/src/db.rs Normal file
View File

@@ -0,0 +1,37 @@
use crate::error_handler::CustomError;
use diesel::pg::PgConnection;
use diesel::r2d2::ConnectionManager;
use lazy_static::lazy_static;
use log::{error, info};
use r2d2;
use std::env;
type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
pub type DbConnection = r2d2::PooledConnection<ConnectionManager<PgConnection>>;
diesel_migrations::embed_migrations!();
lazy_static! {
static ref POOL: Pool = {
let username = env::var("DATABASE_USER").expect("Database username is not set");
let password = env::var("DATABASE_PASSWORD").expect("Database password is not set");
let name = env::var("DATABASE_NAME").expect("Database name is not set");
let url = format!("postgres://{}:{}@localhost:5433/{}", username, password, name);
let manager = ConnectionManager::<PgConnection>::new(url);
Pool::new(manager).expect("Failed to create db pool")
};
}
pub fn init() {
lazy_static::initialize(&POOL);
let conn = connection().expect("Failed to get db connection");
match embedded_migrations::run(&conn) {
Ok(_) => info!("Database initialized"),
Err(err) => error!("Failed to initialize database; {}", err),
};
}
pub fn connection() -> Result<DbConnection, CustomError> {
POOL.get()
.map_err(|e| CustomError::new(500, format!("Failed getting db connection: {}", e)))
}