From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 1F9041FF0A8 for ; Thu, 20 Aug 2026 16:52:27 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id C60C1215A9; Thu, 20 Aug 2026 16:52:26 +0200 (CEST) From: Lukas Wagner To: pdm-devel@lists.proxmox.com Subject: [PATCH proxmox v2 02/20] rest-server: allow to inject shared state Date: Thu, 20 Aug 2026 16:52:02 +0200 Message-ID: <20260820145220.418032-3-l.wagner@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260820145220.418032-1-l.wagner@proxmox.com> References: <20260820145220.418032-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1787237517988 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.775 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: LW5C23DW256MKON622QZNDWVFM6E7PHI X-Message-ID-Hash: LW5C23DW256MKON622QZNDWVFM6E7PHI X-MailFrom: l.wagner@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox Datacenter Manager development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Let users of ApiConfig register a shared state registry while setting up the server and pass it on to API handlers via RestEnvironment. Signed-off-by: Lukas Wagner --- proxmox-rest-server/src/api_config.rs | 14 +++++++++++++- proxmox-rest-server/src/environment.rs | 6 +++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/proxmox-rest-server/src/api_config.rs b/proxmox-rest-server/src/api_config.rs index 3543f8c4..18ef65e9 100644 --- a/proxmox-rest-server/src/api_config.rs +++ b/proxmox-rest-server/src/api_config.rs @@ -17,7 +17,7 @@ use proxmox_daemon::command_socket::CommandSocket; use proxmox_http::Body; use proxmox_log::{FileLogOptions, FileLogger}; use proxmox_network_types::Cidr; -use proxmox_router::{Router, RpcEnvironmentType, UserInformation}; +use proxmox_router::{Router, RpcEnvironmentType, SharedStateRegistry, UserInformation}; use proxmox_sys::fs::{CreateOptions, create_path}; use crate::RestEnvironment; @@ -33,6 +33,8 @@ pub struct ApiConfig { handlers: Vec, auth_handler: Option, index_handler: Option, + /// State that API handlers can request via a `State` parameter. + pub(crate) shared_state: Option, pub(crate) privileged_addr: Option, // Name of the auth cookie that should be unset on 401 request. If `None` no cookie will be // removed. @@ -92,6 +94,7 @@ impl ApiConfig { index_handler: None, privileged_addr: None, auth_cookie_name: None, + shared_state: None, real_ip_header, real_ip_allow_from: None, @@ -366,6 +369,15 @@ impl ApiConfig { .push(Handler::unformatted_router(prefix, router)); self } + + /// Set the [`SharedStateRegistry`] from which API handlers get their `State` parameters. + /// + /// All types a handler requests must be registered using + /// [`SharedStateRegistry::register`], otherwise a call to the API handler will fail. + pub fn with_shared_state(mut self, shared_state: SharedStateRegistry) -> Self { + self.shared_state = Some(shared_state); + self + } } #[cfg(feature = "templates")] diff --git a/proxmox-rest-server/src/environment.rs b/proxmox-rest-server/src/environment.rs index 47ff5a4f..9133999f 100644 --- a/proxmox-rest-server/src/environment.rs +++ b/proxmox-rest-server/src/environment.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use serde_json::{Value, json}; -use proxmox_router::{RpcEnvironment, RpcEnvironmentType}; +use proxmox_router::{RpcEnvironment, RpcEnvironmentType, SharedStateRegistry}; use crate::ApiConfig; @@ -89,4 +89,8 @@ impl RpcEnvironment for RestEnvironment { fn get_client_ip(&self) -> Option { self.client_ip } + + fn shared_state(&self) -> Option<&SharedStateRegistry> { + self.api.shared_state.as_ref() + } } -- 2.47.3