|
|
|
@ -3,16 +3,32 @@ use log::LevelFilter;
|
|
|
|
|
use kochab::{Document, document::HeadingLevel, Request, Response}; |
|
|
|
|
|
|
|
|
|
#[tokio::main] |
|
|
|
|
/// A quick demo to show off how an app can have multiple handlers on diffrent routes
|
|
|
|
|
///
|
|
|
|
|
/// We set up three different routes:
|
|
|
|
|
/// * `/route/long/*`
|
|
|
|
|
/// * `/route/*`
|
|
|
|
|
/// * `/*` which matches any other route
|
|
|
|
|
///
|
|
|
|
|
/// Each route generates a slightly different page, although they all use the same layout
|
|
|
|
|
/// through the [`generate_doc()`] method. Each page states which route was matched, and
|
|
|
|
|
/// all the trailing path segments.
|
|
|
|
|
///
|
|
|
|
|
/// For example, a request to `/route/trail` would be matched by the short route
|
|
|
|
|
/// (`/route/*`) with the trailing path segment `["trail"]`
|
|
|
|
|
async fn main() -> Result<()> { |
|
|
|
|
|
|
|
|
|
// We set up logging so we can see what's happening. This isn't technically required,
|
|
|
|
|
// and you can use a simpler solution (like env_logger::init()) during production
|
|
|
|
|
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_ip("localhost:1965") |
|
|
|
|
kochab::Server::new() // Create a new server
|
|
|
|
|
.add_route("/", handle_base) // Register the base route (order irrelevant)
|
|
|
|
|
.add_route("/route", handle_short) // Reigster the short route
|
|
|
|
|
.add_route("/route/long", handle_long) // Register the long route
|
|
|
|
|
.serve_ip("localhost:1965") // Start the server
|
|
|
|
|
.await |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -32,7 +48,11 @@ async fn handle_long(req: Request) -> Result<Response> {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn generate_doc(route_name: &str, req: &Request) -> Document { |
|
|
|
|
|
|
|
|
|
// Trailing segments comes in as a Vec of segments, so we join them together for
|
|
|
|
|
// display purposes
|
|
|
|
|
let trailing = req.trailing_segments().join("/"); |
|
|
|
|
|
|
|
|
|
let mut doc = Document::new(); |
|
|
|
|
doc.add_heading(HeadingLevel::H1, "Routing Demo") |
|
|
|
|
.add_text(&format!("You're currently on the {} route", route_name)) |
|
|
|
|