implement list(s) reload api

This commit is contained in:
yggverse 2026-03-28 06:40:01 +02:00
parent cda06f0811
commit 96b9646c8c
4 changed files with 68 additions and 4 deletions

View file

@ -29,6 +29,8 @@ RUST_LOG=trace psokcs -c=/path/to/config.toml no-auth
* `/api/totals` - blocking summary * `/api/totals` - blocking summary
* `/api/list/enable/<ID>` - enable all parsed rules of given list ID (`[list.ID]` in your config) * `/api/list/enable/<ID>` - enable all parsed rules of given list ID (`[list.ID]` in your config)
* `/api/list/disable/<ID>` - disable all parsed rules of given list ID (`[list.ID]` in your config) * `/api/list/disable/<ID>` - disable all parsed rules of given list ID (`[list.ID]` in your config)
* `/api/list/reload/<ID>` - reload list rules from its `source` by list ID (`[list.ID]` in your config)
* `/api/list/reload` - reload all lists rules from their `source`
### Allow list example ### Allow list example

View file

@ -65,6 +65,31 @@ async fn api_list_disable(
)) ))
} }
#[rocket::get("/api/list/reload/<alias>")]
async fn api_list_reload(
alias: Option<&str>,
rules: &State<Arc<RwLock<Rules>>>,
) -> Result<Json<Vec<String>>, Status> {
Ok(Json(rules.write().await.reload(alias).await.map_err(
|err| {
error!("List reload error: `{err}`");
Status::InternalServerError
},
)?))
}
#[rocket::get("/api/list/reload")]
async fn api_list_reload_all(
rules: &State<Arc<RwLock<Rules>>>,
) -> Result<Json<Vec<String>>, Status> {
Ok(Json(rules.write().await.reload(None).await.map_err(
|err| {
error!("Lists reload error: `{err}`");
Status::InternalServerError
},
)?))
}
#[rocket::launch] #[rocket::launch]
async fn rocket() -> _ { async fn rocket() -> _ {
env_logger::init(); env_logger::init();
@ -106,7 +131,14 @@ async fn rocket() -> _ {
.manage(Instant::now()) .manage(Instant::now())
.mount( .mount(
"/", "/",
rocket::routes![index, api_totals, api_list_enable, api_list_disable], rocket::routes![
index,
api_totals,
api_list_enable,
api_list_disable,
api_list_reload,
api_list_reload_all
],
) )
} }

View file

@ -16,13 +16,37 @@ impl Rules {
.insert(list_alias.into(), List::init(src, status).await?) .insert(list_alias.into(), List::init(src, status).await?)
.is_none()) .is_none())
} }
/// Change rule set status by list ID /// Change rule set status by alias
pub async fn set_enabled(&mut self, list_alias: &str, status: bool) -> Result<Option<bool>> { pub async fn set_enabled(&mut self, list_alias: &str, status: bool) -> Result<Option<bool>> {
Ok(match self.0.get_mut(list_alias) { Ok(match self.0.get_mut(list_alias) {
Some(list) => Some(list.set_enabled(status).await?), Some(list) => Some(list.set_enabled(status).await?),
None => None, None => None,
}) })
} }
/// Reload rule set by alias from it source
/// * `None` to reload all
pub async fn reload(&mut self, list_alias: Option<&str>) -> Result<Vec<String>> {
let mut affected = Vec::with_capacity(if list_alias.is_some() {
1
} else {
self.0.len()
});
match list_alias {
Some(alias) => {
if let Some(list) = self.0.get_mut(alias) {
list.reload().await?;
affected.push(alias.into());
}
}
None => {
for (alias, list) in self.0.iter_mut() {
list.reload().await?;
affected.push(alias.into());
}
}
}
Ok(affected)
}
/// Check if rule is exist in the index /// Check if rule is exist in the index
pub fn any(&self, value: &str) -> bool { pub fn any(&self, value: &str) -> bool {
self.0.values().any(|list| list.contains(value)) self.0.values().any(|list| list.contains(value))

View file

@ -24,7 +24,7 @@ impl List {
is_enabled, is_enabled,
}) })
} }
/// Change rule set status by list ID /// Change rule set status
pub async fn set_enabled(&mut self, is_enabled: bool) -> Result<bool> { pub async fn set_enabled(&mut self, is_enabled: bool) -> Result<bool> {
let was_enabled = self.is_enabled; let was_enabled = self.is_enabled;
self.is_enabled = is_enabled; self.is_enabled = is_enabled;
@ -35,7 +35,13 @@ impl List {
} }
Ok(was_enabled) Ok(was_enabled)
} }
/// Check if rule is exist in the items index /// Reload rule set from it source
pub async fn reload(&mut self) -> Result<()> {
self.rules.clear();
load(&mut self.rules, &self.source).await?;
Ok(())
}
/// Check if rule is exist in the index
pub fn contains(&self, value: &str) -> bool { pub fn contains(&self, value: &str) -> bool {
self.is_enabled && self.rules.iter().any(|rule| rule.contains(value)) self.is_enabled && self.rules.iter().any(|rule| rule.contains(value))
} }