Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow Specifying a Config File Path on Execution #81

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 122 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ brotli = "6.0.0"
bytes = "1.6.0"
cbc = { version = "0.1.2", features = ["std"] }
chrono = { version = "0.4.38", features = ["serde"] }
clap = { version = "4.5.18", features = ["derive", "env"] }
cookie = "0.18.1"
hex = "0.4.3"
html-escape = "0.2.13"
Expand Down
41 changes: 30 additions & 11 deletions src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,36 +201,55 @@ fn default_max_compressed_body_size() -> usize {
3000000
}

fn read_config() -> Result<StaticConfiguration, String> {
let toml_exists = Path::new("edgee.toml").exists();
let yaml_exists = Path::new("edgee.yaml").exists();
fn read_config(path: Option<&Path>) -> Result<StaticConfiguration, String> {
let toml_path = Path::new("edgee.toml");
let yaml_path = Path::new("edgee.yaml");

if let Some(path) = path {
let extension = path
.extension()
.and_then(|extension| extension.to_str())
.expect("provided configuration file does not have a format extension or is invalid");

let config_data =
std::fs::read_to_string(path).expect("should read provided configuration file");

match extension {
"toml" => {
return toml::from_str(&config_data)
.map_err(|e| format!("should parse config file: {e}"))
}
"yml" | "yaml" => {
return serde_yml::from_str(&config_data)
.map_err(|e| format!("should parse config file: {e}"));
}
_ => return Err("provided configuration file has an unknown extension".to_string()),
}
}

match (toml_exists, yaml_exists) {
match (toml_path.exists(), yaml_path.exists()) {
(true, true) => {
Err("both edgee.toml and edgee.yaml exist but only one is expected.".into())
}
(false, false) => {
Err("no configuration file found, either edgee.toml or edgee.yaml is required.".into())
}
(true, false) => {
let config_file =
std::fs::read_to_string("edgee.toml").expect("should read edgee.toml");
let config_file = std::fs::read_to_string(toml_path).expect("should read edgee.toml");
toml::from_str(&config_file).map_err(|e| format!("should parse config file: {}", e))
}
(false, true) => {
let config_file =
std::fs::read_to_string("edgee.yaml").expect("should read edgee.yaml");
let config_file = std::fs::read_to_string(yaml_path).expect("should read edgee.yaml");
serde_yml::from_str(&config_file)
.map_err(|e| format!("should parse config file: {}", e))
}
}
}

// TODO: Read config from CLI arguments
// TODO: Add more configuration validations
// TODO: Improve error messages for configuration errors
pub fn init() {
let config = read_config().expect("should read config file");
pub fn init(path: Option<&Path>) {
let config = read_config(path).expect("should read config file");
config.validate().unwrap();

CONFIG.set(config).expect("Should initialize config");
Expand Down
14 changes: 13 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::path::PathBuf;

use clap::Parser;
use tracing::error;

mod config;
Expand All @@ -7,9 +10,18 @@ mod proxy;
mod server;
mod tools;

#[derive(Debug, Parser)]
#[command(about, author, version)]
struct Options {
#[arg(short = 'f', long = "config", env = "EDGEE_CONFIG_PATH")]
config_path: Option<PathBuf>,
}

#[tokio::main]
async fn main() {
config::config::init();
let options = Options::parse();

config::config::init(options.config_path.as_deref());
logger::logger::init();
proxy::compute::data_collection::components::init();

Expand Down