Skip to content

Commit

Permalink
actually validate
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsnaps committed Sep 1, 2022
1 parent becf124 commit 7b8291b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion limitador-server/examples/limits.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
max_value: 5
seconds: 60
conditions:
- "req.method == 'POST'"
- "req.method ~= 'POST'"
variables:
- user_id
2 changes: 2 additions & 0 deletions limitador-server/src/http_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
#[allow(clippy::field_reassign_with_default)]
mod request_types;

pub use request_types::Limit as LimitVO;

pub mod server;
31 changes: 23 additions & 8 deletions limitador-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,29 +560,44 @@ fn create_config() -> (Configuration, String) {
let limits_file = matches.value_of("LIMITS_FILE").unwrap();

if matches.is_present("validate") {
let result = match std::fs::File::open(limits_file) {
let error = match std::fs::File::open(limits_file) {
Ok(f) => {
let parsed_limits: Result<Vec<Limit>, _> = serde_yaml::from_reader(f);
match parsed_limits {
Ok(limits) => {
Ok(())
if limitador::limit::check_deprecated_syntax_usages_and_reset() {
eprintln!("Deprecated syntax for conditions corrected!\n")
}

let output: Vec<http_api::LimitVO> =
limits.iter().map(|l| l.into()).collect();
match serde_yaml::to_string(&output) {
Ok(cfg) => {
println!("{}", cfg);
}
Err(err) => {
eprintln!("Config file is valid, but can't be output: {}", err);
}
}
process::exit(0);
}
Err(e) => Err(LimitadorServerError::ConfigFile(format!(
Err(e) => LimitadorServerError::ConfigFile(format!(
"Couldn't parse: {}",
e
))),
)),
}
}
Err(e) => Err(LimitadorServerError::ConfigFile(format!(
Err(e) => LimitadorServerError::ConfigFile(format!(
"Couldn't read file '{}': {}",
limits_file,
e
))),
)),
};
println!("{:?}", result);
process::exit(0);
eprintln!("{}", error);
process::exit(1);
}


let storage = match matches.subcommand() {
Some(("redis", sub)) => StorageConfiguration::Redis(RedisStorageConfiguration {
url: sub.value_of("URL").unwrap().to_owned(),
Expand Down

0 comments on commit 7b8291b

Please sign in to comment.