diff --git a/Cargo.toml b/Cargo.toml index d6c2005..6abec11 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "id-worker" -version = "0.1.8" +version = "0.1.9" edition = "2021" description = "Cloudflare worker for providing ip address id" license = "Apache-2.0" diff --git a/src/lib.rs b/src/lib.rs index 5d6a37d..cef7b77 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,16 +27,32 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result Result(req: Request, ctx: RouteContext<()>, f: F) -> Result +async fn checked(req: Request, ctx: RouteContext<()>, o: O, e: E) -> Result where - F: FnOnce(&str) -> Result, + O: FnOnce(&str) -> Result, + E: FnOnce(&str, u16) -> Result, { if let Ok(Some(ip)) = req.headers().get("CF-Connecting-IP") { if let Ok(store) = ctx.kv("id") { if let Err(x) = rate::rate_control(store, &ip).await { - Response::error(x, 429) + e(&x, 429) } else { - f(&ip) + o(&ip) } } else { - Response::error("Service unavailable :(", 503) + e("Service unavailable :(", 503) } } else { console_log!("Missing 'CF-Connecting-IP' header"); console_log!("{:?}", req.headers()); - Response::error("Missing 'CF-Connecting-IP' header", 424) + e("Missing 'CF-Connecting-IP' header", 424) } }