rest-server: update example to new ApiConfig

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2023-03-02 15:53:30 +01:00
parent 1f373b9276
commit dcd6e85ab2

View File

@ -14,7 +14,7 @@ use proxmox_router::{
}; };
use proxmox_schema::api; use proxmox_schema::api;
use proxmox_rest_server::{ApiConfig, AuthError, RestEnvironment, RestServer, ServerAdapter}; use proxmox_rest_server::{ApiConfig, AuthError, RestEnvironment, RestServer};
// Create a Dummy User information system // Create a Dummy User information system
struct DummyUserInfo; struct DummyUserInfo;
@ -32,19 +32,14 @@ impl UserInformation for DummyUserInfo {
} }
} }
struct MinimalServer; fn check_auth<'a>(
_headers: &'a HeaderMap,
// implement the server adapter _method: &'a Method,
impl ServerAdapter for MinimalServer {
// normally this would check and authenticate the user
fn check_auth(
&self,
_headers: &HeaderMap,
_method: &Method,
) -> Pin< ) -> Pin<
Box< Box<
dyn Future<Output = Result<(String, Box<dyn UserInformation + Sync + Send>), AuthError>> dyn Future<Output = Result<(String, Box<dyn UserInformation + Sync + Send>), AuthError>>
+ Send, + Send
+ 'a,
>, >,
> { > {
Box::pin(async move { Box::pin(async move {
@ -55,9 +50,7 @@ impl ServerAdapter for MinimalServer {
}) })
} }
// this should return the index page of the webserver, iow. what the user browses to
fn get_index( fn get_index(
&self,
_env: RestEnvironment, _env: RestEnvironment,
_parts: Parts, _parts: Parts,
) -> Pin<Box<dyn Future<Output = Response<Body>> + Send>> { ) -> Pin<Box<dyn Future<Output = Response<Body>> + Send>> {
@ -68,7 +61,6 @@ impl ServerAdapter for MinimalServer {
.unwrap() .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
@ -199,12 +191,10 @@ const ROUTER: Router = Router::new()
async fn run() -> Result<(), Error> { async fn run() -> Result<(), Error> {
// we first have to configure the api environment (basedir etc.) // we first have to configure the api environment (basedir etc.)
let config = ApiConfig::new( let config = ApiConfig::new("/var/tmp/", RpcEnvironmentType::PUBLIC)
"/var/tmp/", .default_api2_handler(&ROUTER)
&ROUTER, .auth_handler_func(check_auth)
RpcEnvironmentType::PUBLIC, .index_handler_func(get_index);
MinimalServer,
)?;
let rest_server = RestServer::new(config); let rest_server = RestServer::new(config);
// then we have to create a daemon that listens, accepts and serves the api to clients // then we have to create a daemon that listens, accepts and serves the api to clients