Merge pull request '[rust] refactor: config' (#10109) from nmkj/calckey:refactor/rustify into refactor/rocket
Reviewed-on: https://codeberg.org/calckey/calckey/pulls/10109
This commit is contained in:
commit
bdc391caa7
5 changed files with 83 additions and 51 deletions
|
@ -23,5 +23,9 @@ queue = { path = "crates/queue" }
|
||||||
config = { path = "crates/config" }
|
config = { path = "crates/config" }
|
||||||
macros = { path = "crates/macros" }
|
macros = { path = "crates/macros" }
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
|
tracing = "0.1.37"
|
||||||
|
tracing-subscriber = "0.3.17"
|
||||||
|
tokio = { version = "1.28.1", features = ["full"] }
|
||||||
|
anyhow = "1.0.71"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|
|
@ -9,4 +9,6 @@ edition = "2021"
|
||||||
once_cell = "1.17.1"
|
once_cell = "1.17.1"
|
||||||
serde = { version = "1.0.160", features = [ "derive" ] }
|
serde = { version = "1.0.160", features = [ "derive" ] }
|
||||||
serde_yaml = "0.9.21"
|
serde_yaml = "0.9.21"
|
||||||
|
thiserror = "1.0.40"
|
||||||
|
url = "2.3.1"
|
||||||
|
|
||||||
|
|
|
@ -112,13 +112,20 @@ pub struct Config {
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub max_caption_length: MaxCommentLength,
|
pub max_caption_length: MaxCommentLength,
|
||||||
// pub disable_hsts: bool,
|
// pub disable_hsts: bool,
|
||||||
pub cluster_limit: Option<u16>,
|
#[serde(default = "cluster_limit_default")]
|
||||||
// pub deliver_job_concurrency: u16,
|
pub cluster_limit: u16,
|
||||||
// pub inbox_job_concurrency: u16,
|
#[serde(default = "deliver_job_default")]
|
||||||
// pub deliver_job_per_sec: u16,
|
pub deliver_job_concurrency: u16,
|
||||||
// pub inbox_job_per_sec: u16,
|
#[serde(default = "inbox_job_default")]
|
||||||
// pub deliver_job_max_attempts: u16,
|
pub inbox_job_concurrency: u16,
|
||||||
// pub inbox_job_max_attempts: u16,
|
#[serde(default = "deliver_job_default")]
|
||||||
|
pub deliver_job_per_sec: u16,
|
||||||
|
#[serde(default = "inbox_job_default")]
|
||||||
|
pub inbox_job_per_sec: u16,
|
||||||
|
#[serde(default = "deliver_job_attempts_default")]
|
||||||
|
pub deliver_job_max_attempts: u16,
|
||||||
|
#[serde(default = "inbox_job_attempts_default")]
|
||||||
|
pub inbox_job_max_attempts: u16,
|
||||||
// pub outgoing_address_family: IpFamily,
|
// pub outgoing_address_family: IpFamily,
|
||||||
// pub syslog: syslog::SyslogConfig,
|
// pub syslog: syslog::SyslogConfig,
|
||||||
// pub proxy: Option<Host>,
|
// pub proxy: Option<Host>,
|
||||||
|
@ -182,6 +189,8 @@ pub mod db {
|
||||||
|
|
||||||
/// redis config
|
/// redis config
|
||||||
pub mod redis {
|
pub mod redis {
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Deserialize)]
|
#[derive(Debug, PartialEq, Deserialize)]
|
||||||
|
@ -196,6 +205,13 @@ pub mod redis {
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub db: u8,
|
pub db: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<&RedisConfig> for Url {
|
||||||
|
fn from(value: &RedisConfig) -> Self {
|
||||||
|
Url::parse(&format!("redis://{}:{}", value.host.1, value.port))
|
||||||
|
.expect("Invalid redis host and port")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// sonic search config
|
/// sonic search config
|
||||||
|
@ -270,6 +286,26 @@ impl Default for MaxCommentLength {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn cluster_limit_default() -> u16 {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deliver_job_default() -> u16 {
|
||||||
|
128
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deliver_job_attempts_default() -> u16 {
|
||||||
|
12
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inbox_job_default() -> u16 {
|
||||||
|
16
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inbox_job_attempts_default() -> u16 {
|
||||||
|
8
|
||||||
|
}
|
||||||
|
|
||||||
fn true_fn() -> bool {
|
fn true_fn() -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use std::fmt::Display;
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, Read};
|
use std::io::{self, Read};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
@ -11,44 +10,16 @@ mod data;
|
||||||
pub use data::*;
|
pub use data::*;
|
||||||
|
|
||||||
// Config Errors
|
// Config Errors
|
||||||
#[derive(Debug)]
|
#[derive(thiserror::Error, Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
#[error("The configuration has not been initialized yet")]
|
||||||
Uninitialized,
|
Uninitialized,
|
||||||
Deserialize(serde_yaml::Error),
|
#[error("Error when parsing config file: {0}")]
|
||||||
FileError(io::Error),
|
Deserialize(#[from] serde_yaml::Error),
|
||||||
|
#[error("Error when reading config file: {0}")]
|
||||||
|
FileError(#[from] io::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! generate_error_impl {
|
|
||||||
($t:ident, $o:ty) => {
|
|
||||||
impl From<$o> for Error {
|
|
||||||
fn from(value: $o) -> Self {
|
|
||||||
Self::$t(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for Error {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
use Error::*;
|
|
||||||
|
|
||||||
f.write_str(&{
|
|
||||||
match self {
|
|
||||||
Uninitialized => {
|
|
||||||
format!("The configuration has not been initialized yet: {:?}", self)
|
|
||||||
}
|
|
||||||
Deserialize(e) => format!("Error when parsing config file: {}", e),
|
|
||||||
FileError(e) => format!("Error when reading config file: {}", e),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
generate_error_impl!(FileError, io::Error);
|
|
||||||
generate_error_impl!(Deserialize, serde_yaml::Error);
|
|
||||||
|
|
||||||
impl std::error::Error for Error {}
|
|
||||||
|
|
||||||
// Functions
|
// Functions
|
||||||
fn fetch_config(path: &Path) -> Result<Config, Error> {
|
fn fetch_config(path: &Path) -> Result<Config, Error> {
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
|
@ -152,8 +123,14 @@ redis:
|
||||||
},
|
},
|
||||||
max_note_length: MaxNoteLength(3000),
|
max_note_length: MaxNoteLength(3000),
|
||||||
max_caption_length: MaxCommentLength(1500),
|
max_caption_length: MaxCommentLength(1500),
|
||||||
cluster_limit: None,
|
cluster_limit: 1,
|
||||||
env: Environment { },
|
env: Environment {},
|
||||||
|
deliver_job_concurrency: 128,
|
||||||
|
inbox_job_concurrency: 16,
|
||||||
|
deliver_job_per_sec: 128,
|
||||||
|
inbox_job_per_sec: 16,
|
||||||
|
deliver_job_max_attempts: 12,
|
||||||
|
inbox_job_max_attempts: 8,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
use std::{
|
use std::{
|
||||||
env, error, fmt,
|
env, fmt,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
use tracing::debug;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate macros;
|
extern crate macros;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn error::Error>> {
|
fn main() -> anyhow::Result<()> {
|
||||||
env::set_var(
|
env::set_var(
|
||||||
"CK_REPO_DIR",
|
"CK_REPO_DIR",
|
||||||
PathBuf::from(env!("PWD"))
|
PathBuf::from(env!("PWD"))
|
||||||
|
@ -14,17 +15,29 @@ fn main() -> Result<(), Box<dyn error::Error>> {
|
||||||
.and_then(|p| p.parent())
|
.and_then(|p| p.parent())
|
||||||
.ok_or(fmt::Error)?,
|
.ok_or(fmt::Error)?,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// logging
|
||||||
|
let subscriber = tracing_subscriber::fmt();
|
||||||
|
if is_release!() {
|
||||||
|
subscriber.with_max_level(tracing::Level::INFO).init();
|
||||||
|
} else {
|
||||||
|
subscriber
|
||||||
|
.with_max_level(tracing::Level::DEBUG)
|
||||||
|
.pretty()
|
||||||
|
.init();
|
||||||
|
}
|
||||||
|
|
||||||
// bootstrap
|
// bootstrap
|
||||||
|
|
||||||
// ENV
|
// ENV
|
||||||
|
|
||||||
// get config
|
// get config
|
||||||
|
|
||||||
config::init_config(
|
let config_path = &Path::new(&env::var("CK_REPO_DIR")?)
|
||||||
&Path::new(&env::var("CK_REPO_DIR")?)
|
.join(".config")
|
||||||
.join(".config")
|
.join("default.yml");
|
||||||
.join("default.yml"),
|
debug!(target: "config", path = ?config_path, "Loading yaml file");
|
||||||
)?;
|
config::init_config(config_path)?;
|
||||||
|
|
||||||
eprintln!("{:?}", config::get_config()?);
|
eprintln!("{:?}", config::get_config()?);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue