🏗️ 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.

refactor(hooks): implement call for hooks

Fuwn c360d1d7 87fc86df

+42 -23
+12 -8
examples/callbacks.rs
··· 18 18 19 19 //! `cargo run --example callbacks` 20 20 21 + use windmark::context::HookContext; 22 + 21 23 #[windmark::main] 22 24 async fn main() -> Result<(), Box<dyn std::error::Error>> { 23 25 windmark::Router::new() 24 26 .set_private_key_file("windmark_private.pem") 25 27 .set_certificate_file("windmark_public.pem") 26 28 .mount("/", windmark::success!("Hello!")) 27 - .set_pre_route_callback(|context| { 29 + .set_pre_route_callback(|context: HookContext| { 28 30 println!( 29 31 "accepted connection from {} to {}", 30 32 context.peer_address.unwrap().ip(), 31 33 context.url.to_string() 32 34 ) 33 35 }) 34 - .set_post_route_callback(|context, content| { 35 - content.content = content.content.replace("Hello", "Hi"); 36 + .set_post_route_callback( 37 + |context: HookContext, content: &mut windmark::Response| { 38 + content.content = content.content.replace("Hello", "Hi"); 36 39 37 - println!( 38 - "closed connection from {}", 39 - context.peer_address.unwrap().ip() 40 - ) 41 - }) 40 + println!( 41 + "closed connection from {}", 42 + context.peer_address.unwrap().ip() 43 + ) 44 + }, 45 + ) 42 46 .run() 43 47 .await 44 48 }
+9 -5
src/handler/hooks/post_route.rs
··· 19 19 use crate::{context::HookContext, Response}; 20 20 21 21 #[allow(clippy::module_name_repetitions)] 22 - pub trait PostRouteHook: 23 - FnMut(HookContext, &mut Response) + Send + Sync 24 - { 22 + pub trait PostRouteHook: Send + Sync { 23 + fn call(&mut self, context: HookContext, response: &mut Response); 25 24 } 26 25 27 - impl<T> PostRouteHook for T where T: FnMut(HookContext, &mut Response) + Send + Sync 28 - {} 26 + impl<T> PostRouteHook for T 27 + where T: FnMut(HookContext, &mut Response) + Send + Sync 28 + { 29 + fn call(&mut self, context: HookContext, response: &mut Response) { 30 + (*self)(context, response); 31 + } 32 + }
+8 -2
src/handler/hooks/pre_route.rs
··· 19 19 use crate::context::HookContext; 20 20 21 21 #[allow(clippy::module_name_repetitions)] 22 - pub trait PreRouteHook: FnMut(HookContext) + Send + Sync {} 22 + pub trait PreRouteHook: Send + Sync { 23 + fn call(&mut self, context: HookContext); 24 + } 23 25 24 - impl<T> PreRouteHook for T where T: FnMut(HookContext) + Send + Sync {} 26 + impl<T> PreRouteHook for T 27 + where T: FnMut(HookContext) + Send + Sync 28 + { 29 + fn call(&mut self, context: HookContext) { (*self)(context) } 30 + }
+13 -8
src/router.rs
··· 83 83 ssl_acceptor: Arc<SslAcceptor>, 84 84 #[cfg(feature = "logger")] 85 85 default_logger: bool, 86 - pre_route_callback: Arc<Mutex<Box<dyn PreRouteHook<Output = ()>>>>, 87 - post_route_callback: Arc<Mutex<Box<dyn PostRouteHook<Output = ()>>>>, 86 + pre_route_callback: Arc<Mutex<Box<dyn PreRouteHook>>>, 87 + post_route_callback: Arc<Mutex<Box<dyn PostRouteHook>>>, 88 88 character_set: String, 89 89 languages: Vec<String>, 90 90 port: i32, ··· 368 368 module.on_pre_route(hook_context.clone()); 369 369 } 370 370 371 - (*self.pre_route_callback).lock().unwrap()(hook_context.clone()); 371 + (*self.pre_route_callback) 372 + .lock() 373 + .unwrap() 374 + .call(hook_context.clone()); 372 375 373 376 let mut content = if let Ok(ref route) = route { 374 377 let footers_length = (*self.footers.lock().unwrap()).len(); ··· 425 428 module.on_post_route(hook_context.clone()); 426 429 } 427 430 428 - (*self.post_route_callback).lock().unwrap()( 429 - hook_context.clone(), 430 - &mut content, 431 - ); 431 + (*self.post_route_callback) 432 + .lock() 433 + .unwrap() 434 + .call(hook_context.clone(), &mut content); 432 435 433 436 stream 434 437 .write_all( ··· 886 889 #[cfg(feature = "logger")] 887 890 default_logger: false, 888 891 pre_route_callback: Arc::new(Mutex::new(Box::new(|_| {}))), 889 - post_route_callback: Arc::new(Mutex::new(Box::new(|_, _| {}))), 892 + post_route_callback: Arc::new(Mutex::new(Box::new( 893 + |_, _: &'_ mut Response| {}, 894 + ))), 890 895 character_set: "utf-8".to_string(), 891 896 languages: vec!["en".to_string()], 892 897 port: 1965,