diff --git a/packages/backend/Cargo.toml b/packages/backend/Cargo.toml index 1d2e91c2e8..442febff54 100644 --- a/packages/backend/Cargo.toml +++ b/packages/backend/Cargo.toml @@ -23,5 +23,9 @@ queue = { path = "crates/queue" } config = { path = "crates/config" } macros = { path = "crates/macros" } 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] diff --git a/packages/backend/crates/config/Cargo.toml b/packages/backend/crates/config/Cargo.toml index 0058bdbf2e..9214c74976 100644 --- a/packages/backend/crates/config/Cargo.toml +++ b/packages/backend/crates/config/Cargo.toml @@ -9,4 +9,5 @@ edition = "2021" once_cell = "1.17.1" serde = { version = "1.0.160", features = [ "derive" ] } serde_yaml = "0.9.21" +thiserror = "1.0.40" diff --git a/packages/backend/crates/config/src/lib.rs b/packages/backend/crates/config/src/lib.rs index e825137f47..ba1c3ec461 100644 --- a/packages/backend/crates/config/src/lib.rs +++ b/packages/backend/crates/config/src/lib.rs @@ -1,4 +1,3 @@ -use std::fmt::Display; use std::fs::File; use std::io::{self, Read}; use std::path::Path; @@ -11,44 +10,16 @@ mod data; pub use data::*; // Config Errors -#[derive(Debug)] +#[derive(thiserror::Error, Debug)] pub enum Error { + #[error("The configuration has not been initialized yet")] Uninitialized, - Deserialize(serde_yaml::Error), - FileError(io::Error), + #[error("Error when parsing config file: {0}")] + 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 fn fetch_config(path: &Path) -> Result<Config, Error> { let mut buf = String::new(); @@ -153,7 +124,7 @@ redis: max_note_length: MaxNoteLength(3000), max_caption_length: MaxCommentLength(1500), cluster_limit: None, - env: Environment { }, + env: Environment {}, } ); } diff --git a/packages/backend/src/main.rs b/packages/backend/src/main.rs index 760391f136..c9a2cc95d0 100644 --- a/packages/backend/src/main.rs +++ b/packages/backend/src/main.rs @@ -1,12 +1,14 @@ use std::{ - env, error, fmt, + env, fmt, path::{Path, PathBuf}, }; +use tracing::debug; #[macro_use] extern crate macros; -fn main() -> Result<(), Box<dyn error::Error>> { +#[tokio::main] +async fn main() -> anyhow::Result<()> { env::set_var( "CK_REPO_DIR", PathBuf::from(env!("PWD")) @@ -14,17 +16,33 @@ fn main() -> Result<(), Box<dyn error::Error>> { .and_then(|p| p.parent()) .ok_or(fmt::Error)?, ); + + // logging + let is_debug = match env::var_os("NODE_ENV") { + None => true, + Some(val) => val != "production", + }; + let subscriber = tracing_subscriber::fmt(); + if is_debug { + subscriber + .with_max_level(tracing::Level::DEBUG) + .pretty() + .init(); + } else { + subscriber.with_max_level(tracing::Level::INFO).init(); + } + // bootstrap // ENV // get config - config::init_config( - &Path::new(&env::var("CK_REPO_DIR")?) - .join(".config") - .join("default.yml"), - )?; + let config_path = &Path::new(&env::var("CK_REPO_DIR")?) + .join(".config") + .join("default.yml"); + debug!(target: "config", path = ?config_path, "Loading yaml file"); + config::init_config(config_path)?; eprintln!("{:?}", config::get_config()?);