2020-11-20 03:53:50 +00:00
|
|
|
use anyhow::*;
|
|
|
|
use log::LevelFilter;
|
2020-12-07 22:57:30 +00:00
|
|
|
use kochab::{Gemtext, Request, Response};
|
2020-11-20 03:53:50 +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-20 03:53:50 +00:00
|
|
|
.init();
|
|
|
|
|
2020-12-01 07:31:08 +00:00
|
|
|
kochab::Server::new()
|
2020-11-20 03:53:50 +00:00
|
|
|
.add_route("/", handle_base)
|
|
|
|
.add_route("/route", handle_short)
|
|
|
|
.add_route("/route/long", handle_long)
|
2020-12-01 07:31:08 +00:00
|
|
|
.serve_unix("kochab.sock")
|
2020-11-20 03:53:50 +00:00
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2020-11-20 19:00:49 +00:00
|
|
|
async fn handle_base(req: Request) -> Result<Response> {
|
2020-12-07 22:57:30 +00:00
|
|
|
Ok(generate_resp("base", &req))
|
2020-11-20 03:53:50 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 19:00:49 +00:00
|
|
|
async fn handle_short(req: Request) -> Result<Response> {
|
2020-12-07 22:57:30 +00:00
|
|
|
Ok(generate_resp("short", &req))
|
2020-11-20 03:53:50 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 19:00:49 +00:00
|
|
|
async fn handle_long(req: Request) -> Result<Response> {
|
2020-12-07 22:57:30 +00:00
|
|
|
Ok(generate_resp("long", &req))
|
2020-11-20 03:53:50 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 22:57:30 +00:00
|
|
|
fn generate_resp(route_name: &str, req: &Request) -> Response {
|
2020-11-20 18:22:34 +00:00
|
|
|
let trailing = req.trailing_segments().join("/");
|
2020-12-07 22:57:30 +00:00
|
|
|
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()
|
2020-11-20 03:53:50 +00:00
|
|
|
}
|