Add rate limiting
This commit is contained in:
25
geneit_backend/src/connections/db_connection.rs
Normal file
25
geneit_backend/src/connections/db_connection.rs
Normal file
@ -0,0 +1,25 @@
|
||||
//! # Database connection management
|
||||
|
||||
use crate::app_config::AppConfig;
|
||||
use diesel::{Connection, PgConnection};
|
||||
use std::cell::RefCell;
|
||||
thread_local! {
|
||||
static POSTGRES_CONNECTION: RefCell<Option<PgConnection>> = RefCell::new(None);
|
||||
}
|
||||
|
||||
/// Execute a request on the database
|
||||
pub fn execute<E, I>(cb: E) -> anyhow::Result<I>
|
||||
where
|
||||
E: FnOnce(&mut PgConnection) -> anyhow::Result<I>,
|
||||
{
|
||||
// Establish connection if required
|
||||
if POSTGRES_CONNECTION.with(|i| i.borrow().is_none()) {
|
||||
let database_url = AppConfig::get().db_connection_chain();
|
||||
let conn = PgConnection::establish(&database_url)
|
||||
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url));
|
||||
|
||||
POSTGRES_CONNECTION.with(|i| *i.borrow_mut() = Some(conn))
|
||||
}
|
||||
|
||||
POSTGRES_CONNECTION.with(|i| cb(i.borrow_mut().as_mut().unwrap()))
|
||||
}
|
Reference in New Issue
Block a user