aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorFuwn <[email protected]>2022-03-26 09:12:57 +0000
committerFuwn <[email protected]>2022-03-26 09:12:57 +0000
commite3fe241b126b049499cdd7263715a83278846f33 (patch)
treebfd1e657053c2fb49c794d9293c15804200d8c7b /examples
parentfeat(mount): dynamic parameters (diff)
downloadwindmark-e3fe241b126b049499cdd7263715a83278846f33.tar.xz
windmark-e3fe241b126b049499cdd7263715a83278846f33.zip
feat(response): variable responses
Diffstat (limited to 'examples')
-rw-r--r--examples/windmark.rs39
1 files changed, 27 insertions, 12 deletions
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()
}