diff options
| author | Fuwn <[email protected]> | 2023-04-15 01:53:17 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-04-15 01:53:17 +0000 |
| commit | 87fc86dfa3ed50eece56093186e2f986d44d8f2f (patch) | |
| tree | 877fcbd4fb22f439e10ee31ad12378eaca4dae13 | |
| parent | chore(examples): seperate complex examples (diff) | |
| download | windmark-87fc86dfa3ed50eece56093186e2f986d44d8f2f.tar.xz windmark-87fc86dfa3ed50eece56093186e2f986d44d8f2f.zip | |
chore(examples): add sync module example
| -rw-r--r-- | examples/stateful_module.rs | 67 |
1 files changed, 67 insertions, 0 deletions
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 <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 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<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(Clicker::default()); + router.mount("/", windmark::success!("Hello!")); + + router.run().await +} |