kochab/examples/ratelimiting.rs

38 lines
1.1 KiB
Rust

use std::time::Duration;
use anyhow::*;
use log::LevelFilter;
use kochab::{Server, Request, Response, Gemtext};
#[tokio::main]
async fn main() -> Result<()> {
env_logger::builder()
.filter_module("kochab", LevelFilter::Debug)
.init();
Server::new()
.add_route("/", handle_request)
.ratelimit("/limit", 2, Duration::from_secs(60))
.serve_unix("kochab.sock")
.await
}
async fn handle_request(request: Request) -> Result<Response> {
let mut document = if let Some("limit") = request.trailing_segments().get(0).map(String::as_str) {
Gemtext::new()
.text("You're on a rate limited page!")
.text("You can only access this page twice per minute")
} else {
Gemtext::new()
.text("You're on a normal page!")
.text("You can access this page as much as you like.")
};
document = document
.blank_line()
.link("/limit", Some("Go to rate limited page".to_string()))
.link("/", Some("Go to a page that's not rate limited".to_string()));
Ok(document.into())
}