diff --git a/proxmox-router/Cargo.toml b/proxmox-router/Cargo.toml index 57334a20..7e4b84a3 100644 --- a/proxmox-router/Cargo.toml +++ b/proxmox-router/Cargo.toml @@ -11,8 +11,8 @@ exclude = [ "debian" ] [dependencies] anyhow = "1.0" env_logger = { version = "0.9", optional = true } -http = "0.2" -hyper = { version = "0.14", features = [ "full" ] } +http = { version = "0.2", optional = true } +hyper = { version = "0.14", features = [ "full" ], optional = true } nix = "0.24.1" percent-encoding = "2.1" serde_json = "1.0" @@ -29,6 +29,7 @@ proxmox-schema = { path = "../proxmox-schema", version = "1.1" } proxmox-async = { path = "../proxmox-async", version = "0.4.1" } [features] -default = [ "cli" ] +default = [ "cli", "server" ] cli = [ "dep:env_logger", "dep:libc", "dep:rustyline", "dep:tokio" ] +server = [ "dep:http", "dep:hyper" ] test-harness = [ "proxmox-schema/test-harness" ] diff --git a/proxmox-router/src/cli/command.rs b/proxmox-router/src/cli/command.rs index 8c809696..8ae889f9 100644 --- a/proxmox-router/src/cli/command.rs +++ b/proxmox-router/src/cli/command.rs @@ -68,6 +68,7 @@ async fn handle_simple_command_future( ApiHandler::StreamingAsync(handler) => (handler)(params, cli_cmd.info, &mut rpcenv) .await .and_then(|r| r.to_value().map_err(Error::from)), + #[cfg(feature = "server")] ApiHandler::AsyncHttp(_) => { let err_msg = "CliHandler does not support ApiHandler::AsyncHttp - internal error"; print_simple_usage_error(prefix, cli_cmd, err_msg); @@ -119,6 +120,7 @@ fn handle_simple_command( print_simple_usage_error(prefix, cli_cmd, err_msg); return Err(format_err!("{}", err_msg)); } + #[cfg(feature = "server")] ApiHandler::AsyncHttp(_) => { let err_msg = "CliHandler does not support ApiHandler::AsyncHttp - internal error"; print_simple_usage_error(prefix, cli_cmd, err_msg); diff --git a/proxmox-router/src/format.rs b/proxmox-router/src/format.rs index 9f805b3c..a265a2c8 100644 --- a/proxmox-router/src/format.rs +++ b/proxmox-router/src/format.rs @@ -7,7 +7,9 @@ use anyhow::Error; use proxmox_schema::format::*; use proxmox_schema::ObjectSchemaType; -use crate::{ApiHandler, ApiMethod}; +#[cfg(feature = "server")] +use crate::ApiHandler; +use crate::ApiMethod; fn dump_method_definition(method: &str, path: &str, def: Option<&ApiMethod>) -> Option { let style = ParameterDisplayStyle::Config; @@ -19,8 +21,12 @@ fn dump_method_definition(method: &str, path: &str, def: Option<&ApiMethod>) -> let return_descr = dump_api_return_schema(&api_method.returns, style); + #[cfg(feature = "server")] let mut method = method; + #[cfg(not(feature = "server"))] + let method = method; + #[cfg(feature = "server")] if let ApiHandler::AsyncHttp(_) = api_method.handler { method = if method == "POST" { "UPLOAD" } else { method }; method = if method == "GET" { "DOWNLOAD" } else { method }; diff --git a/proxmox-router/src/lib.rs b/proxmox-router/src/lib.rs index 84f39f84..7f4f2d97 100644 --- a/proxmox-router/src/lib.rs +++ b/proxmox-router/src/lib.rs @@ -7,6 +7,7 @@ pub mod cli; // this is public so the `http_err!` macro can access `http::StatusCode` through it #[doc(hidden)] +#[cfg(feature = "server")] pub mod error; mod permission; @@ -15,6 +16,7 @@ mod rpc_environment; mod serializable_return; #[doc(inline)] +#[cfg(feature = "server")] pub use error::HttpError; pub use permission::*; diff --git a/proxmox-router/src/router.rs b/proxmox-router/src/router.rs index 224dce11..e9a669a4 100644 --- a/proxmox-router/src/router.rs +++ b/proxmox-router/src/router.rs @@ -4,8 +4,11 @@ use std::future::Future; use std::pin::Pin; use anyhow::Error; +#[cfg(feature = "server")] use http::request::Parts; +#[cfg(feature = "server")] use http::{Method, Response}; +#[cfg(feature = "server")] use hyper::Body; use percent_encoding::percent_decode_str; use serde_json::Value; @@ -176,6 +179,7 @@ pub type StreamingApiFuture<'a> = Pin< /// &ObjectSchema::new("Hello World Example (low level)", &[]) /// ); /// ``` +#[cfg(feature = "server")] pub type ApiAsyncHttpHandlerFn = &'static (dyn Fn( Parts, Body, @@ -188,15 +192,18 @@ pub type ApiAsyncHttpHandlerFn = &'static (dyn Fn( + 'static); /// The output of an asynchronous API handler is a future yielding a `Response`. +#[cfg(feature = "server")] pub type ApiResponseFuture = Pin, anyhow::Error>> + Send>>; /// Enum for different types of API handler functions. +#[non_exhaustive] pub enum ApiHandler { Sync(ApiHandlerFn), StreamingSync(StreamingApiHandlerFn), Async(ApiAsyncHandlerFn), StreamingAsync(StreamingApiAsyncHandlerFn), + #[cfg(feature = "server")] AsyncHttp(ApiAsyncHttpHandlerFn), } @@ -220,6 +227,7 @@ impl PartialEq for ApiHandler { (ApiHandler::StreamingAsync(l), ApiHandler::StreamingAsync(r)) => { core::mem::transmute::<_, usize>(l) == core::mem::transmute::<_, usize>(r) } + #[cfg(feature = "server")] (ApiHandler::AsyncHttp(l), ApiHandler::AsyncHttp(r)) => { core::mem::transmute::<_, usize>(l) == core::mem::transmute::<_, usize>(r) } @@ -431,6 +439,7 @@ impl Router { /// - `components`: Path, split into individual components. /// - `method`: The HTTP method. /// - `uri_param`: Mutable hash map to store parameter from `MatchAll` router. + #[cfg(feature = "server")] pub fn find_method( &self, components: &[&str],