🏗️ 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): add sync module example

Fuwn 87fc86df 9b47502b

+67
+67
examples/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 stateful_module` 20 + 21 + use windmark::{context::HookContext, Router}; 22 + 23 + #[derive(Default)] 24 + struct Clicker { 25 + clicks: usize, 26 + } 27 + 28 + impl windmark::Module for Clicker { 29 + fn on_attach(&mut self, _router: &mut Router) { 30 + println!("module 'clicker' has been attached!"); 31 + } 32 + 33 + fn on_pre_route(&mut self, context: HookContext) { 34 + self.clicks += 1; 35 + 36 + println!( 37 + "module 'clicker' has been called before the route '{}' with {} clicks!", 38 + context.url.path(), 39 + self.clicks, 40 + ); 41 + } 42 + 43 + fn on_post_route(&mut self, context: HookContext) { 44 + println!( 45 + "module 'clicker' clicker has been called after the route '{}' with {} \ 46 + clicks!", 47 + context.url.path(), 48 + self.clicks, 49 + ); 50 + } 51 + } 52 + 53 + #[windmark::main] 54 + async fn main() -> Result<(), Box<dyn std::error::Error>> { 55 + let mut router = Router::new(); 56 + 57 + router.set_private_key_file("windmark_private.pem"); 58 + router.set_certificate_file("windmark_public.pem"); 59 + #[cfg(feature = "logger")] 60 + { 61 + router.enable_default_logger(true); 62 + } 63 + router.attach(Clicker::default()); 64 + router.mount("/", windmark::success!("Hello!")); 65 + 66 + router.run().await 67 + }