From e3fe241b126b049499cdd7263715a83278846f33 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sat, 26 Mar 2022 09:12:57 +0000 Subject: feat(response): variable responses --- examples/windmark.rs | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) (limited to 'examples') diff --git a/examples/windmark.rs b/examples/windmark.rs index aa86ebc..d719993 100644 --- a/examples/windmark.rs +++ b/examples/windmark.rs @@ -21,12 +21,16 @@ #[macro_use] extern crate log; +use windmark::response::Response; + fn main() -> std::io::Result<()> { windmark::Router::new() .set_private_key_file("windmark_private.pem") .set_certificate_chain_file("windmark_pair.pem") .enable_default_logger(true) - .set_error_handler(|_, _, _| "error...".to_string()) + .set_error_handler(|_, _, _| { + Response::PermanentFailure("error...".to_string()) + }) .set_pre_route_callback(|stream, url| { info!( "accepted connection from {} to {}", @@ -43,25 +47,36 @@ fn main() -> std::io::Result<()> { .set_header(|_, _, _| "```\nART IS COOL\n```".to_string()) .set_footer(|_, _, _| "Copyright 2022".to_string()) .mount("/", |_, _, _| { - "# INDEX\n\nWelcome!\n\n=> /test Test Page\n=> /time Unix Epoch\n" - .to_string() + Response::Success( + "# INDEX\n\nWelcome!\n\n=> /test Test Page\n=> /time Unix Epoch\n" + .to_string(), + ) }) .mount("/ip", |stream, _, _| { - { format!("Hello, {}", stream.peer_addr().unwrap().ip()) }.into() + Response::Success( + { format!("Hello, {}", stream.peer_addr().unwrap().ip()) }.into(), + ) + }) + .mount("/test", |_, _, _| { + Response::Success("hi there\n=> / back".to_string()) }) - .mount("/test", |_, _, _| "hi there\n=> / back".to_string()) .mount("/time", |_, _, _| { - std::time::UNIX_EPOCH - .elapsed() - .unwrap() - .as_nanos() - .to_string() + Response::Success( + std::time::UNIX_EPOCH + .elapsed() + .unwrap() + .as_nanos() + .to_string(), + ) }) .mount("/query", |_, url, _| { - format!("queries: {:?}", windmark::utilities::queries_from_url(&url)) + Response::Success(format!( + "queries: {:?}", + windmark::utilities::queries_from_url(&url) + )) }) .mount("/param/:lang", |_, _url, dynamic_parameter| { - format!("Parameter lang is {:?}", dynamic_parameter) + Response::Success(format!("Parameter lang is {:?}", dynamic_parameter)) }) .run() } -- cgit v1.2.3