proxmox-rest-server: make get_index async

This commit is contained in:
Dietmar Maurer 2021-10-01 09:38:10 +02:00
parent 58a6e5f512
commit 89766c4f95
3 changed files with 17 additions and 13 deletions

View File

@ -45,16 +45,18 @@ impl ApiAuth for DummyAuth {
// this should return the index page of the webserver
// iow. what the user browses to
fn get_index(
fn get_index<'a>(
_auth_id: Option<String>,
_language: Option<String>,
_api: &ApiConfig,
_api: &'a ApiConfig,
_parts: http::request::Parts,
) -> http::Response<hyper::Body> {
// build an index page
http::Response::builder()
.body("hello world".into())
.unwrap()
) -> Pin<Box<dyn Future<Output = http::Response<hyper::Body>> + Send + 'a>> {
Box::pin(async move {
// build an index page
http::Response::builder()
.body("hello world".into())
.unwrap()
})
}
// a few examples on how to do api calls with the Router
@ -191,7 +193,7 @@ async fn run() -> Result<(), Error> {
&ROUTER,
RpcEnvironmentType::PUBLIC,
Arc::new(DummyAuth {}),
get_index,
&get_index,
)?;
let rest_server = RestServer::new(config);

View File

@ -3,6 +3,8 @@ use std::path::PathBuf;
use std::time::SystemTime;
use std::fs::metadata;
use std::sync::{Arc, Mutex, RwLock};
use std::future::Future;
use std::pin::Pin;
use anyhow::{bail, Error, format_err};
use hyper::{Method, Body, Response};
@ -16,7 +18,7 @@ use proxmox::tools::fs::{create_path, CreateOptions};
use crate::{ApiAuth, FileLogger, FileLogOptions, CommandSocket};
pub type GetIndexFn = fn(Option<String>, Option<String>, &ApiConfig, Parts) -> Response<Body>;
pub type GetIndexFn = &'static (dyn for<'a> Fn(Option<String>, Option<String>, &'a ApiConfig, Parts) -> Pin<Box<dyn Future<Output = Response<Body>> + Send + 'a>> + Send + Sync);
/// REST server configuration
pub struct ApiConfig {
@ -68,13 +70,13 @@ impl ApiConfig {
})
}
pub(crate) fn get_index(
pub(crate) async fn get_index(
&self,
auth_id: Option<String>,
language: Option<String>,
parts: Parts,
) -> Response<Body> {
(self.get_index_fn)(auth_id, language, self, parts)
(self.get_index_fn)(auth_id, language, self, parts).await
}
pub(crate) fn find_method(

View File

@ -732,14 +732,14 @@ async fn handle_request(
let language = extract_lang_header(&parts.headers);
match auth.check_auth(&parts.headers, &method).await {
Ok((auth_id, _user_info)) => {
return Ok(api.get_index(Some(auth_id), language, parts));
return Ok(api.get_index(Some(auth_id), language, parts).await);
}
Err(AuthError::Generic(_)) => {
tokio::time::sleep_until(Instant::from_std(delay_unauth_time)).await;
}
Err(AuthError::NoData) => {}
}
return Ok(api.get_index(None, language, parts));
return Ok(api.get_index(None, language, parts).await);
} else {
let filename = api.find_alias(&components);
let compression = extract_compression_method(&parts.headers);