2020-11-24 18:58:18 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2020-11-21 22:20:32 +00:00
|
|
|
use anyhow::*;
|
|
|
|
use log::LevelFilter;
|
2020-11-25 05:42:09 +00:00
|
|
|
use kochab::{Server, Request, Response, GEMINI_PORT, Document};
|
2020-11-21 22:20:32 +00:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
|
|
|
env_logger::builder()
|
2020-11-25 05:42:09 +00:00
|
|
|
.filter_module("kochab", LevelFilter::Debug)
|
2020-11-21 22:20:32 +00:00
|
|
|
.init();
|
|
|
|
|
|
|
|
Server::bind(("localhost", GEMINI_PORT))
|
|
|
|
.add_route("/", handle_request)
|
2020-11-24 18:58:18 +00:00
|
|
|
.ratelimit("/limit", 2, Duration::from_secs(60))
|
2020-11-21 22:20:32 +00:00
|
|
|
.serve()
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_request(request: Request) -> BoxFuture<'static, Result<Response>> {
|
|
|
|
async move {
|
|
|
|
let mut document = Document::new();
|
|
|
|
|
|
|
|
if let Some("limit") = request.trailing_segments().get(0).map(String::as_str) {
|
|
|
|
document.add_text("You're on a rate limited page!")
|
|
|
|
.add_text("You can only access this page twice per minute");
|
|
|
|
} else {
|
|
|
|
document.add_text("You're on a normal page!")
|
|
|
|
.add_text("You can access this page as much as you like.");
|
|
|
|
}
|
|
|
|
document.add_blank_line()
|
|
|
|
.add_link("/limit", "Go to rate limited page")
|
|
|
|
.add_link("/", "Go to a page that's not rate limited");
|
|
|
|
Ok(Response::document(document))
|
|
|
|
}
|
|
|
|
.boxed()
|
|
|
|
}
|