diff options
| author | Fuwn <[email protected]> | 2022-04-02 00:48:47 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-04-02 00:48:47 +0000 |
| commit | 8869b6ef044aa42043f8e17ee0d56767730d56fc (patch) | |
| tree | 465d58df7fd5ec125f7ebe9400e6b6d28ed8c7ff /examples/windmark.rs | |
| parent | build(cargo): simplify example usage (diff) | |
| download | windmark-8869b6ef044aa42043f8e17ee0d56767730d56fc.tar.xz windmark-8869b6ef044aa42043f8e17ee0d56767730d56fc.zip | |
feat(router): stateful modules!
Diffstat (limited to 'examples/windmark.rs')
| -rw-r--r-- | examples/windmark.rs | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/examples/windmark.rs b/examples/windmark.rs index 211d235..dc69bbe 100644 --- a/examples/windmark.rs +++ b/examples/windmark.rs @@ -21,7 +21,35 @@ #[macro_use] extern crate log; -use windmark::Response; +use windmark::{returnable::CallbackContext, Response, Router}; + +#[derive(Default)] +struct Clicker { + clicks: isize, +} +impl windmark::Module for Clicker { + fn on_attach(&mut self, _: &mut Router) { + println!("clicker has been attached!"); + } + + fn on_pre_route(&mut self, context: CallbackContext<'_>) { + self.clicks += 1; + + info!( + "clicker has been called pre-route on {} with {} clicks!", + context.url.path(), + self.clicks + ); + } + + fn on_post_route(&mut self, context: CallbackContext<'_>) { + info!( + "clicker has been called post-route on {} with {} clicks!", + context.url.path(), + self.clicks + ); + } +} #[windmark::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { @@ -38,12 +66,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { Response::PermanentFailure("e".into()) })) - .attach(|r| { + .attach_stateless(|r| { r.mount( "/module", Box::new(|_| Response::Success("This is a module!".into())), ); }) + .attach(Clicker::default()) .set_pre_route_callback(Box::new(|stream, url, _| { info!( "accepted connection from {} to {}", |