From 06228c577ff34f85cf96681891851d2845c26979 Mon Sep 17 00:00:00 2001 From: postscriptum Date: Mon, 23 Mar 2026 08:11:19 +0200 Subject: [PATCH] create namespace for json api; make temporarily index page reference to api/totals --- README.md | 4 ++-- src/main.rs | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 41e3ce6..59c2583 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,8 @@ RUST_LOG=trace cargo run -- --allow=http://localhost/allow.txt \ ``` * set `socks5://127.0.0.1:1080` proxy in your application * open http://127.0.0.1:8010 in browser for stats: - * `/allow/domain.com` - add rule to the current session (and `--cache` if defined) - * `/block/domain.com` - delete rule from the current session (and `--cache` if defined) + * `/api/allow/domain.com` - add rule to the current session (and `--cache` if defined) + * `/api/block/domain.com` - delete rule from the current session (and `--cache` if defined) ### Allow list example diff --git a/src/main.rs b/src/main.rs index aed36ff..82a5247 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,11 +18,16 @@ use tokio::{net::TcpListener, task}; #[rocket::get("/")] async fn index(totals: &State>, startup_time: &State) -> Json { + Json(totals.inner().snap(startup_time.elapsed().as_secs())) // @TODO implement Web UI +} + +#[rocket::get("/api/totals")] +async fn api_totals(totals: &State>, startup_time: &State) -> Json { Json(totals.inner().snap(startup_time.elapsed().as_secs())) } -#[rocket::get("/allow/")] -async fn allow( +#[rocket::get("/api/allow/")] +async fn api_allow( rule: &str, list: &State>, totals: &State>, @@ -36,8 +41,8 @@ async fn allow( })?)) } -#[rocket::get("/block/")] -async fn block( +#[rocket::get("/api/block/")] +async fn api_block( rule: &str, list: &State>, totals: &State>, @@ -84,7 +89,10 @@ async fn rocket() -> _ { .manage(list) .manage(totals) .manage(Instant::now()) - .mount("/", rocket::routes![index, allow, block]) + .mount( + "/", + rocket::routes![index, api_totals, api_allow, api_block], + ) } async fn spawn_socks_server(opt: &'static Opt, list: Arc, totals: Arc) -> Result<()> {