| Crates.io | axum-tariff |
| lib.rs | axum-tariff |
| version | 0.1.1 |
| created_at | 2025-04-15 13:47:10.320785+00 |
| updated_at | 2025-05-01 07:27:55.871298+00 |
| description | An Axum middleware to apply request delay tariffs based on client IP address. |
| homepage | https://github.com/jdrouet/axum-tariff |
| repository | https://github.com/jdrouet/axum-tariff |
| max_upload_size | |
| id | 1634488 |
| size | 53,443 |
“Some countries... they need to wait.” — You, probably
🚦 Middleware for Axum that introduces configurable request delays based on the client's country (IP geolocation).
Inspired by the chaotic beauty of international trade wars, this crate uses the MaxMind GeoIP2 database to detect IPs by country and apply a delay ("tariff") per your configuration.
tower::Layer and tower::Service integrationcargo add axum-tariff
use axum::{routing::get, Router};
use std::{net::IpAddr, time::Duration};
use axum_tariff::{Config, Reader};
#[tokio::main]
async fn main() {
let reader = Reader::open_readfile("assets/GeoLite2-Country-Test.mmdb").unwrap();
let layer = Config::new(reader)
.with("US", Duration::from_secs(1))
.with("FR", Duration::from_millis(500))
.into_layer();
let app: Router<()> = Router::new()
.route("/", get(|| async { "Hello, world!" }))
.layer(layer);
let listener = tokio::net::TcpListener::bind("127.0.1.1:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
This crate includes tests that use the GeoLite2-Country-Test.mmdb. You'll need to place it at:
assets/GeoLite2-Country-Test.mmdb
Then:
cargo test
let config = Config::new(reader)
.with("CN", Duration::from_secs(2));
MIT