From 41a47bdf8ff317211861f18713813511a35e0c32 Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Sun, 27 Aug 2023 11:15:20 +0100 Subject: [PATCH] feat: add revolt_config crate --- crates/core/config/Cargo.toml | 26 ++++++ crates/core/config/Revolt.toml | 64 +++++++++++++ crates/core/config/src/lib.rs | 160 +++++++++++++++++++++++++++++++++ 3 files changed, 250 insertions(+) create mode 100644 crates/core/config/Cargo.toml create mode 100644 crates/core/config/Revolt.toml create mode 100644 crates/core/config/src/lib.rs diff --git a/crates/core/config/Cargo.toml b/crates/core/config/Cargo.toml new file mode 100644 index 00000000..8ffe90d7 --- /dev/null +++ b/crates/core/config/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "revolt-config" +version = "0.6.7" +edition = "2021" +license = "AGPL-3.0-or-later" +authors = ["Paul Makles "] +description = "Revolt Backend: Configuration" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[features] +test = ["async-std"] +default = ["test"] + +[dependencies] +# Utility +config = "0.13.3" +cached = "0.44.0" +once_cell = "1.18.0" + +# Serde +serde = { version = "1", features = ["derive"] } + +# Async +futures-locks = "0.7.1" +async-std = { version = "1.8.0", features = ["attributes"], optional = true } diff --git a/crates/core/config/Revolt.toml b/crates/core/config/Revolt.toml new file mode 100644 index 00000000..213f1fd0 --- /dev/null +++ b/crates/core/config/Revolt.toml @@ -0,0 +1,64 @@ +[database] +mongodb = "mongodb://database" +redis = "redis://redis/" + +[hosts] +app = "http://local.revolt.chat" +api = "http://local.revolt.chat/api" +events = "ws://local.revolt.chat/ws" +autumn = "http://local.revolt.chat/autumn" +january = "http://local.revolt.chat/january" +voso_legacy = "" +voso_legacy_ws = "" + +[api] +staging = false + +[api.registration] +invite_only = false + +[api.smtp] +host = "" +username = "" +password = "" +from_address = "" + +[api.vapid] +private_key = "LS0tLS1CRUdJTiBFQyBQUklWQVRFIEtFWS0tLS0tCk1IY0NBUUVFSUJSUWpyTWxLRnBiVWhsUHpUbERvcEliYk1yeVNrNXpKYzVYVzIxSjJDS3hvQW9HQ0NxR1NNNDkKQXdFSG9VUURRZ0FFWnkrQkg2TGJQZ2hEa3pEempXOG0rUXVPM3pCajRXT1phdkR6ZU00c0pqbmFwd1psTFE0WAp1ZDh2TzVodU94QWhMQlU3WWRldVovWHlBdFpWZmNyQi9BPT0KLS0tLS1FTkQgRUMgUFJJVkFURSBLRVktLS0tLQo" +public_key = "BGcvgR-i2z4IQ5Mw841vJvkLjt8wY-FjmWrw83jOLCY52qcGZS0OF7nfLzuYbjsQISwVO2HXrmf18gLWVX3Kwfw=" + +[api.fcm] +api_key = "" + +[api.security] +authifier_shield_key = "" +voso_legacy_token = "" + +[api.security.captcha] +hcaptcha_key = "" +hcaptcha_sitekey = "" + +[api.workers] +max_concurrent_connections = 50 + +[features] + +[features.limits] + +[features.limits.default] +group_size = 100 +bots = 5 +message_replies = 5 +message_attachments = 5 +message_embeds = 5 +servers = 100 +server_emoji = 100 +server_roles = 200 +server_channels = 200 + +attachment_size = 20000000 +avatar_size = 4000000 +background_size = 6000000 +icon_size = 2500000 +banner_size = 6000000 +emoji_size = 500000 diff --git a/crates/core/config/src/lib.rs b/crates/core/config/src/lib.rs new file mode 100644 index 00000000..bc459290 --- /dev/null +++ b/crates/core/config/src/lib.rs @@ -0,0 +1,160 @@ +use std::collections::HashMap; + +use cached::proc_macro::cached; +use config::{Config, File, FileFormat}; +use futures_locks::RwLock; +use once_cell::sync::Lazy; +use serde::Deserialize; + +static CONFIG_BUILDER: Lazy> = Lazy::new(|| { + RwLock::new({ + Config::builder() + .add_source(File::from_str( + include_str!("../Revolt.toml"), + FileFormat::Toml, + )) + .add_source(File::new("revolt.toml", FileFormat::Toml)) + .build() + .unwrap() + }) +}); + +// https://gifbox.me/view/gT5mqxYKCZv-twilight-meow + +#[derive(Deserialize, Debug, Clone)] +pub struct Database { + pub mongodb: String, + pub redis: String, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct Hosts { + pub app: String, + pub api: String, + pub events: String, + pub autumn: String, + pub january: String, + pub voso_legacy: String, + pub voso_legacy_ws: String, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct ApiRegistration { + pub invite_only: bool, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct ApiSmtp { + pub host: String, + pub username: String, + pub password: String, + pub from_address: String, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct ApiVapid { + pub private_key: String, + pub public_key: String, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct ApiFcm { + pub api_key: String, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct ApiSecurityCaptcha { + pub hcaptcha_key: String, + pub hcaptcha_sitekey: String, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct ApiSecurity { + pub authifier_shield_key: String, + pub voso_legacy_token: String, + pub captcha: ApiSecurityCaptcha, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct ApiWorkers { + pub max_concurrent_connections: usize, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct Api { + pub staging: bool, + pub registration: ApiRegistration, + pub smtp: ApiSmtp, + pub vapid: ApiVapid, + pub fcm: ApiFcm, + pub security: ApiSecurity, + pub workers: ApiWorkers, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct FeaturesLimits { + pub group_size: usize, + pub bots: usize, + pub message_replies: usize, + pub message_attachments: usize, + pub message_embeds: usize, + pub servers: usize, + pub server_emoji: usize, + pub server_roles: usize, + pub server_channels: usize, + + pub attachment_size: usize, + pub avatar_size: usize, + pub background_size: usize, + pub icon_size: usize, + pub banner_size: usize, + pub emoji_size: usize, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct FeaturesLimitsCollection { + pub default: FeaturesLimits, + + #[serde(flatten)] + pub roles: HashMap, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct Features { + pub limits: FeaturesLimitsCollection, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct Settings { + pub database: Database, + pub hosts: Hosts, + pub api: Api, + pub features: Features, +} + +pub async fn init() { + println!( + ":: Revolt Configuration ::\n\x1b[32m{:?}\x1b[0m", + config().await + ); +} + +pub async fn read() -> Config { + CONFIG_BUILDER.read().await.clone() +} + +#[cached(time = 30)] +pub async fn config() -> Settings { + read().await.try_deserialize::().unwrap() +} + +#[cfg(feature = "test")] +#[cfg(test)] +mod tests { + use crate::init; + + #[async_std::test] + async fn it_works() { + init().await; + } +}