feat: load settings in application context
This commit is contained in:
parent
d5211cf946
commit
009ee5d11e
1 changed files with 11 additions and 6 deletions
17
src/data.rs
17
src/data.rs
|
@ -22,13 +22,14 @@ use argon2_creds::{Config, ConfigBuilder, PasswordPolicy};
|
||||||
use sqlx::postgres::PgPoolOptions;
|
use sqlx::postgres::PgPoolOptions;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
|
|
||||||
use crate::SETTINGS;
|
use crate::settings::Settings;
|
||||||
|
|
||||||
/// App data
|
/// App data
|
||||||
pub struct Data {
|
pub struct Data {
|
||||||
/// databse pool
|
/// database pool
|
||||||
pub db: PgPool,
|
pub db: PgPool,
|
||||||
pub creds: Config,
|
pub creds: Config,
|
||||||
|
pub settings: Settings,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Data {
|
impl Data {
|
||||||
|
@ -44,7 +45,7 @@ impl Data {
|
||||||
|
|
||||||
#[cfg(not(tarpaulin_include))]
|
#[cfg(not(tarpaulin_include))]
|
||||||
/// create new instance of app data
|
/// create new instance of app data
|
||||||
pub async fn new() -> Arc<Self> {
|
pub async fn new(settings: Settings) -> Arc<Self> {
|
||||||
let creds = Self::get_creds();
|
let creds = Self::get_creds();
|
||||||
let c = creds.clone();
|
let c = creds.clone();
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
|
@ -55,14 +56,18 @@ impl Data {
|
||||||
});
|
});
|
||||||
|
|
||||||
let db = PgPoolOptions::new()
|
let db = PgPoolOptions::new()
|
||||||
.max_connections(SETTINGS.database.pool)
|
.max_connections(settings.database.pool)
|
||||||
.connect(&SETTINGS.database.url)
|
.connect(&settings.database.url)
|
||||||
.await
|
.await
|
||||||
.expect("Unable to form database pool");
|
.expect("Unable to form database pool");
|
||||||
|
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(not(debug_assertions))]
|
||||||
init.join().unwrap();
|
init.join().unwrap();
|
||||||
let data = Data { db, creds };
|
let data = Data {
|
||||||
|
db,
|
||||||
|
creds,
|
||||||
|
settings,
|
||||||
|
};
|
||||||
|
|
||||||
Arc::new(data)
|
Arc::new(data)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue