Fix the examples to work with Gemtext

I always forget the examples
This commit is contained in:
Emi Tatsuo 2020-12-07 17:57:30 -05:00
parent 485f579e4c
commit 1c1e2567f5
Signed by: Emi
GPG Key ID: 68FAB2E2E6DFC98B
4 changed files with 62 additions and 123 deletions

View File

@ -8,10 +8,11 @@ async fn main() -> Result<()> {
.filter_module("kochab", LevelFilter::Debug) .filter_module("kochab", LevelFilter::Debug)
.init(); .init();
let response: Response = Document::new() // Generate a fancy procedural response
.add_preformatted_with_alt("kochab", include_str!("kochab_logo.txt")) let response: Response = Gemtext::new()
.add_blank_line() .preformatted("kochab", include_str!("kochab_logo.txt"))
.add_text( .blank_line()
.text(
concat!( concat!(
"Kochab is an extension & a fork of the Gemini SDK [northstar]. Where", "Kochab is an extension & a fork of the Gemini SDK [northstar]. Where",
" northstar creates an efficient and flexible foundation for Gemini projects,", " 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." " worrying about needing to build the tools to get there."
) )
) )
.add_blank_line() .blank_line()
.add_link("https://github.com/Alch-Emi/kochab", "GitHub") .link("https://github.com/Alch-Emi/kochab", Some("GitHub"))
.add_blank_line() .blank_line()
.add_heading(H2, "Usage") .heading(2, "Usage")
.add_blank_line() .blank_line()
.add_text("Add the latest version of kochab to your `Cargo.toml`.") .text("Add the latest version of kochab to your `Cargo.toml`.")
.add_blank_line() .blank_line()
.add_preformatted_with_alt("toml", r#"kochab = { git = "https://github.com/Alch-Emi/kochab.git" }"#) .preformatted("toml", r#"kochab = { git = "https://github.com/Alch-Emi/kochab.git" }"#)
.add_blank_line() .blank_line()
.add_heading(H2, "Generating a key & certificate") .heading(2, "Generating a key & certificate")
.add_blank_line() .blank_line()
.add_preformatted_with_alt("sh", concat!( .preformatted("sh", concat!(
"mkdir cert && cd cert\n", "mkdir cert && cd cert\n",
"openssl req -x509 -nodes -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365", "openssl req -x509 -nodes -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365",
)) ))
.into(); .into();
<<<<<<< HEAD
fn handle_request(_request: Request) -> BoxFuture<'static, Result<Response>> {
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<Response>> {
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() 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) .add_route("/", response)
.serve_unix("kochab.sock") .serve_unix("kochab.sock")
.await .await
>>>>>>> devel
} }

View File

@ -2,7 +2,7 @@ use std::time::Duration;
use anyhow::*; use anyhow::*;
use log::LevelFilter; use log::LevelFilter;
use kochab::{Server, Request, Response, Document}; use kochab::{Server, Request, Response, Gemtext};
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
@ -18,17 +18,20 @@ async fn main() -> Result<()> {
} }
async fn handle_request(request: Request) -> Result<Response> { async fn handle_request(request: Request) -> Result<Response> {
let mut document = Document::new(); let mut document = if let Some("limit") = request.trailing_segments().get(0).map(String::as_str) {
Gemtext::new()
if let Some("limit") = request.trailing_segments().get(0).map(String::as_str) { .text("You're on a rate limited page!")
document.add_text("You're on a rate limited page!") .text("You can only access this page twice per minute")
.add_text("You can only access this page twice per minute");
} else { } else {
document.add_text("You're on a normal page!") Gemtext::new()
.add_text("You can access this page as much as you like."); .text("You're on a normal page!")
} .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"); 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()) Ok(document.into())
} }

View File

@ -1,6 +1,6 @@
use anyhow::*; use anyhow::*;
use log::LevelFilter; use log::LevelFilter;
use kochab::{Document, document::HeadingLevel, Request, Response}; use kochab::{Gemtext, Request, Response};
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
@ -17,32 +17,29 @@ async fn main() -> Result<()> {
} }
async fn handle_base(req: Request) -> Result<Response> { async fn handle_base(req: Request) -> Result<Response> {
let doc = generate_doc("base", &req); Ok(generate_resp("base", &req))
Ok(doc.into())
} }
async fn handle_short(req: Request) -> Result<Response> { async fn handle_short(req: Request) -> Result<Response> {
let doc = generate_doc("short", &req); Ok(generate_resp("short", &req))
Ok(doc.into())
} }
async fn handle_long(req: Request) -> Result<Response> { async fn handle_long(req: Request) -> Result<Response> {
let doc = generate_doc("long", &req); Ok(generate_resp("long", &req))
Ok(doc.into())
} }
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 trailing = req.trailing_segments().join("/");
let mut doc = Document::new(); Gemtext::new()
doc.add_heading(HeadingLevel::H1, "Routing Demo") .heading(1, "Routing Demo")
.add_text(&format!("You're currently on the {} route", route_name)) .text(&format!("You're currently on the {} route", route_name))
.add_text(&format!("Trailing segments: /{}", trailing)) .text(&format!("Trailing segments: /{}", trailing))
.add_blank_line() .blank_line()
.add_text("Here's some links to try:") .text("Here's some links to try:")
.add_link_without_label("/") .link("/", Option::<String>::None)
.add_link_without_label("/route") .link("/route", Option::<String>::None)
.add_link_without_label("/route/long") .link("/route/long", Option::<String>::None)
.add_link_without_label("/route/not_real") .link("/route/not_real", Option::<String>::None)
.add_link_without_label("/rowte"); .link("/rowte", Option::<String>::None)
doc .into()
} }

View File

@ -1,7 +1,7 @@
use anyhow::*; use anyhow::*;
use log::LevelFilter; use log::LevelFilter;
use kochab::{ use kochab::{
Document, Gemtext,
Request, Request,
Response, Response,
Server, Server,
@ -50,12 +50,12 @@ async fn main() -> Result<()> {
/// certificate will be prompted to add a certificate and register. /// certificate will be prompted to add a certificate and register.
async fn handle_main(_req: Request, user: RegisteredUser<String>) -> Result<Response> { async fn handle_main(_req: Request, user: RegisteredUser<String>) -> Result<Response> {
// If the user is signed in, render and return their page // If the user is signed in, render and return their page
let response = Document::new() let response = Gemtext::new()
.add_text("Your personal secret string:") .text("Your personal secret string:")
.add_text(user.as_ref()) .text(user.as_ref())
.add_blank_line() .blank_line()
.add_link("/update", "Change your string") .link("/update", Some("Change your string"))
.add_link("/account", "Update your account") .link("/account", Some("Update your account"))
.into(); .into();
Ok(response) Ok(response)
} }
@ -72,10 +72,10 @@ async fn handle_update(_request: Request, mut user: RegisteredUser<String>, inpu
*user.as_mut() = input; *user.as_mut() = input;
// Render a response // Render a response
let response = Document::new() let response = Gemtext::new()
.add_text("String updated!") .text("String updated!")
.add_blank_line() .blank_line()
.add_link("/", "Back") .link("/", Some("Back"))
.into(); .into();
Ok(response) Ok(response)