aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-03-29 07:58:05 +0000
committerFuwn <[email protected]>2023-03-29 08:34:11 +0000
commit68b508fda73f5458202e6b3abf52778663954f84 (patch)
treef9a9dfd15946c9d5429b23b182ec6cbae7073fd6
parentci(check): use table toolchain (diff)
downloadwindmark-68b508fda73f5458202e6b3abf52778663954f84.tar.xz
windmark-68b508fda73f5458202e6b3abf52778663954f84.zip
refactor(returnable): unify callback context
-rw-r--r--examples/windmark.rs10
-rw-r--r--src/handler.rs14
-rw-r--r--src/router.rs22
3 files changed, 24 insertions, 22 deletions
diff --git a/examples/windmark.rs b/examples/windmark.rs
index 736eb54..fca1c97 100644
--- a/examples/windmark.rs
+++ b/examples/windmark.rs
@@ -75,17 +75,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
);
});
router.attach(Clicker::default());
- router.set_pre_route_callback(Box::new(|stream, url, _| {
+ router.set_pre_route_callback(Box::new(|context| {
info!(
"accepted connection from {} to {}",
- stream.peer_addr().unwrap().ip(),
- url.to_string()
+ context.tcp.peer_addr().unwrap().ip(),
+ context.url.to_string()
)
}));
- router.set_post_route_callback(Box::new(|stream, _url, _| {
+ router.set_post_route_callback(Box::new(|context| {
info!(
"closed connection from {}",
- stream.peer_addr().unwrap().ip()
+ context.tcp.peer_addr().unwrap().ip()
)
}));
router.add_header(Box::new(|_| "```\nART IS COOL\n```\nhi".to_string()));
diff --git a/src/handler.rs b/src/handler.rs
index 5377703..15dfd6d 100644
--- a/src/handler.rs
+++ b/src/handler.rs
@@ -16,16 +16,12 @@
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only
-use crate::{returnable::RouteContext, Response};
+use crate::{returnable, returnable::RouteContext, Response};
pub type RouteResponse =
Box<dyn FnMut(RouteContext<'_>) -> Response<'_> + Send + Sync>;
-pub type ErrorResponse = Box<
- dyn FnMut(crate::returnable::ErrorContext<'_>) -> Response<'_> + Send + Sync,
->;
-pub type Callback = Box<
- dyn FnMut(&tokio::net::TcpStream, &url::Url, Option<&matchit::Params<'_, '_>>)
- + Send
- + Sync,
->;
+pub type ErrorResponse =
+ Box<dyn FnMut(returnable::ErrorContext<'_>) -> Response<'_> + Send + Sync>;
+pub type Callback =
+ Box<dyn FnMut(returnable::CallbackContext<'_>) + Send + Sync>;
pub type Partial = Box<dyn FnMut(RouteContext<'_>) -> String + Send + Sync>;
diff --git a/src/router.rs b/src/router.rs
index 6d2737e..dc81d40 100644
--- a/src/router.rs
+++ b/src/router.rs
@@ -316,9 +316,12 @@ impl Router {
));
}
- (*self.pre_route_callback).lock().unwrap()(stream.get_ref(), &url, {
- route.as_ref().map_or(None, |route| Some(&route.params))
- });
+ (*self.pre_route_callback).lock().unwrap()(CallbackContext::new(
+ stream.get_ref(),
+ &url,
+ route.as_ref().map_or(None, |route| Some(&route.params)),
+ &stream.ssl().peer_certificate(),
+ ));
let content = if let Ok(ref route) = route {
let footers_length = (*self.footers.lock().unwrap()).len();
@@ -418,9 +421,12 @@ impl Router {
));
}
- (*self.post_route_callback).lock().unwrap()(stream.get_ref(), &url, {
- route.as_ref().map_or(None, |route| Some(&route.params))
- });
+ (*self.post_route_callback).lock().unwrap()(CallbackContext::new(
+ stream.get_ref(),
+ &url,
+ route.as_ref().map_or(None, |route| Some(&route.params)),
+ &stream.ssl().peer_certificate(),
+ ));
stream.shutdown().await?;
@@ -756,8 +762,8 @@ impl Default for Router {
),
#[cfg(feature = "logger")]
default_logger: false,
- pre_route_callback: Arc::new(Mutex::new(Box::new(|_, _, _| {}))),
- post_route_callback: Arc::new(Mutex::new(Box::new(|_, _, _| {}))),
+ pre_route_callback: Arc::new(Mutex::new(Box::new(|_| {}))),
+ post_route_callback: Arc::new(Mutex::new(Box::new(|_| {}))),
charset: "utf-8".to_string(),
language: "en".to_string(),
port: 1965,