Crates.io | tower-fallthrough-filter |
lib.rs | tower-fallthrough-filter |
version | 0.0.3 |
source | src |
created_at | 2024-02-29 15:26:04.287566 |
updated_at | 2024-03-02 18:06:02.317976 |
description | A Tower middleware that gives controll to a defined service if the filter matches and otherwise falls through to the inner service. |
homepage | https://github.com/32byte/htmx-server |
repository | https://github.com/32byte/htmx-server |
max_upload_size | |
id | 1157889 |
size | 58,146 |
Ever wanted to filter a Tower Service, but not abort the request when the filter doesn't match? Now you can!
Add this to your Cargo.toml
:
[dependencies]
tower-fallthrough-filter = "*"
Or add it using the cargo
command:
cargo add tower-fallthrough-filter
use tower_fallthrough_filter::{Filter, FilterLayer};
#[derive(Clone)]
struct MyFilter;
impl<T> Filter<T> for MyFilter {
fn filter(&self, _: T) -> bool {
// This can be any logic you want
// and can depend on some state stored
// in the filter itself.
// When this function returns true, my_service
// will be called, otherwise the request will
// fall through to the next service.
some_buisness_logic()
}
}
let my_service = MyService::new();
let layer = FilterLayer::new(MyFilter, my_service);
// Now you can use the layer as a normal Tower Layer
Check the examples folder for more examples.