mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-03-26 07:30:44 +00:00
28 lines
616 B
Rust
28 lines
616 B
Rust
|
use std::error::Error;
|
||
|
use std::sync::{Mutex, Arc};
|
||
|
use crate::data::config::DatabaseConfig;
|
||
|
use mysql::Pool;
|
||
|
|
||
|
/// Database access helper
|
||
|
///
|
||
|
/// @author Pierre Hubert
|
||
|
|
||
|
// Pool shared across threads
|
||
|
static mut POOL: Option<Arc<Mutex<mysql::Pool>>> = None;
|
||
|
|
||
|
/// Connect to the database
|
||
|
pub fn connect(conf: &DatabaseConfig) -> Result<(), Box<dyn Error>> {
|
||
|
let url = format!(
|
||
|
"mysql://{}:{}@{}:3306/{}",
|
||
|
conf.username, conf.password, conf.host, conf.name
|
||
|
);
|
||
|
|
||
|
let pool = Pool::new(url)?;
|
||
|
let pool = Some(Arc::new(Mutex::new(pool)));
|
||
|
|
||
|
unsafe {
|
||
|
POOL = pool;
|
||
|
}
|
||
|
|
||
|
Ok(())
|
||
|
}
|