···11+// This file is part of Windmark <https://github.com/gemrest/windmark>.
22+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
33+//
44+// This program is free software: you can redistribute it and/or modify
55+// it under the terms of the GNU General Public License as published by
66+// the Free Software Foundation, version 3.
77+//
88+// This program is distributed in the hope that it will be useful, but
99+// WITHOUT ANY WARRANTY; without even the implied warranty of
1010+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111+// General Public License for more details.
1212+//
1313+// You should have received a copy of the GNU General Public License
1414+// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515+//
1616+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717+// SPDX-License-Identifier: GPL-3.0-only
1818+1919+//! `cargo run --example async`
2020+2121+#[windmark::main]
2222+async fn main() -> Result<(), Box<dyn std::error::Error>> {
2323+ let mut router = windmark::Router::new();
2424+ let async_clicks = std::sync::Arc::new(tokio::sync::Mutex::new(0));
2525+2626+ router.set_private_key_file("windmark_private.pem");
2727+ router.set_certificate_file("windmark_public.pem");
2828+ router.mount("/clicks", move |_| {
2929+ let async_clicks = async_clicks.clone();
3030+3131+ async move {
3232+ let mut clicks = async_clicks.lock().await;
3333+3434+ *clicks += 1;
3535+3636+ windmark::Response::success(*clicks)
3737+ }
3838+ });
3939+ router.mount(
4040+ "/macro",
4141+ windmark::success_async!(
4242+ async { "This response was sent using an asynchronous macro." }.await
4343+ ),
4444+ );
4545+4646+ router.run().await
4747+}
+70
examples/async_stateful_module.rs
···11+// This file is part of Windmark <https://github.com/gemrest/windmark>.
22+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
33+//
44+// This program is free software: you can redistribute it and/or modify
55+// it under the terms of the GNU General Public License as published by
66+// the Free Software Foundation, version 3.
77+//
88+// This program is distributed in the hope that it will be useful, but
99+// WITHOUT ANY WARRANTY; without even the implied warranty of
1010+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111+// General Public License for more details.
1212+//
1313+// You should have received a copy of the GNU General Public License
1414+// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515+//
1616+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717+// SPDX-License-Identifier: GPL-3.0-only
1818+1919+//! `cargo run --example async_stateful_module`
2020+2121+use std::sync::{Arc, Mutex};
2222+2323+use windmark::{context::HookContext, Router};
2424+2525+#[derive(Default)]
2626+struct Clicker {
2727+ clicks: Arc<Mutex<usize>>,
2828+}
2929+3030+#[async_trait::async_trait]
3131+impl windmark::AsyncModule for Clicker {
3232+ async fn on_attach(&mut self, _router: &mut Router) {
3333+ println!("module 'clicker' has been attached!");
3434+ }
3535+3636+ async fn on_pre_route(&mut self, context: HookContext) {
3737+ *self.clicks.lock().unwrap() += 1;
3838+3939+ println!(
4040+ "module 'clicker' has been called before the route '{}' with {} clicks!",
4141+ context.url.path(),
4242+ self.clicks.lock().unwrap()
4343+ );
4444+ }
4545+4646+ async fn on_post_route(&mut self, context: HookContext) {
4747+ println!(
4848+ "module 'clicker' clicker has been called after the route '{}' with {} \
4949+ clicks!",
5050+ context.url.path(),
5151+ self.clicks.lock().unwrap()
5252+ );
5353+ }
5454+}
5555+5656+#[windmark::main]
5757+async fn main() -> Result<(), Box<dyn std::error::Error>> {
5858+ let mut router = Router::new();
5959+6060+ router.set_private_key_file("windmark_private.pem");
6161+ router.set_certificate_file("windmark_public.pem");
6262+ #[cfg(feature = "logger")]
6363+ {
6464+ router.enable_default_logger(true);
6565+ }
6666+ router.attach_async(Clicker::default());
6767+ router.mount("/", windmark::success!("Hello!"));
6868+6969+ router.run().await
7070+}
+42
examples/binary.rs
···11+// This file is part of Windmark <https://github.com/gemrest/windmark>.
22+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
33+//
44+// This program is free software: you can redistribute it and/or modify
55+// it under the terms of the GNU General Public License as published by
66+// the Free Software Foundation, version 3.
77+//
88+// This program is distributed in the hope that it will be useful, but
99+// WITHOUT ANY WARRANTY; without even the implied warranty of
1010+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111+// General Public License for more details.
1212+//
1313+// You should have received a copy of the GNU General Public License
1414+// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515+//
1616+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717+// SPDX-License-Identifier: GPL-3.0-only
1818+1919+//! `cargo run --example binary`
2020+//!
2121+//! Optionally, you can run this example with the `auto-deduce-mime` feature
2222+//! enabled.
2323+2424+#[windmark::main]
2525+async fn main() -> Result<(), Box<dyn std::error::Error>> {
2626+ let mut router = windmark::Router::new();
2727+2828+ router.set_private_key_file("windmark_private.pem");
2929+ router.set_certificate_file("windmark_public.pem");
3030+ #[cfg(feature = "auto-deduce-mime")]
3131+ router.mount("/automatic", {
3232+ windmark::binary_success!(include_bytes!("../LICENSE"))
3333+ });
3434+ router.mount("/specific", {
3535+ windmark::binary_success!(include_bytes!("../LICENSE"), "text/plain")
3636+ });
3737+ router.mount("/direct", {
3838+ windmark::binary_success!("This is a string.", "text/plain")
3939+ });
4040+4141+ router.run().await
4242+}
+44
examples/callbacks.rs
···11+// This file is part of Windmark <https://github.com/gemrest/windmark>.
22+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
33+//
44+// This program is free software: you can redistribute it and/or modify
55+// it under the terms of the GNU General Public License as published by
66+// the Free Software Foundation, version 3.
77+//
88+// This program is distributed in the hope that it will be useful, but
99+// WITHOUT ANY WARRANTY; without even the implied warranty of
1010+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111+// General Public License for more details.
1212+//
1313+// You should have received a copy of the GNU General Public License
1414+// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515+//
1616+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717+// SPDX-License-Identifier: GPL-3.0-only
1818+1919+//! `cargo run --example callbacks`
2020+2121+#[windmark::main]
2222+async fn main() -> Result<(), Box<dyn std::error::Error>> {
2323+ windmark::Router::new()
2424+ .set_private_key_file("windmark_private.pem")
2525+ .set_certificate_file("windmark_public.pem")
2626+ .mount("/", windmark::success!("Hello!"))
2727+ .set_pre_route_callback(|context| {
2828+ println!(
2929+ "accepted connection from {} to {}",
3030+ context.peer_address.unwrap().ip(),
3131+ context.url.to_string()
3232+ )
3333+ })
3434+ .set_post_route_callback(|context, content| {
3535+ content.content = content.content.replace("Hello", "Hi");
3636+3737+ println!(
3838+ "closed connection from {}",
3939+ context.peer_address.unwrap().ip()
4040+ )
4141+ })
4242+ .run()
4343+ .await
4444+}
+53
examples/certificate.rs
···11+// This file is part of Windmark <https://github.com/gemrest/windmark>.
22+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
33+//
44+// This program is free software: you can redistribute it and/or modify
55+// it under the terms of the GNU General Public License as published by
66+// the Free Software Foundation, version 3.
77+//
88+// This program is distributed in the hope that it will be useful, but
99+// WITHOUT ANY WARRANTY; without even the implied warranty of
1010+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111+// General Public License for more details.
1212+//
1313+// You should have received a copy of the GNU General Public License
1414+// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515+//
1616+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717+// SPDX-License-Identifier: GPL-3.0-only
1818+1919+//! `cargo run --example certificate`
2020+2121+use windmark::Response;
2222+2323+#[windmark::main]
2424+async fn main() -> Result<(), Box<dyn std::error::Error>> {
2525+ windmark::Router::new()
2626+ .set_private_key_file("windmark_private.pem")
2727+ .set_certificate_file("windmark_public.pem")
2828+ .mount("/secret", |context: windmark::context::RouteContext| {
2929+ if let Some(certificate) = context.certificate {
3030+ Response::success(format!("Your public key is '{}'.", {
3131+ (|| -> Result<String, openssl::error::ErrorStack> {
3232+ Ok(format!(
3333+ "{:?}",
3434+ certificate.public_key()?.rsa()?.public_key_to_pem()?
3535+ ))
3636+ })()
3737+ .unwrap_or_else(|_| {
3838+ "An error occurred while reading your public key.".to_string()
3939+ })
4040+ }))
4141+ } else {
4242+ Response::client_certificate_required(
4343+ "This is a secret route ... Identify yourself!",
4444+ )
4545+ }
4646+ })
4747+ .mount(
4848+ "/invalid",
4949+ windmark::certificate_not_valid!("Your certificate is invalid."),
5050+ )
5151+ .run()
5252+ .await
5353+}
+41
examples/default_logger.rs
···11+// This file is part of Windmark <https://github.com/gemrest/windmark>.
22+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
33+//
44+// This program is free software: you can redistribute it and/or modify
55+// it under the terms of the GNU General Public License as published by
66+// the Free Software Foundation, version 3.
77+//
88+// This program is distributed in the hope that it will be useful, but
99+// WITHOUT ANY WARRANTY; without even the implied warranty of
1010+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111+// General Public License for more details.
1212+//
1313+// You should have received a copy of the GNU General Public License
1414+// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515+//
1616+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717+// SPDX-License-Identifier: GPL-3.0-only
1818+1919+//! `cargo run --example default_logger --features logger`
2020+2121+#[windmark::main]
2222+async fn main() -> Result<(), Box<dyn std::error::Error>> {
2323+ let mut router = windmark::Router::new();
2424+2525+ router.set_private_key_file("windmark_private.pem");
2626+ router.set_certificate_file("windmark_public.pem");
2727+ #[cfg(feature = "logger")]
2828+ {
2929+ router.enable_default_logger(true);
3030+ }
3131+ router.mount(
3232+ "/",
3333+ windmark::success!({
3434+ log::info!("Hello!");
3535+3636+ "Hello!"
3737+ }),
3838+ );
3939+4040+ router.run().await
4141+}
+28
examples/empty.rs
···11+// This file is part of Windmark <https://github.com/gemrest/windmark>.
22+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
33+//
44+// This program is free software: you can redistribute it and/or modify
55+// it under the terms of the GNU General Public License as published by
66+// the Free Software Foundation, version 3.
77+//
88+// This program is distributed in the hope that it will be useful, but
99+// WITHOUT ANY WARRANTY; without even the implied warranty of
1010+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111+// General Public License for more details.
1212+//
1313+// You should have received a copy of the GNU General Public License
1414+// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515+//
1616+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717+// SPDX-License-Identifier: GPL-3.0-only
1818+1919+//! `cargo run --example empty`
2020+2121+#[windmark::main]
2222+async fn main() -> Result<(), Box<dyn std::error::Error>> {
2323+ windmark::Router::new()
2424+ .set_private_key_file("windmark_private.pem")
2525+ .set_certificate_file("windmark_public.pem")
2626+ .run()
2727+ .await
2828+}
+44
examples/error_handler.rs
···11+// This file is part of Windmark <https://github.com/gemrest/windmark>.
22+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
33+//
44+// This program is free software: you can redistribute it and/or modify
55+// it under the terms of the GNU General Public License as published by
66+// the Free Software Foundation, version 3.
77+//
88+// This program is distributed in the hope that it will be useful, but
99+// WITHOUT ANY WARRANTY; without even the implied warranty of
1010+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111+// General Public License for more details.
1212+//
1313+// You should have received a copy of the GNU General Public License
1414+// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515+//
1616+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717+// SPDX-License-Identifier: GPL-3.0-only
1818+1919+//! `cargo run --example error_handler`
2020+2121+use windmark::Response;
2222+2323+#[windmark::main]
2424+async fn main() -> Result<(), Box<dyn std::error::Error>> {
2525+ let mut error_count = 0;
2626+2727+ windmark::Router::new()
2828+ .set_private_key_file("windmark_private.pem")
2929+ .set_certificate_file("windmark_public.pem")
3030+ .set_error_handler(move |_| {
3131+ error_count += 1;
3232+3333+ println!("{} errors so far", error_count);
3434+3535+ Response::permanent_failure("e")
3636+ })
3737+ .mount("/error", |_| {
3838+ let nothing = None::<String>;
3939+4040+ Response::success(nothing.unwrap())
4141+ })
4242+ .run()
4343+ .await
4444+}
+33
examples/fix_path.rs
···11+// This file is part of Windmark <https://github.com/gemrest/windmark>.
22+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
33+//
44+// This program is free software: you can redistribute it and/or modify
55+// it under the terms of the GNU General Public License as published by
66+// the Free Software Foundation, version 3.
77+//
88+// This program is distributed in the hope that it will be useful, but
99+// WITHOUT ANY WARRANTY; without even the implied warranty of
1010+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111+// General Public License for more details.
1212+//
1313+// You should have received a copy of the GNU General Public License
1414+// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515+//
1616+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717+// SPDX-License-Identifier: GPL-3.0-only
1818+1919+//! `cargo run --example fix_path`
2020+2121+#[windmark::main]
2222+async fn main() -> Result<(), Box<dyn std::error::Error>> {
2323+ windmark::Router::new()
2424+ .set_private_key_file("windmark_private.pem")
2525+ .set_certificate_file("windmark_public.pem")
2626+ .set_fix_path(true)
2727+ .mount(
2828+ "/close",
2929+ windmark::success!("Visit '/close/'; you should be close enough!"),
3030+ )
3131+ .run()
3232+ .await
3333+}
+44
examples/input.rs
···11+// This file is part of Windmark <https://github.com/gemrest/windmark>.
22+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
33+//
44+// This program is free software: you can redistribute it and/or modify
55+// it under the terms of the GNU General Public License as published by
66+// the Free Software Foundation, version 3.
77+//
88+// This program is distributed in the hope that it will be useful, but
99+// WITHOUT ANY WARRANTY; without even the implied warranty of
1010+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111+// General Public License for more details.
1212+//
1313+// You should have received a copy of the GNU General Public License
1414+// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515+//
1616+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717+// SPDX-License-Identifier: GPL-3.0-only
1818+1919+//! `cargo run --example input`
2020+2121+use windmark::{context::RouteContext, Response};
2222+2323+#[windmark::main]
2424+async fn main() -> Result<(), Box<dyn std::error::Error>> {
2525+ windmark::Router::new()
2626+ .set_private_key_file("windmark_private.pem")
2727+ .set_certificate_file("windmark_public.pem")
2828+ .mount("/input", |context: RouteContext| {
2929+ if let Some(name) = context.url.query() {
3030+ Response::success(format!("Your name is {}!", name))
3131+ } else {
3232+ Response::input("What is your name?")
3333+ }
3434+ })
3535+ .mount("/sensitive", |context: RouteContext| {
3636+ if let Some(password) = context.url.query() {
3737+ Response::success(format!("Your password is {}!", password))
3838+ } else {
3939+ Response::sensitive_input("What is your password?")
4040+ }
4141+ })
4242+ .run()
4343+ .await
4444+}
+33
examples/mime.rs
···11+// This file is part of Windmark <https://github.com/gemrest/windmark>.
22+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
33+//
44+// This program is free software: you can redistribute it and/or modify
55+// it under the terms of the GNU General Public License as published by
66+// the Free Software Foundation, version 3.
77+//
88+// This program is distributed in the hope that it will be useful, but
99+// WITHOUT ANY WARRANTY; without even the implied warranty of
1010+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111+// General Public License for more details.
1212+//
1313+// You should have received a copy of the GNU General Public License
1414+// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515+//
1616+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717+// SPDX-License-Identifier: GPL-3.0-only
1818+1919+//! `cargo run --example mime`
2020+2121+#[windmark::main]
2222+async fn main() -> Result<(), Box<dyn std::error::Error>> {
2323+ windmark::Router::new()
2424+ .set_private_key_file("windmark_private.pem")
2525+ .set_certificate_file("windmark_public.pem")
2626+ .mount("/mime", |_| {
2727+ windmark::Response::success("Hello!".to_string())
2828+ .with_mime("text/plain")
2929+ .clone()
3030+ })
3131+ .run()
3232+ .await
3333+}
+51
examples/parameters.rs
···11+// This file is part of Windmark <https://github.com/gemrest/windmark>.
22+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
33+//
44+// This program is free software: you can redistribute it and/or modify
55+// it under the terms of the GNU General Public License as published by
66+// the Free Software Foundation, version 3.
77+//
88+// This program is distributed in the hope that it will be useful, but
99+// WITHOUT ANY WARRANTY; without even the implied warranty of
1010+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111+// General Public License for more details.
1212+//
1313+// You should have received a copy of the GNU General Public License
1414+// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515+//
1616+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717+// SPDX-License-Identifier: GPL-3.0-only
1818+1919+//! `cargo run --example parameters`
2020+2121+use windmark::success;
2222+2323+#[windmark::main]
2424+async fn main() -> Result<(), Box<dyn std::error::Error>> {
2525+ windmark::Router::new()
2626+ .set_private_key_file("windmark_private.pem")
2727+ .set_certificate_file("windmark_public.pem")
2828+ .mount(
2929+ "/language/:language",
3030+ success!(
3131+ context,
3232+ format!(
3333+ "Your language of choice is {}.",
3434+ context.parameters.get("language").unwrap()
3535+ )
3636+ ),
3737+ )
3838+ .mount(
3939+ "/name/:first/:last",
4040+ success!(
4141+ context,
4242+ format!(
4343+ "Your name is {} {}.",
4444+ context.parameters.get("first").unwrap(),
4545+ context.parameters.get("last").unwrap()
4646+ )
4747+ ),
4848+ )
4949+ .run()
5050+ .await
5151+}
+34
examples/partial.rs
···11+// This file is part of Windmark <https://github.com/gemrest/windmark>.
22+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
33+//
44+// This program is free software: you can redistribute it and/or modify
55+// it under the terms of the GNU General Public License as published by
66+// the Free Software Foundation, version 3.
77+//
88+// This program is distributed in the hope that it will be useful, but
99+// WITHOUT ANY WARRANTY; without even the implied warranty of
1010+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111+// General Public License for more details.
1212+//
1313+// You should have received a copy of the GNU General Public License
1414+// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515+//
1616+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717+// SPDX-License-Identifier: GPL-3.0-only
1818+1919+//! `cargo run --example partial`
2020+2121+#[windmark::main]
2222+async fn main() -> Result<(), Box<dyn std::error::Error>> {
2323+ windmark::Router::new()
2424+ .set_private_key_file("windmark_private.pem")
2525+ .set_certificate_file("windmark_public.pem")
2626+ .add_header(|_| "This is fancy art.\n".to_string())
2727+ .add_footer(|context: windmark::context::RouteContext| {
2828+ format!("\nYou came from '{}'.", context.url.path())
2929+ })
3030+ .add_footer(|_| "\nCopyright (C) 2022".to_string())
3131+ .mount("/", windmark::success!("Hello!"))
3232+ .run()
3333+ .await
3434+}
+38
examples/query.rs
···11+// This file is part of Windmark <https://github.com/gemrest/windmark>.
22+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
33+//
44+// This program is free software: you can redistribute it and/or modify
55+// it under the terms of the GNU General Public License as published by
66+// the Free Software Foundation, version 3.
77+//
88+// This program is distributed in the hope that it will be useful, but
99+// WITHOUT ANY WARRANTY; without even the implied warranty of
1010+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111+// General Public License for more details.
1212+//
1313+// You should have received a copy of the GNU General Public License
1414+// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515+//
1616+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717+// SPDX-License-Identifier: GPL-3.0-only
1818+1919+//! `cargo run --example input`
2020+2121+#[windmark::main]
2222+async fn main() -> Result<(), Box<dyn std::error::Error>> {
2323+ windmark::Router::new()
2424+ .set_private_key_file("windmark_private.pem")
2525+ .set_certificate_file("windmark_public.pem")
2626+ .mount(
2727+ "/query",
2828+ windmark::success!(
2929+ context,
3030+ format!(
3131+ "You provided the following queries: '{:?}'",
3232+ windmark::utilities::queries_from_url(&context.url)
3333+ )
3434+ ),
3535+ )
3636+ .run()
3737+ .await
3838+}
+49
examples/responses.rs
···11+// This file is part of Windmark <https://github.com/gemrest/windmark>.
22+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
33+//
44+// This program is free software: you can redistribute it and/or modify
55+// it under the terms of the GNU General Public License as published by
66+// the Free Software Foundation, version 3.
77+//
88+// This program is distributed in the hope that it will be useful, but
99+// WITHOUT ANY WARRANTY; without even the implied warranty of
1010+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111+// General Public License for more details.
1212+//
1313+// You should have received a copy of the GNU General Public License
1414+// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515+//
1616+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717+// SPDX-License-Identifier: GPL-3.0-only
1818+1919+//! `cargo run --example responses`
2020+2121+use windmark::success;
2222+2323+#[windmark::main]
2424+async fn main() -> Result<(), Box<dyn std::error::Error>> {
2525+ windmark::Router::new()
2626+ .set_private_key_file("windmark_private.pem")
2727+ .set_certificate_file("windmark_public.pem")
2828+ .mount(
2929+ "/",
3030+ success!(
3131+ "# Index\n\nWelcome!\n\n=> /test Test Page\n=> /time Unix Epoch"
3232+ ),
3333+ )
3434+ .mount("/test", success!("This is a test page.\n=> / back"))
3535+ .mount(
3636+ "/failure",
3737+ windmark::temporary_failure!("Woops ... temporarily."),
3838+ )
3939+ .mount(
4040+ "/time",
4141+ success!(std::time::UNIX_EPOCH.elapsed().unwrap().as_nanos()),
4242+ )
4343+ .mount(
4444+ "/redirect",
4545+ windmark::permanent_redirect!("gemini://localhost/test"),
4646+ )
4747+ .run()
4848+ .await
4949+}
+37
examples/stateless_module.rs
···11+// This file is part of Windmark <https://github.com/gemrest/windmark>.
22+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
33+//
44+// This program is free software: you can redistribute it and/or modify
55+// it under the terms of the GNU General Public License as published by
66+// the Free Software Foundation, version 3.
77+//
88+// This program is distributed in the hope that it will be useful, but
99+// WITHOUT ANY WARRANTY; without even the implied warranty of
1010+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111+// General Public License for more details.
1212+//
1313+// You should have received a copy of the GNU General Public License
1414+// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515+//
1616+// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717+// SPDX-License-Identifier: GPL-3.0-only
1818+1919+//! `cargo run --example stateless_module`
2020+2121+use windmark::Router;
2222+2323+fn smiley(_context: windmark::context::RouteContext) -> windmark::Response {
2424+ windmark::Response::success("😀")
2525+}
2626+2727+fn emojis(router: &mut Router) { router.mount("/smiley", smiley); }
2828+2929+#[windmark::main]
3030+async fn main() -> Result<(), Box<dyn std::error::Error>> {
3131+ Router::new()
3232+ .set_private_key_file("windmark_private.pem")
3333+ .set_certificate_file("windmark_public.pem")
3434+ .attach_stateless(emojis)
3535+ .run()
3636+ .await
3737+}
-219
examples/windmark.rs
···11-// This file is part of Windmark <https://github.com/gemrest/windmark>.
22-// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
33-//
44-// This program is free software: you can redistribute it and/or modify
55-// it under the terms of the GNU General Public License as published by
66-// the Free Software Foundation, version 3.
77-//
88-// This program is distributed in the hope that it will be useful, but
99-// WITHOUT ANY WARRANTY; without even the implied warranty of
1010-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1111-// General Public License for more details.
1212-//
1313-// You should have received a copy of the GNU General Public License
1414-// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515-//
1616-// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717-// SPDX-License-Identifier: GPL-3.0-only
1818-1919-//! `cargo run --example windmark --features logger`
2020-2121-#[macro_use]
2222-extern crate log;
2323-2424-use windmark::{
2525- context::{HookContext, RouteContext},
2626- response::Response,
2727- success,
2828- Router,
2929-};
3030-3131-#[derive(Default)]
3232-struct Clicker {
3333- clicks: isize,
3434-}
3535-3636-#[async_trait::async_trait]
3737-impl windmark::AsyncModule for Clicker {
3838- async fn on_attach(&mut self, _: &mut Router) {
3939- println!("clicker has been attached!");
4040- }
4141-4242- async fn on_pre_route(&mut self, context: HookContext) {
4343- self.clicks += 1;
4444-4545- info!(
4646- "clicker has been called pre-route on {} with {} clicks!",
4747- context.url.path(),
4848- self.clicks
4949- );
5050- }
5151-5252- async fn on_post_route(&mut self, context: HookContext) {
5353- info!(
5454- "clicker has been called post-route on {} with {} clicks!",
5555- context.url.path(),
5656- self.clicks
5757- );
5858- }
5959-}
6060-6161-#[windmark::main]
6262-async fn main() -> Result<(), Box<dyn std::error::Error>> {
6363- let mut error_count = 0;
6464- let mut router = Router::new();
6565- let async_clicks = std::sync::Arc::new(tokio::sync::Mutex::new(0));
6666-6767- router.set_private_key_file("windmark_private.pem");
6868- router.set_certificate_file("windmark_public.pem");
6969- #[cfg(feature = "logger")]
7070- router.enable_default_logger(true);
7171- router.set_error_handler(move |_| {
7272- error_count += 1;
7373-7474- println!("{} errors so far", error_count);
7575-7676- Response::permanent_failure("e")
7777- });
7878- router.set_fix_path(true);
7979- router.attach_stateless(|r| {
8080- r.mount("/module", success!("This is a module!"));
8181- });
8282- router.attach_async(Clicker::default());
8383- router.set_pre_route_callback(|context| {
8484- info!(
8585- "accepted connection from {} to {}",
8686- context.peer_address.unwrap().ip(),
8787- context.url.to_string()
8888- )
8989- });
9090- router.set_post_route_callback(|context, content| {
9191- content.content =
9292- content.content.replace("Welcome!", "Welcome to Windmark!");
9393-9494- info!(
9595- "closed connection from {}",
9696- context.peer_address.unwrap().ip()
9797- )
9898- });
9999- router.add_header(|_| "```\nART IS COOL\n```\nhi".to_string());
100100- router.add_footer(|_| "Copyright 2022".to_string());
101101- router.add_footer(|context: RouteContext| {
102102- format!("Another footer, but lower! (from {})", context.url.path())
103103- });
104104- router.mount(
105105- "/",
106106- success!("# INDEX\n\nWelcome!\n\n=> /test Test Page\n=> /time Unix Epoch"),
107107- );
108108- router.mount("/specific-mime", |_| {
109109- Response::success("hi".to_string())
110110- .with_mime("text/plain")
111111- .clone()
112112- });
113113- router.mount("/test", success!("hi there\n=> / back"));
114114- router.mount(
115115- "/temporary-failure",
116116- windmark::temporary_failure!("Woops, temporarily..."),
117117- );
118118- router.mount(
119119- "/time",
120120- success!(std::time::UNIX_EPOCH.elapsed().unwrap().as_nanos()),
121121- );
122122- router.mount(
123123- "/query",
124124- success!(
125125- context,
126126- format!(
127127- "queries: {:?}",
128128- windmark::utilities::queries_from_url(&context.url)
129129- )
130130- ),
131131- );
132132- router.mount(
133133- "/param/:lang",
134134- success!(
135135- context,
136136- format!(
137137- "Parameter lang is {}",
138138- context.parameters.get("lang").unwrap()
139139- )
140140- ),
141141- );
142142- router.mount(
143143- "/names/:first/:last",
144144- success!(
145145- context,
146146- format!(
147147- "{} {}",
148148- context.parameters.get("first").unwrap(),
149149- context.parameters.get("last").unwrap()
150150- )
151151- ),
152152- );
153153- router.mount("/input", |context: RouteContext| {
154154- if let Some(name) = context.url.query() {
155155- Response::success(format!("Your name is {}!", name))
156156- } else {
157157- Response::input("What is your name?")
158158- }
159159- });
160160- router.mount("/sensitive-input", |context: RouteContext| {
161161- if let Some(password) = context.url.query() {
162162- Response::success(format!("Your password is {}!", password))
163163- } else {
164164- Response::sensitive_input("What is your password?")
165165- }
166166- });
167167- router.mount("/error", windmark::certificate_not_valid!("no"));
168168- router.mount(
169169- "/redirect",
170170- windmark::permanent_redirect!("gemini://localhost/test"),
171171- );
172172- #[cfg(feature = "auto-deduce-mime")]
173173- router.mount("/auto-file", {
174174- windmark::binary_success!(include_bytes!("../LICENSE"))
175175- });
176176- router.mount("/file", {
177177- windmark::binary_success!(include_bytes!("../LICENSE"), "text/plain")
178178- });
179179- router.mount("/string-file", {
180180- windmark::binary_success!("hi", "text/plain")
181181- });
182182- router.mount("/secret", |context: RouteContext| {
183183- if let Some(certificate) = context.certificate {
184184- Response::success(format!("Your public key: {}.", {
185185- (|| -> Result<String, openssl::error::ErrorStack> {
186186- Ok(format!(
187187- "{:?}",
188188- certificate.public_key()?.rsa()?.public_key_to_pem()?
189189- ))
190190- })()
191191- .unwrap_or_else(|_| "Unknown".to_string())
192192- },))
193193- } else {
194194- Response::client_certificate_required(
195195- "This is a secret route! Identify yourself!",
196196- )
197197- }
198198- });
199199- router.mount("/async", move |_| {
200200- let async_clicks = async_clicks.clone();
201201-202202- async move {
203203- let mut clicks = async_clicks.lock().await;
204204-205205- *clicks += 1;
206206-207207- Response::success(*clicks)
208208- }
209209- });
210210- router.mount("/async-nothing", |context| {
211211- async move { Response::success(context.url.path()) }
212212- });
213213- router.mount(
214214- "/async-macro",
215215- windmark::success_async!(async { "hi" }.await),
216216- );
217217-218218- router.run().await
219219-}