From db905214349fac45825ca72d5850f0d71da76b4f Mon Sep 17 00:00:00 2001 From: zleyyij <75810274+zleyyij@users.noreply.github.com> Date: Mon, 11 Nov 2024 09:30:41 -0700 Subject: [PATCH 1/3] fix(config): there was a lil switcheroo --- README.md | 3 ++- default.toml | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f22402ad..0a8d3010 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,8 @@ If you're running hyde via local development, place `hyde-data` folder inside `b Hyde expects: -- `hyde-data/default.toml` file with all the configuration options. You can find `default.toml` as an example configuration to use in the root of the project. +- `hyde-data/config.toml` file with all configuration options. `./default.toml` is the example config file, copy it to `hyde-data/config.toml`. +Hyde will accept any `.toml` file in that directory, but `config.toml` is a good choice. - `hyde-data/key.pem`, the private key linked to your Github Application deployment. - `hyde-data/data.db` - If not created, this file will be automatically created and stores permissions and user account info. diff --git a/default.toml b/default.toml index d0773a6c..acb1af39 100644 --- a/default.toml +++ b/default.toml @@ -1,9 +1,9 @@ # Files is related to any URL or internal files Hyde will use [files] # The location of the markdown files relative to the root of the repo -asset_path = "docs/" +docs_path = "docs/" # The location of the assets files relative to the root of the repo -docs_path = "assets/" +asset_path = "assets/" # The path where the repository will be pulled and used repo_path = "repo/" # The URL of the jekyll repository to interface with @@ -11,7 +11,7 @@ repo_url = "https://github.com/r-Techsupport/rTS_Wiki.git" # Discord is related to discord specific information to pass to Hyde. [discord] -# The discord username of the admin account +# The Discord username of the admin account admin_username = "username" # OAuth for Discord and GitHub, handles passing all relevant information to clients in Hyde @@ -27,7 +27,7 @@ token_url = "https://discord.com/api/oauth2/token" [oauth.github] # Github Application Client ID -client_id = "Iv23libASQn9266tzjpm" +client_id = "aBc123DEf456" # Database for anything database related Hyde will utilise. [database] From ded3080687100500f858f3338aaed12adbda0535 Mon Sep 17 00:00:00 2001 From: zleyyij <75810274+zleyyij@users.noreply.github.com> Date: Mon, 11 Nov 2024 12:53:21 -0700 Subject: [PATCH 2/3] feat(backend): make config loading more flexible --- backend/src/app_conf.rs | 51 +++++++++++++++++++++++++++++------------ backend/src/main.rs | 4 ++-- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/backend/src/app_conf.rs b/backend/src/app_conf.rs index c88bad86..97fad686 100644 --- a/backend/src/app_conf.rs +++ b/backend/src/app_conf.rs @@ -1,8 +1,12 @@ +use color_eyre::eyre::ContextCompat; use serde::Deserialize; -use std::fs; -use std::process::exit; +use std::ffi::OsStr; +use std::path::PathBuf; +use std::{fs, path::Path}; +use std::fmt::Debug; use std::sync::Arc; -use tracing::{error, info, trace}; +use tracing::{info, trace}; +use color_eyre::Result; #[derive(Deserialize, Debug, Clone, Default, PartialEq, Eq)] pub struct AppConf { @@ -98,21 +102,38 @@ impl ValidateFields for AppConf { } } impl AppConf { - pub fn load(file: &str) -> Arc { - info!("Loading configuration file: {:?}", file); - - if fs::metadata(file).is_err() { - error!("Configuration file {:?} does not exist", file); - exit(1) - } - - let config: Self = - toml::from_str(&fs::read_to_string(file).unwrap()).expect("Unable to parse config"); - + /// Deserializes the config located at `path`. + /// + /// If a file is passed, it will load that file. If a directory is passed, + /// then it'll search that directory for any `.toml` file. + pub fn load + Copy + Debug>(path: P) -> Result> { + let file_metadata = fs::metadata(path)?; + let config_path: PathBuf = if file_metadata.is_file() { + path.as_ref().to_path_buf() + } else { + locate_config_file(path)?.wrap_err_with(|| format!("No config was found in the {path:?} directory"))? + }; + let serialized_config = fs::read_to_string(config_path)?; + let config: Self = toml::from_str(&serialized_config)?; trace!("Loaded config: {:#?}", config); config.validate("config").expect("Invalid config"); - Arc::new(config) + Ok(Arc::new(config)) + } +} + +/// Returns the first toml config file in the provided directory, relative to the executable. +fn locate_config_file + Copy + Debug>(path: P) -> Result> { + info!("Searching directory {path:?} for a config file"); + // Search the directory for a toml file + let dir = fs::read_dir(path).expect("Failed to read path"); + for entry in dir { + let entry = entry?; + if entry.path().extension() == Some(OsStr::new("toml")) { + info!("Using config at {:?}", entry.path()); + return Ok(Some(entry.path())); + } } + Ok(None) } diff --git a/backend/src/main.rs b/backend/src/main.rs index ed196ae5..70dcf735 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -70,7 +70,7 @@ struct Args { short = 'c', long = "config", help = "Pass your own .toml config file to Hyde.", - default_value_t = String::from("hyde-data/default.toml"), + default_value_t = String::from("hyde-data/"), )] cfg: String, } @@ -131,7 +131,7 @@ async fn main() -> Result<()> { /// Initialize an instance of [`AppState`] #[tracing::instrument] async fn init_state(cli_args: &Args) -> Result { - let config: Arc = AppConf::load(&cli_args.cfg); + let config: Arc = AppConf::load(&cli_args.cfg)?; let repo_url = config.files.repo_url.clone(); let repo_path = config.files.repo_path.clone(); From 0218b0cdcac78a4f4897b2f1d13fadf7252d38bc Mon Sep 17 00:00:00 2001 From: zleyyij <75810274+zleyyij@users.noreply.github.com> Date: Mon, 11 Nov 2024 12:54:58 -0700 Subject: [PATCH 3/3] refactor(backend): config uses Result instead of .expect --- backend/src/app_conf.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/app_conf.rs b/backend/src/app_conf.rs index 97fad686..a13061b3 100644 --- a/backend/src/app_conf.rs +++ b/backend/src/app_conf.rs @@ -127,7 +127,7 @@ impl AppConf { fn locate_config_file + Copy + Debug>(path: P) -> Result> { info!("Searching directory {path:?} for a config file"); // Search the directory for a toml file - let dir = fs::read_dir(path).expect("Failed to read path"); + let dir = fs::read_dir(path)?; for entry in dir { let entry = entry?; if entry.path().extension() == Some(OsStr::new("toml")) {