diff --git a/examples/document.rs b/examples/document.rs index 3610fe8..df745a6 100644 --- a/examples/document.rs +++ b/examples/document.rs @@ -8,10 +8,11 @@ async fn main() -> Result<()> { .filter_module("kochab", LevelFilter::Debug) .init(); - let response: Response = Document::new() - .add_preformatted_with_alt("kochab", include_str!("kochab_logo.txt")) - .add_blank_line() - .add_text( + // Generate a fancy procedural response + let response: Response = Gemtext::new() + .preformatted("kochab", include_str!("kochab_logo.txt")) + .blank_line() + .text( concat!( "Kochab is an extension & a fork of the Gemini SDK [northstar]. Where", " northstar creates an efficient and flexible foundation for Gemini projects,", @@ -20,89 +21,27 @@ async fn main() -> Result<()> { " worrying about needing to build the tools to get there." ) ) - .add_blank_line() - .add_link("https://github.com/Alch-Emi/kochab", "GitHub") - .add_blank_line() - .add_heading(H2, "Usage") - .add_blank_line() - .add_text("Add the latest version of kochab to your `Cargo.toml`.") - .add_blank_line() - .add_preformatted_with_alt("toml", r#"kochab = { git = "https://github.com/Alch-Emi/kochab.git" }"#) - .add_blank_line() - .add_heading(H2, "Generating a key & certificate") - .add_blank_line() - .add_preformatted_with_alt("sh", concat!( + .blank_line() + .link("https://github.com/Alch-Emi/kochab", Some("GitHub")) + .blank_line() + .heading(2, "Usage") + .blank_line() + .text("Add the latest version of kochab to your `Cargo.toml`.") + .blank_line() + .preformatted("toml", r#"kochab = { git = "https://github.com/Alch-Emi/kochab.git" }"#) + .blank_line() + .heading(2, "Generating a key & certificate") + .blank_line() + .preformatted("sh", concat!( "mkdir cert && cd cert\n", "openssl req -x509 -nodes -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365", )) .into(); -<<<<<<< HEAD -fn handle_request(_request: Request) -> BoxFuture<'static, Result> { - async move { - let response = Gemtext::new() - .preformatted("Northstar", include_str!("northstar_logo.txt")) - .blank_line() - .link("https://docs.rs/northstar", Some("Documentation")) - .link("https://github.com/panicbit/northstar", Some("GitHub")) - .blank_line() - .heading(1, "Usage") - .blank_line() - .text("Add the latest version of northstar to your `Cargo.toml`.") - .blank_line() - .heading(2, "Manually") - .blank_line() - .preformatted("toml", r#"northstar = "0.3.0" # check crates.io for the latest version"#) - .blank_line() - .heading(2, "Automatically") - .blank_line() - .preformatted("sh", "cargo add northstar") - .blank_line() - .heading(1, "Generating a key & certificate") - .blank_line() - .preformatted("sh", concat!( - "mkdir cert && cd cert\n", - "openssl req -x509 -nodes -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365", - )) - .into(); - Ok(response) - } - .boxed() -||||||| merged common ancestors -fn handle_request(_request: Request) -> BoxFuture<'static, Result> { - async move { - let response = Document::new() - .add_preformatted(include_str!("northstar_logo.txt")) - .add_blank_line() - .add_link("https://docs.rs/northstar", "Documentation") - .add_link("https://github.com/panicbit/northstar", "GitHub") - .add_blank_line() - .add_heading(H1, "Usage") - .add_blank_line() - .add_text("Add the latest version of northstar to your `Cargo.toml`.") - .add_blank_line() - .add_heading(H2, "Manually") - .add_blank_line() - .add_preformatted_with_alt("toml", r#"northstar = "0.3.0" # check crates.io for the latest version"#) - .add_blank_line() - .add_heading(H2, "Automatically") - .add_blank_line() - .add_preformatted_with_alt("sh", "cargo add northstar") - .add_blank_line() - .add_heading(H1, "Generating a key & certificate") - .add_blank_line() - .add_preformatted_with_alt("sh", concat!( - "mkdir cert && cd cert\n", - "openssl req -x509 -nodes -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365", - )) - .into(); - Ok(response) - } - .boxed() -======= Server::new() + // You can also return the response from any one of your response handlers, but if + // you want to serve a static response, this works too .add_route("/", response) .serve_unix("kochab.sock") .await ->>>>>>> devel } diff --git a/examples/ratelimiting.rs b/examples/ratelimiting.rs index f8021cb..a43777d 100644 --- a/examples/ratelimiting.rs +++ b/examples/ratelimiting.rs @@ -2,7 +2,7 @@ use std::time::Duration; use anyhow::*; use log::LevelFilter; -use kochab::{Server, Request, Response, Document}; +use kochab::{Server, Request, Response, Gemtext}; #[tokio::main] async fn main() -> Result<()> { @@ -18,17 +18,20 @@ async fn main() -> Result<()> { } async fn handle_request(request: Request) -> Result { - 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"); + let mut document = if let Some("limit") = request.trailing_segments().get(0).map(String::as_str) { + Gemtext::new() + .text("You're on a rate limited page!") + .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"); + Gemtext::new() + .text("You're on a normal page!") + .text("You can access this page as much as you like.") + }; + + document = document + .blank_line() + .link("/limit", Some("Go to rate limited page")) + .link("/", Some("Go to a page that's not rate limited")); + Ok(document.into()) } diff --git a/examples/routing.rs b/examples/routing.rs index 2586c58..34ad76f 100644 --- a/examples/routing.rs +++ b/examples/routing.rs @@ -1,6 +1,6 @@ use anyhow::*; use log::LevelFilter; -use kochab::{Document, document::HeadingLevel, Request, Response}; +use kochab::{Gemtext, Request, Response}; #[tokio::main] async fn main() -> Result<()> { @@ -17,32 +17,29 @@ async fn main() -> Result<()> { } async fn handle_base(req: Request) -> Result { - let doc = generate_doc("base", &req); - Ok(doc.into()) + Ok(generate_resp("base", &req)) } async fn handle_short(req: Request) -> Result { - let doc = generate_doc("short", &req); - Ok(doc.into()) + Ok(generate_resp("short", &req)) } async fn handle_long(req: Request) -> Result { - let doc = generate_doc("long", &req); - Ok(doc.into()) + Ok(generate_resp("long", &req)) } -fn generate_doc(route_name: &str, req: &Request) -> Document { +fn generate_resp(route_name: &str, req: &Request) -> Response { 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)) - .add_text(&format!("Trailing segments: /{}", trailing)) - .add_blank_line() - .add_text("Here's some links to try:") - .add_link_without_label("/") - .add_link_without_label("/route") - .add_link_without_label("/route/long") - .add_link_without_label("/route/not_real") - .add_link_without_label("/rowte"); - doc + 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::::None) + .link("/route", Option::::None) + .link("/route/long", Option::::None) + .link("/route/not_real", Option::::None) + .link("/rowte", Option::::None) + .into() } diff --git a/examples/user_management.rs b/examples/user_management.rs index d0e945d..4fc8283 100644 --- a/examples/user_management.rs +++ b/examples/user_management.rs @@ -1,7 +1,7 @@ use anyhow::*; use log::LevelFilter; use kochab::{ - Document, + Gemtext, Request, Response, Server, @@ -50,12 +50,12 @@ async fn main() -> Result<()> { /// certificate will be prompted to add a certificate and register. async fn handle_main(_req: Request, user: RegisteredUser) -> Result { // If the user is signed in, render and return their page - let response = Document::new() - .add_text("Your personal secret string:") - .add_text(user.as_ref()) - .add_blank_line() - .add_link("/update", "Change your string") - .add_link("/account", "Update your account") + let response = Gemtext::new() + .text("Your personal secret string:") + .text(user.as_ref()) + .blank_line() + .link("/update", Some("Change your string")) + .link("/account", Some("Update your account")) .into(); Ok(response) } @@ -72,10 +72,10 @@ async fn handle_update(_request: Request, mut user: RegisteredUser, inpu *user.as_mut() = input; // Render a response - let response = Document::new() - .add_text("String updated!") - .add_blank_line() - .add_link("/", "Back") + let response = Gemtext::new() + .text("String updated!") + .blank_line() + .link("/", Some("Back")) .into(); Ok(response)