diff options
| author | Fuwn <[email protected]> | 2023-04-10 08:22:30 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-04-10 08:22:30 +0000 |
| commit | 9b47502b33e9710f76b442534835e99fefc45293 (patch) | |
| tree | ed0f7ea4f8b08fe4f29b848bfea12e9e9405f897 /examples | |
| parent | refactor(partial): into trait (diff) | |
| download | windmark-9b47502b33e9710f76b442534835e99fefc45293.tar.xz windmark-9b47502b33e9710f76b442534835e99fefc45293.zip | |
chore(examples): seperate complex examples
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/async.rs | 47 | ||||
| -rw-r--r-- | examples/async_stateful_module.rs | 70 | ||||
| -rw-r--r-- | examples/binary.rs | 42 | ||||
| -rw-r--r-- | examples/callbacks.rs | 44 | ||||
| -rw-r--r-- | examples/certificate.rs | 53 | ||||
| -rw-r--r-- | examples/default_logger.rs | 41 | ||||
| -rw-r--r-- | examples/empty.rs | 28 | ||||
| -rw-r--r-- | examples/error_handler.rs | 44 | ||||
| -rw-r--r-- | examples/fix_path.rs | 33 | ||||
| -rw-r--r-- | examples/input.rs | 44 | ||||
| -rw-r--r-- | examples/mime.rs | 33 | ||||
| -rw-r--r-- | examples/parameters.rs | 51 | ||||
| -rw-r--r-- | examples/partial.rs | 34 | ||||
| -rw-r--r-- | examples/query.rs | 38 | ||||
| -rw-r--r-- | examples/responses.rs | 49 | ||||
| -rw-r--r-- | examples/stateless_module.rs | 37 | ||||
| -rw-r--r-- | examples/windmark.rs | 219 |
17 files changed, 688 insertions, 219 deletions
diff --git a/examples/async.rs b/examples/async.rs new file mode 100644 index 0000000..9851f56 --- /dev/null +++ b/examples/async.rs @@ -0,0 +1,47 @@ +// This file is part of Windmark <https://github.com/gemrest/windmark>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +//! `cargo run --example async` + +#[windmark::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + let mut router = windmark::Router::new(); + let async_clicks = std::sync::Arc::new(tokio::sync::Mutex::new(0)); + + router.set_private_key_file("windmark_private.pem"); + router.set_certificate_file("windmark_public.pem"); + router.mount("/clicks", move |_| { + let async_clicks = async_clicks.clone(); + + async move { + let mut clicks = async_clicks.lock().await; + + *clicks += 1; + + windmark::Response::success(*clicks) + } + }); + router.mount( + "/macro", + windmark::success_async!( + async { "This response was sent using an asynchronous macro." }.await + ), + ); + + router.run().await +} diff --git a/examples/async_stateful_module.rs b/examples/async_stateful_module.rs new file mode 100644 index 0000000..7c30e99 --- /dev/null +++ b/examples/async_stateful_module.rs @@ -0,0 +1,70 @@ +// This file is part of Windmark <https://github.com/gemrest/windmark>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +//! `cargo run --example async_stateful_module` + +use std::sync::{Arc, Mutex}; + +use windmark::{context::HookContext, Router}; + +#[derive(Default)] +struct Clicker { + clicks: Arc<Mutex<usize>>, +} + +#[async_trait::async_trait] +impl windmark::AsyncModule for Clicker { + async fn on_attach(&mut self, _router: &mut Router) { + println!("module 'clicker' has been attached!"); + } + + async fn on_pre_route(&mut self, context: HookContext) { + *self.clicks.lock().unwrap() += 1; + + println!( + "module 'clicker' has been called before the route '{}' with {} clicks!", + context.url.path(), + self.clicks.lock().unwrap() + ); + } + + async fn on_post_route(&mut self, context: HookContext) { + println!( + "module 'clicker' clicker has been called after the route '{}' with {} \ + clicks!", + context.url.path(), + self.clicks.lock().unwrap() + ); + } +} + +#[windmark::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + let mut router = Router::new(); + + router.set_private_key_file("windmark_private.pem"); + router.set_certificate_file("windmark_public.pem"); + #[cfg(feature = "logger")] + { + router.enable_default_logger(true); + } + router.attach_async(Clicker::default()); + router.mount("/", windmark::success!("Hello!")); + + router.run().await +} diff --git a/examples/binary.rs b/examples/binary.rs new file mode 100644 index 0000000..2934105 --- /dev/null +++ b/examples/binary.rs @@ -0,0 +1,42 @@ +// This file is part of Windmark <https://github.com/gemrest/windmark>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +//! `cargo run --example binary` +//! +//! Optionally, you can run this example with the `auto-deduce-mime` feature +//! enabled. + +#[windmark::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + let mut router = windmark::Router::new(); + + router.set_private_key_file("windmark_private.pem"); + router.set_certificate_file("windmark_public.pem"); + #[cfg(feature = "auto-deduce-mime")] + router.mount("/automatic", { + windmark::binary_success!(include_bytes!("../LICENSE")) + }); + router.mount("/specific", { + windmark::binary_success!(include_bytes!("../LICENSE"), "text/plain") + }); + router.mount("/direct", { + windmark::binary_success!("This is a string.", "text/plain") + }); + + router.run().await +} diff --git a/examples/callbacks.rs b/examples/callbacks.rs new file mode 100644 index 0000000..b161a03 --- /dev/null +++ b/examples/callbacks.rs @@ -0,0 +1,44 @@ +// This file is part of Windmark <https://github.com/gemrest/windmark>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +//! `cargo run --example callbacks` + +#[windmark::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + windmark::Router::new() + .set_private_key_file("windmark_private.pem") + .set_certificate_file("windmark_public.pem") + .mount("/", windmark::success!("Hello!")) + .set_pre_route_callback(|context| { + println!( + "accepted connection from {} to {}", + context.peer_address.unwrap().ip(), + context.url.to_string() + ) + }) + .set_post_route_callback(|context, content| { + content.content = content.content.replace("Hello", "Hi"); + + println!( + "closed connection from {}", + context.peer_address.unwrap().ip() + ) + }) + .run() + .await +} diff --git a/examples/certificate.rs b/examples/certificate.rs new file mode 100644 index 0000000..e9f1847 --- /dev/null +++ b/examples/certificate.rs @@ -0,0 +1,53 @@ +// This file is part of Windmark <https://github.com/gemrest/windmark>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +//! `cargo run --example certificate` + +use windmark::Response; + +#[windmark::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + windmark::Router::new() + .set_private_key_file("windmark_private.pem") + .set_certificate_file("windmark_public.pem") + .mount("/secret", |context: windmark::context::RouteContext| { + if let Some(certificate) = context.certificate { + Response::success(format!("Your public key is '{}'.", { + (|| -> Result<String, openssl::error::ErrorStack> { + Ok(format!( + "{:?}", + certificate.public_key()?.rsa()?.public_key_to_pem()? + )) + })() + .unwrap_or_else(|_| { + "An error occurred while reading your public key.".to_string() + }) + })) + } else { + Response::client_certificate_required( + "This is a secret route ... Identify yourself!", + ) + } + }) + .mount( + "/invalid", + windmark::certificate_not_valid!("Your certificate is invalid."), + ) + .run() + .await +} diff --git a/examples/default_logger.rs b/examples/default_logger.rs new file mode 100644 index 0000000..c4f5b36 --- /dev/null +++ b/examples/default_logger.rs @@ -0,0 +1,41 @@ +// This file is part of Windmark <https://github.com/gemrest/windmark>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +//! `cargo run --example default_logger --features logger` + +#[windmark::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + let mut router = windmark::Router::new(); + + router.set_private_key_file("windmark_private.pem"); + router.set_certificate_file("windmark_public.pem"); + #[cfg(feature = "logger")] + { + router.enable_default_logger(true); + } + router.mount( + "/", + windmark::success!({ + log::info!("Hello!"); + + "Hello!" + }), + ); + + router.run().await +} diff --git a/examples/empty.rs b/examples/empty.rs new file mode 100644 index 0000000..7653ec5 --- /dev/null +++ b/examples/empty.rs @@ -0,0 +1,28 @@ +// This file is part of Windmark <https://github.com/gemrest/windmark>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +//! `cargo run --example empty` + +#[windmark::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + windmark::Router::new() + .set_private_key_file("windmark_private.pem") + .set_certificate_file("windmark_public.pem") + .run() + .await +} diff --git a/examples/error_handler.rs b/examples/error_handler.rs new file mode 100644 index 0000000..5b6ddf6 --- /dev/null +++ b/examples/error_handler.rs @@ -0,0 +1,44 @@ +// This file is part of Windmark <https://github.com/gemrest/windmark>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +//! `cargo run --example error_handler` + +use windmark::Response; + +#[windmark::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + let mut error_count = 0; + + windmark::Router::new() + .set_private_key_file("windmark_private.pem") + .set_certificate_file("windmark_public.pem") + .set_error_handler(move |_| { + error_count += 1; + + println!("{} errors so far", error_count); + + Response::permanent_failure("e") + }) + .mount("/error", |_| { + let nothing = None::<String>; + + Response::success(nothing.unwrap()) + }) + .run() + .await +} diff --git a/examples/fix_path.rs b/examples/fix_path.rs new file mode 100644 index 0000000..4b5294b --- /dev/null +++ b/examples/fix_path.rs @@ -0,0 +1,33 @@ +// This file is part of Windmark <https://github.com/gemrest/windmark>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +//! `cargo run --example fix_path` + +#[windmark::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + windmark::Router::new() + .set_private_key_file("windmark_private.pem") + .set_certificate_file("windmark_public.pem") + .set_fix_path(true) + .mount( + "/close", + windmark::success!("Visit '/close/'; you should be close enough!"), + ) + .run() + .await +} diff --git a/examples/input.rs b/examples/input.rs new file mode 100644 index 0000000..de708ca --- /dev/null +++ b/examples/input.rs @@ -0,0 +1,44 @@ +// This file is part of Windmark <https://github.com/gemrest/windmark>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +//! `cargo run --example input` + +use windmark::{context::RouteContext, Response}; + +#[windmark::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + windmark::Router::new() + .set_private_key_file("windmark_private.pem") + .set_certificate_file("windmark_public.pem") + .mount("/input", |context: RouteContext| { + if let Some(name) = context.url.query() { + Response::success(format!("Your name is {}!", name)) + } else { + Response::input("What is your name?") + } + }) + .mount("/sensitive", |context: RouteContext| { + if let Some(password) = context.url.query() { + Response::success(format!("Your password is {}!", password)) + } else { + Response::sensitive_input("What is your password?") + } + }) + .run() + .await +} diff --git a/examples/mime.rs b/examples/mime.rs new file mode 100644 index 0000000..b5127a3 --- /dev/null +++ b/examples/mime.rs @@ -0,0 +1,33 @@ +// This file is part of Windmark <https://github.com/gemrest/windmark>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +//! `cargo run --example mime` + +#[windmark::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + windmark::Router::new() + .set_private_key_file("windmark_private.pem") + .set_certificate_file("windmark_public.pem") + .mount("/mime", |_| { + windmark::Response::success("Hello!".to_string()) + .with_mime("text/plain") + .clone() + }) + .run() + .await +} diff --git a/examples/parameters.rs b/examples/parameters.rs new file mode 100644 index 0000000..50049fd --- /dev/null +++ b/examples/parameters.rs @@ -0,0 +1,51 @@ +// This file is part of Windmark <https://github.com/gemrest/windmark>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +//! `cargo run --example parameters` + +use windmark::success; + +#[windmark::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + windmark::Router::new() + .set_private_key_file("windmark_private.pem") + .set_certificate_file("windmark_public.pem") + .mount( + "/language/:language", + success!( + context, + format!( + "Your language of choice is {}.", + context.parameters.get("language").unwrap() + ) + ), + ) + .mount( + "/name/:first/:last", + success!( + context, + format!( + "Your name is {} {}.", + context.parameters.get("first").unwrap(), + context.parameters.get("last").unwrap() + ) + ), + ) + .run() + .await +} diff --git a/examples/partial.rs b/examples/partial.rs new file mode 100644 index 0000000..af08f8f --- /dev/null +++ b/examples/partial.rs @@ -0,0 +1,34 @@ +// This file is part of Windmark <https://github.com/gemrest/windmark>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +//! `cargo run --example partial` + +#[windmark::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + windmark::Router::new() + .set_private_key_file("windmark_private.pem") + .set_certificate_file("windmark_public.pem") + .add_header(|_| "This is fancy art.\n".to_string()) + .add_footer(|context: windmark::context::RouteContext| { + format!("\nYou came from '{}'.", context.url.path()) + }) + .add_footer(|_| "\nCopyright (C) 2022".to_string()) + .mount("/", windmark::success!("Hello!")) + .run() + .await +} diff --git a/examples/query.rs b/examples/query.rs new file mode 100644 index 0000000..ac2c047 --- /dev/null +++ b/examples/query.rs @@ -0,0 +1,38 @@ +// This file is part of Windmark <https://github.com/gemrest/windmark>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +//! `cargo run --example input` + +#[windmark::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + windmark::Router::new() + .set_private_key_file("windmark_private.pem") + .set_certificate_file("windmark_public.pem") + .mount( + "/query", + windmark::success!( + context, + format!( + "You provided the following queries: '{:?}'", + windmark::utilities::queries_from_url(&context.url) + ) + ), + ) + .run() + .await +} diff --git a/examples/responses.rs b/examples/responses.rs new file mode 100644 index 0000000..3cdc3a8 --- /dev/null +++ b/examples/responses.rs @@ -0,0 +1,49 @@ +// This file is part of Windmark <https://github.com/gemrest/windmark>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +//! `cargo run --example responses` + +use windmark::success; + +#[windmark::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + windmark::Router::new() + .set_private_key_file("windmark_private.pem") + .set_certificate_file("windmark_public.pem") + .mount( + "/", + success!( + "# Index\n\nWelcome!\n\n=> /test Test Page\n=> /time Unix Epoch" + ), + ) + .mount("/test", success!("This is a test page.\n=> / back")) + .mount( + "/failure", + windmark::temporary_failure!("Woops ... temporarily."), + ) + .mount( + "/time", + success!(std::time::UNIX_EPOCH.elapsed().unwrap().as_nanos()), + ) + .mount( + "/redirect", + windmark::permanent_redirect!("gemini://localhost/test"), + ) + .run() + .await +} diff --git a/examples/stateless_module.rs b/examples/stateless_module.rs new file mode 100644 index 0000000..5daa0e2 --- /dev/null +++ b/examples/stateless_module.rs @@ -0,0 +1,37 @@ +// This file is part of Windmark <https://github.com/gemrest/windmark>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +//! `cargo run --example stateless_module` + +use windmark::Router; + +fn smiley(_context: windmark::context::RouteContext) -> windmark::Response { + windmark::Response::success("😀") +} + +fn emojis(router: &mut Router) { router.mount("/smiley", smiley); } + +#[windmark::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + Router::new() + .set_private_key_file("windmark_private.pem") + .set_certificate_file("windmark_public.pem") + .attach_stateless(emojis) + .run() + .await +} diff --git a/examples/windmark.rs b/examples/windmark.rs deleted file mode 100644 index 9daeb9b..0000000 --- a/examples/windmark.rs +++ /dev/null @@ -1,219 +0,0 @@ -// This file is part of Windmark <https://github.com/gemrest/windmark>. -// Copyright (C) 2022-2022 Fuwn <[email protected]> -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, version 3. -// -// This program is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see <http://www.gnu.org/licenses/>. -// -// Copyright (C) 2022-2022 Fuwn <[email protected]> -// SPDX-License-Identifier: GPL-3.0-only - -//! `cargo run --example windmark --features logger` - -#[macro_use] -extern crate log; - -use windmark::{ - context::{HookContext, RouteContext}, - response::Response, - success, - Router, -}; - -#[derive(Default)] -struct Clicker { - clicks: isize, -} - -#[async_trait::async_trait] -impl windmark::AsyncModule for Clicker { - async fn on_attach(&mut self, _: &mut Router) { - println!("clicker has been attached!"); - } - - async fn on_pre_route(&mut self, context: HookContext) { - self.clicks += 1; - - info!( - "clicker has been called pre-route on {} with {} clicks!", - context.url.path(), - self.clicks - ); - } - - async fn on_post_route(&mut self, context: HookContext) { - info!( - "clicker has been called post-route on {} with {} clicks!", - context.url.path(), - self.clicks - ); - } -} - -#[windmark::main] -async fn main() -> Result<(), Box<dyn std::error::Error>> { - let mut error_count = 0; - let mut router = Router::new(); - let async_clicks = std::sync::Arc::new(tokio::sync::Mutex::new(0)); - - router.set_private_key_file("windmark_private.pem"); - router.set_certificate_file("windmark_public.pem"); - #[cfg(feature = "logger")] - router.enable_default_logger(true); - router.set_error_handler(move |_| { - error_count += 1; - - println!("{} errors so far", error_count); - - Response::permanent_failure("e") - }); - router.set_fix_path(true); - router.attach_stateless(|r| { - r.mount("/module", success!("This is a module!")); - }); - router.attach_async(Clicker::default()); - router.set_pre_route_callback(|context| { - info!( - "accepted connection from {} to {}", - context.peer_address.unwrap().ip(), - context.url.to_string() - ) - }); - router.set_post_route_callback(|context, content| { - content.content = - content.content.replace("Welcome!", "Welcome to Windmark!"); - - info!( - "closed connection from {}", - context.peer_address.unwrap().ip() - ) - }); - router.add_header(|_| "```\nART IS COOL\n```\nhi".to_string()); - router.add_footer(|_| "Copyright 2022".to_string()); - router.add_footer(|context: RouteContext| { - format!("Another footer, but lower! (from {})", context.url.path()) - }); - router.mount( - "/", - success!("# INDEX\n\nWelcome!\n\n=> /test Test Page\n=> /time Unix Epoch"), - ); - router.mount("/specific-mime", |_| { - Response::success("hi".to_string()) - .with_mime("text/plain") - .clone() - }); - router.mount("/test", success!("hi there\n=> / back")); - router.mount( - "/temporary-failure", - windmark::temporary_failure!("Woops, temporarily..."), - ); - router.mount( - "/time", - success!(std::time::UNIX_EPOCH.elapsed().unwrap().as_nanos()), - ); - router.mount( - "/query", - success!( - context, - format!( - "queries: {:?}", - windmark::utilities::queries_from_url(&context.url) - ) - ), - ); - router.mount( - "/param/:lang", - success!( - context, - format!( - "Parameter lang is {}", - context.parameters.get("lang").unwrap() - ) - ), - ); - router.mount( - "/names/:first/:last", - success!( - context, - format!( - "{} {}", - context.parameters.get("first").unwrap(), - context.parameters.get("last").unwrap() - ) - ), - ); - router.mount("/input", |context: RouteContext| { - if let Some(name) = context.url.query() { - Response::success(format!("Your name is {}!", name)) - } else { - Response::input("What is your name?") - } - }); - router.mount("/sensitive-input", |context: RouteContext| { - if let Some(password) = context.url.query() { - Response::success(format!("Your password is {}!", password)) - } else { - Response::sensitive_input("What is your password?") - } - }); - router.mount("/error", windmark::certificate_not_valid!("no")); - router.mount( - "/redirect", - windmark::permanent_redirect!("gemini://localhost/test"), - ); - #[cfg(feature = "auto-deduce-mime")] - router.mount("/auto-file", { - windmark::binary_success!(include_bytes!("../LICENSE")) - }); - router.mount("/file", { - windmark::binary_success!(include_bytes!("../LICENSE"), "text/plain") - }); - router.mount("/string-file", { - windmark::binary_success!("hi", "text/plain") - }); - router.mount("/secret", |context: RouteContext| { - if let Some(certificate) = context.certificate { - Response::success(format!("Your public key: {}.", { - (|| -> Result<String, openssl::error::ErrorStack> { - Ok(format!( - "{:?}", - certificate.public_key()?.rsa()?.public_key_to_pem()? - )) - })() - .unwrap_or_else(|_| "Unknown".to_string()) - },)) - } else { - Response::client_certificate_required( - "This is a secret route! Identify yourself!", - ) - } - }); - router.mount("/async", move |_| { - let async_clicks = async_clicks.clone(); - - async move { - let mut clicks = async_clicks.lock().await; - - *clicks += 1; - - Response::success(*clicks) - } - }); - router.mount("/async-nothing", |context| { - async move { Response::success(context.url.path()) } - }); - router.mount( - "/async-macro", - windmark::success_async!(async { "hi" }.await), - ); - - router.run().await -} |