kochab/examples/routing.rs
2020-12-07 17:57:30 -05:00

46 lines
1.3 KiB
Rust

use anyhow::*;
use log::LevelFilter;
use kochab::{Gemtext, Request, Response};
#[tokio::main]
async fn main() -> Result<()> {
env_logger::builder()
.filter_module("kochab", LevelFilter::Debug)
.init();
kochab::Server::new()
.add_route("/", handle_base)
.add_route("/route", handle_short)
.add_route("/route/long", handle_long)
.serve_unix("kochab.sock")
.await
}
async fn handle_base(req: Request) -> Result<Response> {
Ok(generate_resp("base", &req))
}
async fn handle_short(req: Request) -> Result<Response> {
Ok(generate_resp("short", &req))
}
async fn handle_long(req: Request) -> Result<Response> {
Ok(generate_resp("long", &req))
}
fn generate_resp(route_name: &str, req: &Request) -> Response {
let trailing = req.trailing_segments().join("/");
Gemtext::new()
.heading(1, "Routing Demo")
.text(&format!("You're currently on the {} route", route_name))
.text(&format!("Trailing segments: /{}", trailing))
.blank_line()
.text("Here's some links to try:")
.link("/", Option::<String>::None)
.link("/route", Option::<String>::None)
.link("/route/long", Option::<String>::None)
.link("/route/not_real", Option::<String>::None)
.link("/rowte", Option::<String>::None)
.into()
}