aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2022-03-27 11:26:47 +0000
committerFuwn <[email protected]>2022-03-27 11:26:47 +0000
commitd971186326a6300bad86d94ff1e973c4ca3603da (patch)
tree255b1cf037433f2787446b603147a71a40cb3772
parentfeat(pre/post_route_callback): fnmut closure (diff)
downloadwindmark-d971186326a6300bad86d94ff1e973c4ca3603da.tar.xz
windmark-d971186326a6300bad86d94ff1e973c4ca3603da.zip
feat(handler): fnmut partial
-rw-r--r--examples/windmark.rs4
-rw-r--r--src/handler.rs2
-rw-r--r--src/lib.rs24
3 files changed, 13 insertions, 17 deletions
diff --git a/examples/windmark.rs b/examples/windmark.rs
index 21c84a8..29376a8 100644
--- a/examples/windmark.rs
+++ b/examples/windmark.rs
@@ -54,8 +54,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
stream.peer_addr().unwrap().ip()
)
}))
- .set_header(|_| "```\nART IS COOL\n```".to_string())
- .set_footer(|_| "Copyright 2022".to_string())
+ .set_header(Box::new(|_| "```\nART IS COOL\n```".to_string()))
+ .set_footer(Box::new(|_| "Copyright 2022".to_string()))
.mount("/", |_| {
Response::Success(
"# INDEX\n\nWelcome!\n\n=> /test Test Page\n=> /time Unix Epoch\n"
diff --git a/src/handler.rs b/src/handler.rs
index b1e6afe..7e297f8 100644
--- a/src/handler.rs
+++ b/src/handler.rs
@@ -27,4 +27,4 @@ pub type Callback = Box<
+ Send
+ Sync,
>;
-pub type Partial = fn(RouteContext<'_>) -> String;
+pub type Partial = Box<dyn FnMut(RouteContext<'_>) -> String + Send + Sync>;
diff --git a/src/lib.rs b/src/lib.rs
index fe3489a..09baf20 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -118,8 +118,8 @@ pub struct Router {
error_handler: Arc<Mutex<ErrorResponse>>,
private_key_file_name: String,
certificate_chain_file_name: String,
- header: Partial,
- footer: Partial,
+ header: Arc<Mutex<Partial>>,
+ footer: Arc<Mutex<Partial>>,
ssl_acceptor: Arc<SslAcceptor>,
#[cfg(feature = "logger")]
default_logger: bool,
@@ -223,7 +223,7 @@ impl Router {
/// });
/// ```
pub fn set_header(&mut self, handler: Partial) -> &mut Self {
- self.header = handler;
+ self.header = Arc::new(Mutex::new(handler));
self
}
@@ -238,7 +238,7 @@ impl Router {
/// });
/// ```
pub fn set_footer(&mut self, handler: Partial) -> &mut Self {
- self.footer = handler;
+ self.footer = Arc::new(Mutex::new(handler));
self
}
@@ -334,10 +334,8 @@ impl Router {
if let Ok(ref route) = route {
header = {
- let header = (self.header)(RouteContext::new(
- stream.get_ref(),
- &url,
- &route.params,
+ let header = (*self.header).lock().unwrap().call_mut((
+ RouteContext::new(stream.get_ref(), &url, &route.params),
));
if header.is_empty() {
@@ -347,10 +345,8 @@ impl Router {
}
};
footer = {
- let footer = (self.footer)(RouteContext::new(
- stream.get_ref(),
- &url,
- &route.params,
+ let footer = (*self.footer).lock().unwrap().call_mut((
+ RouteContext::new(stream.get_ref(), &url, &route.params),
));
if footer.is_empty() {
@@ -622,8 +618,8 @@ impl Default for Router {
}))),
private_key_file_name: "".to_string(),
certificate_chain_file_name: "".to_string(),
- header: |_| "".to_string(),
- footer: |_| "".to_string(),
+ header: Arc::new(Mutex::new(Box::new(|_| "".to_string()))),
+ footer: Arc::new(Mutex::new(Box::new(|_| "".to_string()))),
ssl_acceptor: Arc::new(
SslAcceptor::mozilla_intermediate(SslMethod::tls())
.unwrap()