Skip to content

Commit

Permalink
Merge pull request #277 from r-Techsupport/config-cleanup
Browse files Browse the repository at this point in the history
Config cleanup
  • Loading branch information
zleyyij authored Nov 14, 2024
2 parents 68c069d + 0218b0c commit 54e8847
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
51 changes: 36 additions & 15 deletions backend/src/app_conf.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -98,21 +102,38 @@ impl ValidateFields for AppConf {
}
}
impl AppConf {
pub fn load(file: &str) -> Arc<Self> {
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<P: AsRef<Path> + Copy + Debug>(path: P) -> Result<Arc<Self>> {
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<P: AsRef<Path> + Copy + Debug>(path: P) -> Result<Option<PathBuf>> {
info!("Searching directory {path:?} for a config file");
// Search the directory for a toml file
let dir = fs::read_dir(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)
}
4 changes: 2 additions & 2 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -131,7 +131,7 @@ async fn main() -> Result<()> {
/// Initialize an instance of [`AppState`]
#[tracing::instrument]
async fn init_state(cli_args: &Args) -> Result<AppState> {
let config: Arc<AppConf> = AppConf::load(&cli_args.cfg);
let config: Arc<AppConf> = AppConf::load(&cli_args.cfg)?;

let repo_url = config.files.repo_url.clone();
let repo_path = config.files.repo_path.clone();
Expand Down
8 changes: 4 additions & 4 deletions default.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# 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
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
Expand All @@ -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]
Expand Down

0 comments on commit 54e8847

Please sign in to comment.