rust_forum/src/lib.rs

81 lines
2.7 KiB
Rust
Raw Normal View History

2024-11-26 22:30:25 +03:00
pub mod database;
pub mod error;
2024-12-02 23:56:43 +03:00
pub mod feature;
2024-12-18 04:53:14 +03:00
pub mod mail;
2024-12-13 21:55:04 +03:00
pub mod routing;
pub mod server;
2024-11-29 20:31:02 +03:00
pub mod utils;
use std::sync::LazyLock;
use feature::auth::OneTimePassword;
2024-12-03 21:33:42 +03:00
use sqlx::{Pool, Postgres};
use tokio::sync::RwLock;
2024-11-29 20:31:02 +03:00
use utils::naive_toml_parser;
pub static SERVER_CONFIG: LazyLock<ServerConfig> = LazyLock::new(ServerConfig::default);
pub static ONE_TIME_PASSWORDS: LazyLock<RwLock<Vec<OneTimePassword>>> =
LazyLock::new(OneTimePassword::init);
2024-11-29 20:31:02 +03:00
const DATABASE_CONFIG_FILE_LOCATION: &str = "./configs/database_config.toml";
const SERVER_CONFIG_FILE_LOCATION: &str = "./configs/server_config.toml";
2024-11-26 22:30:25 +03:00
#[derive(Debug)]
2024-11-26 22:30:25 +03:00
pub struct DatabaseConfig {
pub address: String,
pub username: String,
pub password: String,
pub database: String,
2024-12-02 23:56:43 +03:00
pub backend: String,
2024-12-03 21:33:42 +03:00
pub connection_pool_size: u32,
2024-11-26 22:30:25 +03:00
}
impl Default for DatabaseConfig {
fn default() -> Self {
2024-11-29 20:31:02 +03:00
let (header, mut database_configs) = naive_toml_parser(DATABASE_CONFIG_FILE_LOCATION);
2024-11-29 20:31:02 +03:00
if header == "[database_config]" {
2024-11-26 22:30:25 +03:00
Self {
2024-12-18 04:53:14 +03:00
address: database_configs.pop_front().unwrap().parse().unwrap(),
username: database_configs.pop_front().unwrap().parse().unwrap(),
password: database_configs.pop_front().unwrap().parse().unwrap(),
database: database_configs.pop_front().unwrap().parse().unwrap(),
backend: database_configs.pop_front().unwrap().parse().unwrap(),
connection_pool_size: database_configs.pop_front().unwrap().parse().unwrap(),
2024-11-26 22:30:25 +03:00
}
} else {
panic!("Database Config File Must Include [database_config] at the First Line")
}
}
}
#[derive(Debug)]
2024-11-26 22:30:25 +03:00
pub struct ServerConfig {
pub address: String,
pub otp_time_limit: usize,
pub login_token_time_limit: usize,
pub concurrency_limit: usize,
2024-11-26 22:30:25 +03:00
}
impl Default for ServerConfig {
fn default() -> Self {
2024-11-29 20:31:02 +03:00
let (header, mut server_configs) = naive_toml_parser(SERVER_CONFIG_FILE_LOCATION);
let value_or_max = |value: String| value.parse().map_or(usize::MAX, |value| value);
2024-11-29 20:31:02 +03:00
if header == "[server_config]" {
2024-11-26 22:30:25 +03:00
Self {
2024-12-18 04:53:14 +03:00
address: server_configs.pop_front().unwrap().parse().unwrap(),
otp_time_limit: value_or_max(server_configs.pop_front().unwrap()),
login_token_time_limit: value_or_max(server_configs.pop_front().unwrap()),
concurrency_limit: value_or_max(server_configs.pop_front().unwrap()),
2024-11-26 22:30:25 +03:00
}
} else {
panic!("Server Config File Must Include [server_config] at the First Line")
}
}
}
2024-12-02 23:56:43 +03:00
#[derive(Debug, Clone)]
pub struct AppState {
2024-12-03 21:33:42 +03:00
pub database_connection: Pool<Postgres>,
2024-12-02 23:56:43 +03:00
}