From 6904dcf4e648511338592ad396516d1b0d3b5842 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Tue, 24 Jan 2023 15:02:56 +0100 Subject: [PATCH] rest-server: make adapter optional when no user information or index needs to be defined Signed-off-by: Wolfgang Bumiller --- proxmox-rest-server/src/api_config.rs | 34 ++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/proxmox-rest-server/src/api_config.rs b/proxmox-rest-server/src/api_config.rs index ba21fcd3..8d81a7e2 100644 --- a/proxmox-rest-server/src/api_config.rs +++ b/proxmox-rest-server/src/api_config.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::future::Future; use std::path::PathBuf; use std::pin::Pin; use std::sync::{Arc, Mutex}; @@ -40,18 +41,14 @@ impl ApiConfig { /// (index). Please note that this functions gets a reference to /// the [ApiConfig], so it can use [Handlebars] templates /// ([render_template](Self::render_template) to generate pages. - pub fn new>( - basedir: B, - env_type: RpcEnvironmentType, - adapter: impl ServerAdapter + 'static, - ) -> Self { + pub fn new>(basedir: B, env_type: RpcEnvironmentType) -> Self { Self { basedir: basedir.into(), aliases: HashMap::new(), env_type, request_log: None, auth_log: None, - adapter: Box::pin(adapter), + adapter: Box::pin(DummyAdapter), handlers: Vec::new(), #[cfg(feature = "templates")] @@ -336,3 +333,28 @@ mod templates { } } } + +pub struct DummyAdapter; + +impl ServerAdapter for DummyAdapter { + fn get_index( + &self, + _rest_env: RestEnvironment, + _parts: Parts, + ) -> Pin> + Send>> { + Box::pin(async move { + Response::builder() + .status(400) + .body("no index defined".into()) + .unwrap() + }) + } + + fn check_auth<'a>( + &'a self, + _headers: &'a http::HeaderMap, + _method: &'a http::Method, + ) -> crate::ServerAdapterCheckAuth<'a> { + Box::pin(async move { Err(crate::AuthError::NoData) }) + } +}