🏗️ Elegant & Highly Performant Async Gemini Server Framework for the Modern Age
async framework gemini-protocol protocol gemini rust
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore(examples): seperate complex examples

Fuwn 9b47502b 9934455d

+688 -219
+47
examples/async.rs
··· 1 + // This file is part of Windmark <https://github.com/gemrest/windmark>. 2 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3 + // 4 + // This program is free software: you can redistribute it and/or modify 5 + // it under the terms of the GNU General Public License as published by 6 + // the Free Software Foundation, version 3. 7 + // 8 + // This program is distributed in the hope that it will be useful, but 9 + // WITHOUT ANY WARRANTY; without even the implied warranty of 10 + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 + // General Public License for more details. 12 + // 13 + // You should have received a copy of the GNU General Public License 14 + // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 + // 16 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 + // SPDX-License-Identifier: GPL-3.0-only 18 + 19 + //! `cargo run --example async` 20 + 21 + #[windmark::main] 22 + async fn main() -> Result<(), Box<dyn std::error::Error>> { 23 + let mut router = windmark::Router::new(); 24 + let async_clicks = std::sync::Arc::new(tokio::sync::Mutex::new(0)); 25 + 26 + router.set_private_key_file("windmark_private.pem"); 27 + router.set_certificate_file("windmark_public.pem"); 28 + router.mount("/clicks", move |_| { 29 + let async_clicks = async_clicks.clone(); 30 + 31 + async move { 32 + let mut clicks = async_clicks.lock().await; 33 + 34 + *clicks += 1; 35 + 36 + windmark::Response::success(*clicks) 37 + } 38 + }); 39 + router.mount( 40 + "/macro", 41 + windmark::success_async!( 42 + async { "This response was sent using an asynchronous macro." }.await 43 + ), 44 + ); 45 + 46 + router.run().await 47 + }
+70
examples/async_stateful_module.rs
··· 1 + // This file is part of Windmark <https://github.com/gemrest/windmark>. 2 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3 + // 4 + // This program is free software: you can redistribute it and/or modify 5 + // it under the terms of the GNU General Public License as published by 6 + // the Free Software Foundation, version 3. 7 + // 8 + // This program is distributed in the hope that it will be useful, but 9 + // WITHOUT ANY WARRANTY; without even the implied warranty of 10 + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 + // General Public License for more details. 12 + // 13 + // You should have received a copy of the GNU General Public License 14 + // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 + // 16 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 + // SPDX-License-Identifier: GPL-3.0-only 18 + 19 + //! `cargo run --example async_stateful_module` 20 + 21 + use std::sync::{Arc, Mutex}; 22 + 23 + use windmark::{context::HookContext, Router}; 24 + 25 + #[derive(Default)] 26 + struct Clicker { 27 + clicks: Arc<Mutex<usize>>, 28 + } 29 + 30 + #[async_trait::async_trait] 31 + impl windmark::AsyncModule for Clicker { 32 + async fn on_attach(&mut self, _router: &mut Router) { 33 + println!("module 'clicker' has been attached!"); 34 + } 35 + 36 + async fn on_pre_route(&mut self, context: HookContext) { 37 + *self.clicks.lock().unwrap() += 1; 38 + 39 + println!( 40 + "module 'clicker' has been called before the route '{}' with {} clicks!", 41 + context.url.path(), 42 + self.clicks.lock().unwrap() 43 + ); 44 + } 45 + 46 + async fn on_post_route(&mut self, context: HookContext) { 47 + println!( 48 + "module 'clicker' clicker has been called after the route '{}' with {} \ 49 + clicks!", 50 + context.url.path(), 51 + self.clicks.lock().unwrap() 52 + ); 53 + } 54 + } 55 + 56 + #[windmark::main] 57 + async fn main() -> Result<(), Box<dyn std::error::Error>> { 58 + let mut router = Router::new(); 59 + 60 + router.set_private_key_file("windmark_private.pem"); 61 + router.set_certificate_file("windmark_public.pem"); 62 + #[cfg(feature = "logger")] 63 + { 64 + router.enable_default_logger(true); 65 + } 66 + router.attach_async(Clicker::default()); 67 + router.mount("/", windmark::success!("Hello!")); 68 + 69 + router.run().await 70 + }
+42
examples/binary.rs
··· 1 + // This file is part of Windmark <https://github.com/gemrest/windmark>. 2 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3 + // 4 + // This program is free software: you can redistribute it and/or modify 5 + // it under the terms of the GNU General Public License as published by 6 + // the Free Software Foundation, version 3. 7 + // 8 + // This program is distributed in the hope that it will be useful, but 9 + // WITHOUT ANY WARRANTY; without even the implied warranty of 10 + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 + // General Public License for more details. 12 + // 13 + // You should have received a copy of the GNU General Public License 14 + // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 + // 16 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 + // SPDX-License-Identifier: GPL-3.0-only 18 + 19 + //! `cargo run --example binary` 20 + //! 21 + //! Optionally, you can run this example with the `auto-deduce-mime` feature 22 + //! enabled. 23 + 24 + #[windmark::main] 25 + async fn main() -> Result<(), Box<dyn std::error::Error>> { 26 + let mut router = windmark::Router::new(); 27 + 28 + router.set_private_key_file("windmark_private.pem"); 29 + router.set_certificate_file("windmark_public.pem"); 30 + #[cfg(feature = "auto-deduce-mime")] 31 + router.mount("/automatic", { 32 + windmark::binary_success!(include_bytes!("../LICENSE")) 33 + }); 34 + router.mount("/specific", { 35 + windmark::binary_success!(include_bytes!("../LICENSE"), "text/plain") 36 + }); 37 + router.mount("/direct", { 38 + windmark::binary_success!("This is a string.", "text/plain") 39 + }); 40 + 41 + router.run().await 42 + }
+44
examples/callbacks.rs
··· 1 + // This file is part of Windmark <https://github.com/gemrest/windmark>. 2 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3 + // 4 + // This program is free software: you can redistribute it and/or modify 5 + // it under the terms of the GNU General Public License as published by 6 + // the Free Software Foundation, version 3. 7 + // 8 + // This program is distributed in the hope that it will be useful, but 9 + // WITHOUT ANY WARRANTY; without even the implied warranty of 10 + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 + // General Public License for more details. 12 + // 13 + // You should have received a copy of the GNU General Public License 14 + // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 + // 16 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 + // SPDX-License-Identifier: GPL-3.0-only 18 + 19 + //! `cargo run --example callbacks` 20 + 21 + #[windmark::main] 22 + async fn main() -> Result<(), Box<dyn std::error::Error>> { 23 + windmark::Router::new() 24 + .set_private_key_file("windmark_private.pem") 25 + .set_certificate_file("windmark_public.pem") 26 + .mount("/", windmark::success!("Hello!")) 27 + .set_pre_route_callback(|context| { 28 + println!( 29 + "accepted connection from {} to {}", 30 + context.peer_address.unwrap().ip(), 31 + context.url.to_string() 32 + ) 33 + }) 34 + .set_post_route_callback(|context, content| { 35 + content.content = content.content.replace("Hello", "Hi"); 36 + 37 + println!( 38 + "closed connection from {}", 39 + context.peer_address.unwrap().ip() 40 + ) 41 + }) 42 + .run() 43 + .await 44 + }
+53
examples/certificate.rs
··· 1 + // This file is part of Windmark <https://github.com/gemrest/windmark>. 2 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3 + // 4 + // This program is free software: you can redistribute it and/or modify 5 + // it under the terms of the GNU General Public License as published by 6 + // the Free Software Foundation, version 3. 7 + // 8 + // This program is distributed in the hope that it will be useful, but 9 + // WITHOUT ANY WARRANTY; without even the implied warranty of 10 + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 + // General Public License for more details. 12 + // 13 + // You should have received a copy of the GNU General Public License 14 + // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 + // 16 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 + // SPDX-License-Identifier: GPL-3.0-only 18 + 19 + //! `cargo run --example certificate` 20 + 21 + use windmark::Response; 22 + 23 + #[windmark::main] 24 + async fn main() -> Result<(), Box<dyn std::error::Error>> { 25 + windmark::Router::new() 26 + .set_private_key_file("windmark_private.pem") 27 + .set_certificate_file("windmark_public.pem") 28 + .mount("/secret", |context: windmark::context::RouteContext| { 29 + if let Some(certificate) = context.certificate { 30 + Response::success(format!("Your public key is '{}'.", { 31 + (|| -> Result<String, openssl::error::ErrorStack> { 32 + Ok(format!( 33 + "{:?}", 34 + certificate.public_key()?.rsa()?.public_key_to_pem()? 35 + )) 36 + })() 37 + .unwrap_or_else(|_| { 38 + "An error occurred while reading your public key.".to_string() 39 + }) 40 + })) 41 + } else { 42 + Response::client_certificate_required( 43 + "This is a secret route ... Identify yourself!", 44 + ) 45 + } 46 + }) 47 + .mount( 48 + "/invalid", 49 + windmark::certificate_not_valid!("Your certificate is invalid."), 50 + ) 51 + .run() 52 + .await 53 + }
+41
examples/default_logger.rs
··· 1 + // This file is part of Windmark <https://github.com/gemrest/windmark>. 2 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3 + // 4 + // This program is free software: you can redistribute it and/or modify 5 + // it under the terms of the GNU General Public License as published by 6 + // the Free Software Foundation, version 3. 7 + // 8 + // This program is distributed in the hope that it will be useful, but 9 + // WITHOUT ANY WARRANTY; without even the implied warranty of 10 + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 + // General Public License for more details. 12 + // 13 + // You should have received a copy of the GNU General Public License 14 + // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 + // 16 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 + // SPDX-License-Identifier: GPL-3.0-only 18 + 19 + //! `cargo run --example default_logger --features logger` 20 + 21 + #[windmark::main] 22 + async fn main() -> Result<(), Box<dyn std::error::Error>> { 23 + let mut router = windmark::Router::new(); 24 + 25 + router.set_private_key_file("windmark_private.pem"); 26 + router.set_certificate_file("windmark_public.pem"); 27 + #[cfg(feature = "logger")] 28 + { 29 + router.enable_default_logger(true); 30 + } 31 + router.mount( 32 + "/", 33 + windmark::success!({ 34 + log::info!("Hello!"); 35 + 36 + "Hello!" 37 + }), 38 + ); 39 + 40 + router.run().await 41 + }
+28
examples/empty.rs
··· 1 + // This file is part of Windmark <https://github.com/gemrest/windmark>. 2 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3 + // 4 + // This program is free software: you can redistribute it and/or modify 5 + // it under the terms of the GNU General Public License as published by 6 + // the Free Software Foundation, version 3. 7 + // 8 + // This program is distributed in the hope that it will be useful, but 9 + // WITHOUT ANY WARRANTY; without even the implied warranty of 10 + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 + // General Public License for more details. 12 + // 13 + // You should have received a copy of the GNU General Public License 14 + // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 + // 16 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 + // SPDX-License-Identifier: GPL-3.0-only 18 + 19 + //! `cargo run --example empty` 20 + 21 + #[windmark::main] 22 + async fn main() -> Result<(), Box<dyn std::error::Error>> { 23 + windmark::Router::new() 24 + .set_private_key_file("windmark_private.pem") 25 + .set_certificate_file("windmark_public.pem") 26 + .run() 27 + .await 28 + }
+44
examples/error_handler.rs
··· 1 + // This file is part of Windmark <https://github.com/gemrest/windmark>. 2 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3 + // 4 + // This program is free software: you can redistribute it and/or modify 5 + // it under the terms of the GNU General Public License as published by 6 + // the Free Software Foundation, version 3. 7 + // 8 + // This program is distributed in the hope that it will be useful, but 9 + // WITHOUT ANY WARRANTY; without even the implied warranty of 10 + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 + // General Public License for more details. 12 + // 13 + // You should have received a copy of the GNU General Public License 14 + // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 + // 16 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 + // SPDX-License-Identifier: GPL-3.0-only 18 + 19 + //! `cargo run --example error_handler` 20 + 21 + use windmark::Response; 22 + 23 + #[windmark::main] 24 + async fn main() -> Result<(), Box<dyn std::error::Error>> { 25 + let mut error_count = 0; 26 + 27 + windmark::Router::new() 28 + .set_private_key_file("windmark_private.pem") 29 + .set_certificate_file("windmark_public.pem") 30 + .set_error_handler(move |_| { 31 + error_count += 1; 32 + 33 + println!("{} errors so far", error_count); 34 + 35 + Response::permanent_failure("e") 36 + }) 37 + .mount("/error", |_| { 38 + let nothing = None::<String>; 39 + 40 + Response::success(nothing.unwrap()) 41 + }) 42 + .run() 43 + .await 44 + }
+33
examples/fix_path.rs
··· 1 + // This file is part of Windmark <https://github.com/gemrest/windmark>. 2 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3 + // 4 + // This program is free software: you can redistribute it and/or modify 5 + // it under the terms of the GNU General Public License as published by 6 + // the Free Software Foundation, version 3. 7 + // 8 + // This program is distributed in the hope that it will be useful, but 9 + // WITHOUT ANY WARRANTY; without even the implied warranty of 10 + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 + // General Public License for more details. 12 + // 13 + // You should have received a copy of the GNU General Public License 14 + // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 + // 16 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 + // SPDX-License-Identifier: GPL-3.0-only 18 + 19 + //! `cargo run --example fix_path` 20 + 21 + #[windmark::main] 22 + async fn main() -> Result<(), Box<dyn std::error::Error>> { 23 + windmark::Router::new() 24 + .set_private_key_file("windmark_private.pem") 25 + .set_certificate_file("windmark_public.pem") 26 + .set_fix_path(true) 27 + .mount( 28 + "/close", 29 + windmark::success!("Visit '/close/'; you should be close enough!"), 30 + ) 31 + .run() 32 + .await 33 + }
+44
examples/input.rs
··· 1 + // This file is part of Windmark <https://github.com/gemrest/windmark>. 2 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3 + // 4 + // This program is free software: you can redistribute it and/or modify 5 + // it under the terms of the GNU General Public License as published by 6 + // the Free Software Foundation, version 3. 7 + // 8 + // This program is distributed in the hope that it will be useful, but 9 + // WITHOUT ANY WARRANTY; without even the implied warranty of 10 + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 + // General Public License for more details. 12 + // 13 + // You should have received a copy of the GNU General Public License 14 + // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 + // 16 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 + // SPDX-License-Identifier: GPL-3.0-only 18 + 19 + //! `cargo run --example input` 20 + 21 + use windmark::{context::RouteContext, Response}; 22 + 23 + #[windmark::main] 24 + async fn main() -> Result<(), Box<dyn std::error::Error>> { 25 + windmark::Router::new() 26 + .set_private_key_file("windmark_private.pem") 27 + .set_certificate_file("windmark_public.pem") 28 + .mount("/input", |context: RouteContext| { 29 + if let Some(name) = context.url.query() { 30 + Response::success(format!("Your name is {}!", name)) 31 + } else { 32 + Response::input("What is your name?") 33 + } 34 + }) 35 + .mount("/sensitive", |context: RouteContext| { 36 + if let Some(password) = context.url.query() { 37 + Response::success(format!("Your password is {}!", password)) 38 + } else { 39 + Response::sensitive_input("What is your password?") 40 + } 41 + }) 42 + .run() 43 + .await 44 + }
+33
examples/mime.rs
··· 1 + // This file is part of Windmark <https://github.com/gemrest/windmark>. 2 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3 + // 4 + // This program is free software: you can redistribute it and/or modify 5 + // it under the terms of the GNU General Public License as published by 6 + // the Free Software Foundation, version 3. 7 + // 8 + // This program is distributed in the hope that it will be useful, but 9 + // WITHOUT ANY WARRANTY; without even the implied warranty of 10 + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 + // General Public License for more details. 12 + // 13 + // You should have received a copy of the GNU General Public License 14 + // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 + // 16 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 + // SPDX-License-Identifier: GPL-3.0-only 18 + 19 + //! `cargo run --example mime` 20 + 21 + #[windmark::main] 22 + async fn main() -> Result<(), Box<dyn std::error::Error>> { 23 + windmark::Router::new() 24 + .set_private_key_file("windmark_private.pem") 25 + .set_certificate_file("windmark_public.pem") 26 + .mount("/mime", |_| { 27 + windmark::Response::success("Hello!".to_string()) 28 + .with_mime("text/plain") 29 + .clone() 30 + }) 31 + .run() 32 + .await 33 + }
+51
examples/parameters.rs
··· 1 + // This file is part of Windmark <https://github.com/gemrest/windmark>. 2 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3 + // 4 + // This program is free software: you can redistribute it and/or modify 5 + // it under the terms of the GNU General Public License as published by 6 + // the Free Software Foundation, version 3. 7 + // 8 + // This program is distributed in the hope that it will be useful, but 9 + // WITHOUT ANY WARRANTY; without even the implied warranty of 10 + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 + // General Public License for more details. 12 + // 13 + // You should have received a copy of the GNU General Public License 14 + // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 + // 16 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 + // SPDX-License-Identifier: GPL-3.0-only 18 + 19 + //! `cargo run --example parameters` 20 + 21 + use windmark::success; 22 + 23 + #[windmark::main] 24 + async fn main() -> Result<(), Box<dyn std::error::Error>> { 25 + windmark::Router::new() 26 + .set_private_key_file("windmark_private.pem") 27 + .set_certificate_file("windmark_public.pem") 28 + .mount( 29 + "/language/:language", 30 + success!( 31 + context, 32 + format!( 33 + "Your language of choice is {}.", 34 + context.parameters.get("language").unwrap() 35 + ) 36 + ), 37 + ) 38 + .mount( 39 + "/name/:first/:last", 40 + success!( 41 + context, 42 + format!( 43 + "Your name is {} {}.", 44 + context.parameters.get("first").unwrap(), 45 + context.parameters.get("last").unwrap() 46 + ) 47 + ), 48 + ) 49 + .run() 50 + .await 51 + }
+34
examples/partial.rs
··· 1 + // This file is part of Windmark <https://github.com/gemrest/windmark>. 2 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3 + // 4 + // This program is free software: you can redistribute it and/or modify 5 + // it under the terms of the GNU General Public License as published by 6 + // the Free Software Foundation, version 3. 7 + // 8 + // This program is distributed in the hope that it will be useful, but 9 + // WITHOUT ANY WARRANTY; without even the implied warranty of 10 + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 + // General Public License for more details. 12 + // 13 + // You should have received a copy of the GNU General Public License 14 + // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 + // 16 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 + // SPDX-License-Identifier: GPL-3.0-only 18 + 19 + //! `cargo run --example partial` 20 + 21 + #[windmark::main] 22 + async fn main() -> Result<(), Box<dyn std::error::Error>> { 23 + windmark::Router::new() 24 + .set_private_key_file("windmark_private.pem") 25 + .set_certificate_file("windmark_public.pem") 26 + .add_header(|_| "This is fancy art.\n".to_string()) 27 + .add_footer(|context: windmark::context::RouteContext| { 28 + format!("\nYou came from '{}'.", context.url.path()) 29 + }) 30 + .add_footer(|_| "\nCopyright (C) 2022".to_string()) 31 + .mount("/", windmark::success!("Hello!")) 32 + .run() 33 + .await 34 + }
+38
examples/query.rs
··· 1 + // This file is part of Windmark <https://github.com/gemrest/windmark>. 2 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3 + // 4 + // This program is free software: you can redistribute it and/or modify 5 + // it under the terms of the GNU General Public License as published by 6 + // the Free Software Foundation, version 3. 7 + // 8 + // This program is distributed in the hope that it will be useful, but 9 + // WITHOUT ANY WARRANTY; without even the implied warranty of 10 + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 + // General Public License for more details. 12 + // 13 + // You should have received a copy of the GNU General Public License 14 + // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 + // 16 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 + // SPDX-License-Identifier: GPL-3.0-only 18 + 19 + //! `cargo run --example input` 20 + 21 + #[windmark::main] 22 + async fn main() -> Result<(), Box<dyn std::error::Error>> { 23 + windmark::Router::new() 24 + .set_private_key_file("windmark_private.pem") 25 + .set_certificate_file("windmark_public.pem") 26 + .mount( 27 + "/query", 28 + windmark::success!( 29 + context, 30 + format!( 31 + "You provided the following queries: '{:?}'", 32 + windmark::utilities::queries_from_url(&context.url) 33 + ) 34 + ), 35 + ) 36 + .run() 37 + .await 38 + }
+49
examples/responses.rs
··· 1 + // This file is part of Windmark <https://github.com/gemrest/windmark>. 2 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3 + // 4 + // This program is free software: you can redistribute it and/or modify 5 + // it under the terms of the GNU General Public License as published by 6 + // the Free Software Foundation, version 3. 7 + // 8 + // This program is distributed in the hope that it will be useful, but 9 + // WITHOUT ANY WARRANTY; without even the implied warranty of 10 + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 + // General Public License for more details. 12 + // 13 + // You should have received a copy of the GNU General Public License 14 + // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 + // 16 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 + // SPDX-License-Identifier: GPL-3.0-only 18 + 19 + //! `cargo run --example responses` 20 + 21 + use windmark::success; 22 + 23 + #[windmark::main] 24 + async fn main() -> Result<(), Box<dyn std::error::Error>> { 25 + windmark::Router::new() 26 + .set_private_key_file("windmark_private.pem") 27 + .set_certificate_file("windmark_public.pem") 28 + .mount( 29 + "/", 30 + success!( 31 + "# Index\n\nWelcome!\n\n=> /test Test Page\n=> /time Unix Epoch" 32 + ), 33 + ) 34 + .mount("/test", success!("This is a test page.\n=> / back")) 35 + .mount( 36 + "/failure", 37 + windmark::temporary_failure!("Woops ... temporarily."), 38 + ) 39 + .mount( 40 + "/time", 41 + success!(std::time::UNIX_EPOCH.elapsed().unwrap().as_nanos()), 42 + ) 43 + .mount( 44 + "/redirect", 45 + windmark::permanent_redirect!("gemini://localhost/test"), 46 + ) 47 + .run() 48 + .await 49 + }
+37
examples/stateless_module.rs
··· 1 + // This file is part of Windmark <https://github.com/gemrest/windmark>. 2 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3 + // 4 + // This program is free software: you can redistribute it and/or modify 5 + // it under the terms of the GNU General Public License as published by 6 + // the Free Software Foundation, version 3. 7 + // 8 + // This program is distributed in the hope that it will be useful, but 9 + // WITHOUT ANY WARRANTY; without even the implied warranty of 10 + // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 + // General Public License for more details. 12 + // 13 + // You should have received a copy of the GNU General Public License 14 + // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 + // 16 + // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 + // SPDX-License-Identifier: GPL-3.0-only 18 + 19 + //! `cargo run --example stateless_module` 20 + 21 + use windmark::Router; 22 + 23 + fn smiley(_context: windmark::context::RouteContext) -> windmark::Response { 24 + windmark::Response::success("😀") 25 + } 26 + 27 + fn emojis(router: &mut Router) { router.mount("/smiley", smiley); } 28 + 29 + #[windmark::main] 30 + async fn main() -> Result<(), Box<dyn std::error::Error>> { 31 + Router::new() 32 + .set_private_key_file("windmark_private.pem") 33 + .set_certificate_file("windmark_public.pem") 34 + .attach_stateless(emojis) 35 + .run() 36 + .await 37 + }
-219
examples/windmark.rs
··· 1 - // This file is part of Windmark <https://github.com/gemrest/windmark>. 2 - // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3 - // 4 - // This program is free software: you can redistribute it and/or modify 5 - // it under the terms of the GNU General Public License as published by 6 - // the Free Software Foundation, version 3. 7 - // 8 - // This program is distributed in the hope that it will be useful, but 9 - // WITHOUT ANY WARRANTY; without even the implied warranty of 10 - // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 - // General Public License for more details. 12 - // 13 - // You should have received a copy of the GNU General Public License 14 - // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 - // 16 - // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 - // SPDX-License-Identifier: GPL-3.0-only 18 - 19 - //! `cargo run --example windmark --features logger` 20 - 21 - #[macro_use] 22 - extern crate log; 23 - 24 - use windmark::{ 25 - context::{HookContext, RouteContext}, 26 - response::Response, 27 - success, 28 - Router, 29 - }; 30 - 31 - #[derive(Default)] 32 - struct Clicker { 33 - clicks: isize, 34 - } 35 - 36 - #[async_trait::async_trait] 37 - impl windmark::AsyncModule for Clicker { 38 - async fn on_attach(&mut self, _: &mut Router) { 39 - println!("clicker has been attached!"); 40 - } 41 - 42 - async fn on_pre_route(&mut self, context: HookContext) { 43 - self.clicks += 1; 44 - 45 - info!( 46 - "clicker has been called pre-route on {} with {} clicks!", 47 - context.url.path(), 48 - self.clicks 49 - ); 50 - } 51 - 52 - async fn on_post_route(&mut self, context: HookContext) { 53 - info!( 54 - "clicker has been called post-route on {} with {} clicks!", 55 - context.url.path(), 56 - self.clicks 57 - ); 58 - } 59 - } 60 - 61 - #[windmark::main] 62 - async fn main() -> Result<(), Box<dyn std::error::Error>> { 63 - let mut error_count = 0; 64 - let mut router = Router::new(); 65 - let async_clicks = std::sync::Arc::new(tokio::sync::Mutex::new(0)); 66 - 67 - router.set_private_key_file("windmark_private.pem"); 68 - router.set_certificate_file("windmark_public.pem"); 69 - #[cfg(feature = "logger")] 70 - router.enable_default_logger(true); 71 - router.set_error_handler(move |_| { 72 - error_count += 1; 73 - 74 - println!("{} errors so far", error_count); 75 - 76 - Response::permanent_failure("e") 77 - }); 78 - router.set_fix_path(true); 79 - router.attach_stateless(|r| { 80 - r.mount("/module", success!("This is a module!")); 81 - }); 82 - router.attach_async(Clicker::default()); 83 - router.set_pre_route_callback(|context| { 84 - info!( 85 - "accepted connection from {} to {}", 86 - context.peer_address.unwrap().ip(), 87 - context.url.to_string() 88 - ) 89 - }); 90 - router.set_post_route_callback(|context, content| { 91 - content.content = 92 - content.content.replace("Welcome!", "Welcome to Windmark!"); 93 - 94 - info!( 95 - "closed connection from {}", 96 - context.peer_address.unwrap().ip() 97 - ) 98 - }); 99 - router.add_header(|_| "```\nART IS COOL\n```\nhi".to_string()); 100 - router.add_footer(|_| "Copyright 2022".to_string()); 101 - router.add_footer(|context: RouteContext| { 102 - format!("Another footer, but lower! (from {})", context.url.path()) 103 - }); 104 - router.mount( 105 - "/", 106 - success!("# INDEX\n\nWelcome!\n\n=> /test Test Page\n=> /time Unix Epoch"), 107 - ); 108 - router.mount("/specific-mime", |_| { 109 - Response::success("hi".to_string()) 110 - .with_mime("text/plain") 111 - .clone() 112 - }); 113 - router.mount("/test", success!("hi there\n=> / back")); 114 - router.mount( 115 - "/temporary-failure", 116 - windmark::temporary_failure!("Woops, temporarily..."), 117 - ); 118 - router.mount( 119 - "/time", 120 - success!(std::time::UNIX_EPOCH.elapsed().unwrap().as_nanos()), 121 - ); 122 - router.mount( 123 - "/query", 124 - success!( 125 - context, 126 - format!( 127 - "queries: {:?}", 128 - windmark::utilities::queries_from_url(&context.url) 129 - ) 130 - ), 131 - ); 132 - router.mount( 133 - "/param/:lang", 134 - success!( 135 - context, 136 - format!( 137 - "Parameter lang is {}", 138 - context.parameters.get("lang").unwrap() 139 - ) 140 - ), 141 - ); 142 - router.mount( 143 - "/names/:first/:last", 144 - success!( 145 - context, 146 - format!( 147 - "{} {}", 148 - context.parameters.get("first").unwrap(), 149 - context.parameters.get("last").unwrap() 150 - ) 151 - ), 152 - ); 153 - router.mount("/input", |context: RouteContext| { 154 - if let Some(name) = context.url.query() { 155 - Response::success(format!("Your name is {}!", name)) 156 - } else { 157 - Response::input("What is your name?") 158 - } 159 - }); 160 - router.mount("/sensitive-input", |context: RouteContext| { 161 - if let Some(password) = context.url.query() { 162 - Response::success(format!("Your password is {}!", password)) 163 - } else { 164 - Response::sensitive_input("What is your password?") 165 - } 166 - }); 167 - router.mount("/error", windmark::certificate_not_valid!("no")); 168 - router.mount( 169 - "/redirect", 170 - windmark::permanent_redirect!("gemini://localhost/test"), 171 - ); 172 - #[cfg(feature = "auto-deduce-mime")] 173 - router.mount("/auto-file", { 174 - windmark::binary_success!(include_bytes!("../LICENSE")) 175 - }); 176 - router.mount("/file", { 177 - windmark::binary_success!(include_bytes!("../LICENSE"), "text/plain") 178 - }); 179 - router.mount("/string-file", { 180 - windmark::binary_success!("hi", "text/plain") 181 - }); 182 - router.mount("/secret", |context: RouteContext| { 183 - if let Some(certificate) = context.certificate { 184 - Response::success(format!("Your public key: {}.", { 185 - (|| -> Result<String, openssl::error::ErrorStack> { 186 - Ok(format!( 187 - "{:?}", 188 - certificate.public_key()?.rsa()?.public_key_to_pem()? 189 - )) 190 - })() 191 - .unwrap_or_else(|_| "Unknown".to_string()) 192 - },)) 193 - } else { 194 - Response::client_certificate_required( 195 - "This is a secret route! Identify yourself!", 196 - ) 197 - } 198 - }); 199 - router.mount("/async", move |_| { 200 - let async_clicks = async_clicks.clone(); 201 - 202 - async move { 203 - let mut clicks = async_clicks.lock().await; 204 - 205 - *clicks += 1; 206 - 207 - Response::success(*clicks) 208 - } 209 - }); 210 - router.mount("/async-nothing", |context| { 211 - async move { Response::success(context.url.path()) } 212 - }); 213 - router.mount( 214 - "/async-macro", 215 - windmark::success_async!(async { "hi" }.await), 216 - ); 217 - 218 - router.run().await 219 - }