From 87fc86dfa3ed50eece56093186e2f986d44d8f2f Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sat, 15 Apr 2023 01:53:17 +0000 Subject: chore(examples): add sync module example --- examples/stateful_module.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 examples/stateful_module.rs (limited to 'examples') diff --git a/examples/stateful_module.rs b/examples/stateful_module.rs new file mode 100644 index 0000000..d97f139 --- /dev/null +++ b/examples/stateful_module.rs @@ -0,0 +1,67 @@ +// This file is part of Windmark . +// Copyright (C) 2022-2022 Fuwn +// +// 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 . +// +// Copyright (C) 2022-2022 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +//! `cargo run --example stateful_module` + +use windmark::{context::HookContext, Router}; + +#[derive(Default)] +struct Clicker { + clicks: usize, +} + +impl windmark::Module for Clicker { + fn on_attach(&mut self, _router: &mut Router) { + println!("module 'clicker' has been attached!"); + } + + fn on_pre_route(&mut self, context: HookContext) { + self.clicks += 1; + + println!( + "module 'clicker' has been called before the route '{}' with {} clicks!", + context.url.path(), + self.clicks, + ); + } + + 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, + ); + } +} + +#[windmark::main] +async fn main() -> Result<(), Box> { + 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(Clicker::default()); + router.mount("/", windmark::success!("Hello!")); + + router.run().await +} -- cgit v1.2.3