From 4eb75a8cb16dc52a0d87da45c94cba3aa8e0e684 Mon Sep 17 00:00:00 2001 From: neon_arch Date: Sat, 2 Sep 2023 20:19:43 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20add=20code=20to=20parse=20t?= =?UTF-8?q?he=20new=20config=20option=20(#203)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/parser.rs | 20 ++++++++------------ src/config/parser_models.rs | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/config/parser.rs b/src/config/parser.rs index 4639013..dbebfd0 100644 --- a/src/config/parser.rs +++ b/src/config/parser.rs @@ -3,7 +3,7 @@ use crate::handler::paths::{file_path, FileType}; -use super::parser_models::Style; +use super::parser_models::{AggregatorConfig, RateLimiter, Style}; use log::LevelFilter; use rlua::Lua; use std::{collections::HashMap, fs, thread::available_parallelism}; @@ -35,17 +35,7 @@ pub struct Config { pub upstream_search_engines: Vec, pub request_timeout: u8, pub threads: u8, -} - -/// Configuration options for the aggregator. -/// -/// # Fields -/// -/// * `random_delay` - It stores the option to whether enable or disable random delays between -/// requests. -#[derive(Clone)] -pub struct AggregatorConfig { - pub random_delay: bool, + pub rate_limter: RateLimiter, } impl Config { @@ -88,6 +78,8 @@ impl Config { parsed_threads }; + let rate_limter = globals.get::<_,HashMap>("rate_limiter")?; + Ok(Config { port: globals.get::<_, u16>("port")?, binding_ip: globals.get::<_, String>("binding_ip")?, @@ -109,6 +101,10 @@ impl Config { .collect(), request_timeout: globals.get::<_, u8>("request_timeout")?, threads, + rate_limter: RateLimiter { + number_of_requests: rate_limter["number_of_requests"], + time_limit: rate_limter["time_limit"], + } }) }) } diff --git a/src/config/parser_models.rs b/src/config/parser_models.rs index 0bc52d8..14e4dd6 100644 --- a/src/config/parser_models.rs +++ b/src/config/parser_models.rs @@ -36,3 +36,26 @@ impl Style { Style { theme, colorscheme } } } + +/// Configuration options for the aggregator. +/// +/// # Fields +/// +/// * `random_delay` - It stores the option to whether enable or disable random delays between +/// requests. +#[derive(Clone)] +pub struct AggregatorConfig { + pub random_delay: bool, +} + +/// Configuration options for the rate limter middleware. +/// +/// # Fields +/// +/// * `number_of_requests` - +/// * `time_limit` - +#[derive(Clone)] +pub struct RateLimiter { + pub number_of_requests: u8, + pub time_limit: u8, +}