Skip to content

Commit

Permalink
HTTP API support the remove_first field
Browse files Browse the repository at this point in the history
  • Loading branch information
magiclen committed Nov 10, 2023
1 parent 70ba363 commit 6a9cef1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nginx-cache-purge"
version = "0.3.0"
version = "0.3.1"
authors = ["Magic Len <[email protected]>"]
edition = "2021"
rust-version = "1.70"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ After finishing the settings:

If the service successfully removes any cache, it will respond the HTTP status code **200**. If no cache needs to be removed, it will respond the HTTP status code **202**.

The `remove_first` field can be set to the query of the `/` endpoint URL, allowing the exclusion of the prefix from the request path of the `key`.

### No Service

If we want to use `nginx-cache-purge` CLI with [lua-nginx-module](https://github.com/openresty/lua-nginx-module), instead of running the service in the background.
Expand Down
15 changes: 11 additions & 4 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,24 @@ impl Accept for ServerAccept {

#[derive(Debug, Deserialize)]
struct Args {
cache_path: PathBuf,
levels: String,
key: String,
cache_path: PathBuf,
levels: String,
key: String,
remove_first: Option<String>,
}

async fn index_handler(args: Query<Args>) -> impl IntoResponse {
let Args {
cache_path,
levels,
key,
mut key,
remove_first,
} = args.0;
if let Some(remove_first) = remove_first {
if let Some(index) = key.find(remove_first.as_str()) {
key.replace_range(index..index + remove_first.len(), "");
}
}

match tokio::task::spawn_blocking(|| purge(cache_path, levels, key)).await.unwrap() {
Ok(result) => match result {
Expand Down

0 comments on commit 6a9cef1

Please sign in to comment.