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-12-01 07:31:08 +00:00
|
|
|
use kochab::{Server, Request, Response, 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();
|
|
|
|
|
2020-12-01 07:31:08 +00:00
|
|
|
Server::new()
|
2020-11-21 22:20:32 +00:00
|
|
|
.add_route("/", handle_request)
|
2020-12-16 15:17:27 +00:00
|
|
|
.ratelimit("/limit", 2, Duration::from_secs(10))
|
2020-12-14 00:53:18 +00:00
|
|
|
.serve_ip("localhost:1965")
|
2020-11-21 22:20:32 +00:00
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2020-12-01 22:13:54 +00:00
|
|
|
async fn handle_request(request: Request) -> Result<Response> {
|
|
|
|
let mut document = Document::new();
|
2020-11-21 22:20:32 +00:00
|
|
|
|
2020-12-01 22:13:54 +00:00
|
|
|
if let Some("limit") = request.trailing_segments().get(0).map(String::as_str) {
|
|
|
|
document.add_text("You're on a rate limited page!")
|
2020-12-16 15:17:27 +00:00
|
|
|
.add_text("You can only access this page twice every 10 seconds");
|
2020-12-01 22:13:54 +00:00
|
|
|
} else {
|
|
|
|
document.add_text("You're on a normal page!")
|
|
|
|
.add_text("You can access this page as much as you like.");
|
2020-11-21 22:20:32 +00:00
|
|
|
}
|
2020-12-01 22:13:54 +00:00
|
|
|
document.add_blank_line()
|
|
|
|
.add_link("/limit", "Go to rate limited page")
|
|
|
|
.add_link("/", "Go to a page that's not rate limited");
|
|
|
|
Ok(document.into())
|
2020-11-21 22:20:32 +00:00
|
|
|
}
|