proxmox-rest-server: make get_index async
This commit is contained in:
parent
593043ed53
commit
6680878b5c
@ -45,16 +45,18 @@ impl ApiAuth for DummyAuth {
|
|||||||
// this should return the index page of the webserver
|
// this should return the index page of the webserver
|
||||||
// iow. what the user browses to
|
// iow. what the user browses to
|
||||||
|
|
||||||
fn get_index(
|
fn get_index<'a>(
|
||||||
_auth_id: Option<String>,
|
_auth_id: Option<String>,
|
||||||
_language: Option<String>,
|
_language: Option<String>,
|
||||||
_api: &ApiConfig,
|
_api: &'a ApiConfig,
|
||||||
_parts: http::request::Parts,
|
_parts: http::request::Parts,
|
||||||
) -> http::Response<hyper::Body> {
|
) -> Pin<Box<dyn Future<Output = http::Response<hyper::Body>> + Send + 'a>> {
|
||||||
// build an index page
|
Box::pin(async move {
|
||||||
http::Response::builder()
|
// build an index page
|
||||||
.body("hello world".into())
|
http::Response::builder()
|
||||||
.unwrap()
|
.body("hello world".into())
|
||||||
|
.unwrap()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// a few examples on how to do api calls with the Router
|
// a few examples on how to do api calls with the Router
|
||||||
@ -191,7 +193,7 @@ async fn run() -> Result<(), Error> {
|
|||||||
&ROUTER,
|
&ROUTER,
|
||||||
RpcEnvironmentType::PUBLIC,
|
RpcEnvironmentType::PUBLIC,
|
||||||
Arc::new(DummyAuth {}),
|
Arc::new(DummyAuth {}),
|
||||||
get_index,
|
&get_index,
|
||||||
)?;
|
)?;
|
||||||
let rest_server = RestServer::new(config);
|
let rest_server = RestServer::new(config);
|
||||||
|
|
||||||
|
@ -3,6 +3,8 @@ use std::path::PathBuf;
|
|||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
use std::fs::metadata;
|
use std::fs::metadata;
|
||||||
use std::sync::{Arc, Mutex, RwLock};
|
use std::sync::{Arc, Mutex, RwLock};
|
||||||
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
|
|
||||||
use anyhow::{bail, Error, format_err};
|
use anyhow::{bail, Error, format_err};
|
||||||
use hyper::{Method, Body, Response};
|
use hyper::{Method, Body, Response};
|
||||||
@ -16,7 +18,7 @@ use proxmox::tools::fs::{create_path, CreateOptions};
|
|||||||
|
|
||||||
use crate::{ApiAuth, FileLogger, FileLogOptions, CommandSocket};
|
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
|
/// REST server configuration
|
||||||
pub struct ApiConfig {
|
pub struct ApiConfig {
|
||||||
@ -68,13 +70,13 @@ impl ApiConfig {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_index(
|
pub(crate) async fn get_index(
|
||||||
&self,
|
&self,
|
||||||
auth_id: Option<String>,
|
auth_id: Option<String>,
|
||||||
language: Option<String>,
|
language: Option<String>,
|
||||||
parts: Parts,
|
parts: Parts,
|
||||||
) -> Response<Body> {
|
) -> 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(
|
pub(crate) fn find_method(
|
||||||
|
@ -732,14 +732,14 @@ async fn handle_request(
|
|||||||
let language = extract_lang_header(&parts.headers);
|
let language = extract_lang_header(&parts.headers);
|
||||||
match auth.check_auth(&parts.headers, &method).await {
|
match auth.check_auth(&parts.headers, &method).await {
|
||||||
Ok((auth_id, _user_info)) => {
|
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(_)) => {
|
Err(AuthError::Generic(_)) => {
|
||||||
tokio::time::sleep_until(Instant::from_std(delay_unauth_time)).await;
|
tokio::time::sleep_until(Instant::from_std(delay_unauth_time)).await;
|
||||||
}
|
}
|
||||||
Err(AuthError::NoData) => {}
|
Err(AuthError::NoData) => {}
|
||||||
}
|
}
|
||||||
return Ok(api.get_index(None, language, parts));
|
return Ok(api.get_index(None, language, parts).await);
|
||||||
} else {
|
} else {
|
||||||
let filename = api.find_alias(&components);
|
let filename = api.find_alias(&components);
|
||||||
let compression = extract_compression_method(&parts.headers);
|
let compression = extract_compression_method(&parts.headers);
|
||||||
|
@ -7,6 +7,8 @@ use std::os::unix::{
|
|||||||
};
|
};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
|
|
||||||
use anyhow::{bail, format_err, Error};
|
use anyhow::{bail, format_err, Error};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
@ -91,20 +93,22 @@ fn setup_system_env() -> Result<(), Error> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_index(
|
fn get_index<'a>(
|
||||||
_auth_id: Option<String>,
|
_auth_id: Option<String>,
|
||||||
_language: Option<String>,
|
_language: Option<String>,
|
||||||
_api: &ApiConfig,
|
_api: &'a ApiConfig,
|
||||||
_parts: Parts,
|
_parts: Parts,
|
||||||
) -> Response<Body> {
|
) -> Pin<Box<dyn Future<Output = http::Response<Body>> + Send + 'a>> {
|
||||||
|
Box::pin(async move {
|
||||||
|
|
||||||
let index = "<center><h1>Proxmox Backup Restore Daemon/h1></center>";
|
let index = "<center><h1>Proxmox Backup Restore Daemon/h1></center>";
|
||||||
|
|
||||||
Response::builder()
|
Response::builder()
|
||||||
.status(StatusCode::OK)
|
.status(StatusCode::OK)
|
||||||
.header(header::CONTENT_TYPE, "text/html")
|
.header(header::CONTENT_TYPE, "text/html")
|
||||||
.body(index.into())
|
.body(index.into())
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run() -> Result<(), Error> {
|
async fn run() -> Result<(), Error> {
|
||||||
@ -113,7 +117,7 @@ async fn run() -> Result<(), Error> {
|
|||||||
let auth_config = Arc::new(
|
let auth_config = Arc::new(
|
||||||
auth::ticket_auth().map_err(|err| format_err!("reading ticket file failed: {}", err))?,
|
auth::ticket_auth().map_err(|err| format_err!("reading ticket file failed: {}", err))?,
|
||||||
);
|
);
|
||||||
let config = ApiConfig::new("", &ROUTER, RpcEnvironmentType::PUBLIC, auth_config, get_index)?;
|
let config = ApiConfig::new("", &ROUTER, RpcEnvironmentType::PUBLIC, auth_config, &get_index)?;
|
||||||
let rest_server = RestServer::new(config);
|
let rest_server = RestServer::new(config);
|
||||||
|
|
||||||
let vsock_fd = get_vsock_fd()?;
|
let vsock_fd = get_vsock_fd()?;
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
|
|
||||||
use anyhow::{bail, Error};
|
use anyhow::{bail, Error};
|
||||||
use futures::*;
|
use futures::*;
|
||||||
use http::request::Parts;
|
use http::request::Parts;
|
||||||
@ -24,20 +27,22 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_index(
|
fn get_index<'a>(
|
||||||
_auth_id: Option<String>,
|
_auth_id: Option<String>,
|
||||||
_language: Option<String>,
|
_language: Option<String>,
|
||||||
_api: &ApiConfig,
|
_api: &'a ApiConfig,
|
||||||
_parts: Parts,
|
_parts: Parts,
|
||||||
) -> Response<Body> {
|
) -> Pin<Box<dyn Future<Output = Response<Body>> + Send + 'a>> {
|
||||||
|
Box::pin(async move {
|
||||||
|
|
||||||
let index = "<center><h1>Proxmox Backup API Server</h1></center>";
|
let index = "<center><h1>Proxmox Backup API Server</h1></center>";
|
||||||
|
|
||||||
Response::builder()
|
Response::builder()
|
||||||
.status(StatusCode::OK)
|
.status(StatusCode::OK)
|
||||||
.header(header::CONTENT_TYPE, "text/html")
|
.header(header::CONTENT_TYPE, "text/html")
|
||||||
.body(index.into())
|
.body(index.into())
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run() -> Result<(), Error> {
|
async fn run() -> Result<(), Error> {
|
||||||
@ -76,7 +81,7 @@ async fn run() -> Result<(), Error> {
|
|||||||
&proxmox_backup::api2::ROUTER,
|
&proxmox_backup::api2::ROUTER,
|
||||||
RpcEnvironmentType::PRIVILEGED,
|
RpcEnvironmentType::PRIVILEGED,
|
||||||
default_api_auth(),
|
default_api_auth(),
|
||||||
get_index,
|
&get_index,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let backup_user = pbs_config::backup_user()?;
|
let backup_user = pbs_config::backup_user()?;
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
use std::sync::{Mutex, Arc};
|
use std::sync::{Mutex, Arc};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::os::unix::io::AsRawFd;
|
use std::os::unix::io::AsRawFd;
|
||||||
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
|
|
||||||
use anyhow::{bail, format_err, Error};
|
use anyhow::{bail, format_err, Error};
|
||||||
use futures::*;
|
use futures::*;
|
||||||
@ -76,13 +78,24 @@ fn main() -> Result<(), Error> {
|
|||||||
pbs_runtime::main(run())
|
pbs_runtime::main(run())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_index(
|
fn get_index<'a>(
|
||||||
|
auth_id: Option<String>,
|
||||||
|
language: Option<String>,
|
||||||
|
api: &'a ApiConfig,
|
||||||
|
parts: Parts,
|
||||||
|
) -> Pin<Box<dyn Future<Output = Response<Body>> + Send + 'a>> {
|
||||||
|
Box::pin(get_index_future(auth_id, language, api, parts))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_index_future(
|
||||||
auth_id: Option<String>,
|
auth_id: Option<String>,
|
||||||
language: Option<String>,
|
language: Option<String>,
|
||||||
api: &ApiConfig,
|
api: &ApiConfig,
|
||||||
parts: Parts,
|
parts: Parts,
|
||||||
) -> Response<Body> {
|
) -> Response<Body> {
|
||||||
|
|
||||||
|
// fixme: make all IO async
|
||||||
|
|
||||||
let (userid, csrf_token) = match auth_id {
|
let (userid, csrf_token) = match auth_id {
|
||||||
Some(auth_id) => {
|
Some(auth_id) => {
|
||||||
let auth_id = auth_id.parse::<Authid>();
|
let auth_id = auth_id.parse::<Authid>();
|
||||||
@ -169,7 +182,7 @@ async fn run() -> Result<(), Error> {
|
|||||||
&proxmox_backup::api2::ROUTER,
|
&proxmox_backup::api2::ROUTER,
|
||||||
RpcEnvironmentType::PUBLIC,
|
RpcEnvironmentType::PUBLIC,
|
||||||
default_api_auth(),
|
default_api_auth(),
|
||||||
get_index,
|
&get_index,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
config.add_alias("novnc", "/usr/share/novnc-pve");
|
config.add_alias("novnc", "/usr/share/novnc-pve");
|
||||||
|
Loading…
Reference in New Issue
Block a user