librepages/src/ctx/mod.rs

81 lines
2.2 KiB
Rust
Raw Normal View History

/*
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::sync::Arc;
2022-09-16 13:23:06 +05:30
use std::thread;
2022-09-10 19:21:49 +05:30
use crate::db::*;
use crate::settings::Settings;
2022-09-16 13:23:06 +05:30
use argon2_creds::{Config as ArgonConfig, ConfigBuilder as ArgonConfigBuilder, PasswordPolicy};
2022-12-19 00:26:17 +05:30
use reqwest::Client;
2022-11-11 14:56:36 +05:30
use tracing::info;
2022-09-16 13:23:06 +05:30
pub mod api;
2022-12-19 00:26:17 +05:30
pub mod gitea;
2022-12-15 01:00:15 +05:30
use crate::conductor::Conductor;
2022-09-10 19:21:49 +05:30
pub type ArcCtx = Arc<Ctx>;
#[derive(Clone)]
pub struct Ctx {
pub settings: Settings,
2022-09-10 19:21:49 +05:30
pub db: Database,
2022-12-15 01:00:15 +05:30
pub conductor: Conductor,
2022-09-16 13:23:06 +05:30
/// credential-procession policy
pub creds: ArgonConfig,
2022-12-19 00:26:17 +05:30
client: Client,
}
impl Ctx {
2022-09-16 13:23:06 +05:30
/// Get credential-processing policy
pub fn get_creds() -> ArgonConfig {
ArgonConfigBuilder::default()
.username_case_mapped(true)
.profanity(true)
.blacklist(true)
.password_policy(PasswordPolicy::default())
.build()
.unwrap()
}
2022-09-10 19:21:49 +05:30
pub async fn new(settings: Settings) -> Arc<Self> {
2022-09-16 13:23:06 +05:30
let creds = Self::get_creds();
let c = creds.clone();
2022-12-15 01:00:15 +05:30
let conductor = Conductor::new(settings.clone());
2022-09-16 13:23:06 +05:30
#[allow(unused_variables)]
let init = thread::spawn(move || {
2022-11-11 14:56:36 +05:30
info!("Initializing credential manager");
2022-09-16 13:23:06 +05:30
c.init();
2022-11-11 14:56:36 +05:30
info!("Initialized credential manager");
2022-09-16 13:23:06 +05:30
});
2022-09-10 19:21:49 +05:30
let db = get_db(&settings).await;
2022-09-16 13:23:06 +05:30
#[cfg(not(debug_assertions))]
init.join();
2022-12-19 00:26:17 +05:30
let client = Client::new();
2022-09-16 13:23:06 +05:30
Arc::new(Self {
settings,
db,
creds,
2022-12-15 01:00:15 +05:30
conductor,
2022-12-19 00:26:17 +05:30
client,
2022-09-16 13:23:06 +05:30
})
}
}