From 726bf413f56d856d6cdc112c180b672e70b52184 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Wed, 25 Jan 2023 11:34:21 +0100 Subject: [PATCH] rest-handler: more convenient auth/index handler setters Signed-off-by: Wolfgang Bumiller --- proxmox-rest-server/src/api_config.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/proxmox-rest-server/src/api_config.rs b/proxmox-rest-server/src/api_config.rs index 7f5af954..f4cc30f3 100644 --- a/proxmox-rest-server/src/api_config.rs +++ b/proxmox-rest-server/src/api_config.rs @@ -65,12 +65,28 @@ impl ApiConfig { self } + /// Set the authentication handler from a function. + pub fn with_auth_handler_func(self, func: Func) -> Self + where + Func: for<'a> Fn(&'a HeaderMap, &'a Method) -> CheckAuthFuture<'a> + Send + Sync + 'static, + { + self.with_auth_handler(AuthHandler::from_fn(func)) + } + /// Set the index handler. pub fn with_index_handler(mut self, index_handler: IndexHandler) -> Self { self.index_handler = Some(index_handler); self } + /// Set the index handler from a function. + pub fn with_index_handler_func(self, func: Func) -> Self + where + Func: Fn(RestEnvironment, Parts) -> IndexFuture + Send + Sync + 'static, + { + self.with_index_handler(IndexHandler::from_fn(func)) + } + pub(crate) async fn get_index( &self, rest_env: RestEnvironment, @@ -373,13 +389,13 @@ impl IndexHandler { where B: Clone + Send + Sync + Into + 'static, { - Self::from_response_fn(move |_, _| { + Self::from_fn(move |_, _| { let body = body.clone().into(); Box::pin(async move { Response::builder().status(200).body(body).unwrap() }) }) } - pub fn from_response_fn(func: Func) -> Self + pub fn from_fn(func: Func) -> Self where Func: Fn(RestEnvironment, Parts) -> IndexFuture + Send + Sync + 'static, {