all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Lukas Wagner <l.wagner@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [PATCH datacenter-manager v6 10/21] context: register PdmApplication in router
Date: Fri,  2 Oct 2026 11:36:05 +0200	[thread overview]
Message-ID: <20261002093616.206933-11-l.wagner@proxmox.com> (raw)
In-Reply-To: <20261002093616.206933-1-l.wagner@proxmox.com>

Register the PdmApplication handle in a SharedStateRegistry and pass it
to the REST server config via with_shared_state(), so API handlers can
request it through the `#[state] app: PdmApplication` extractor instead
of reaching for context::pdm_application() or other globals directly.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 cli/admin/src/main.rs                             | 10 ++++++++--
 server/src/bin/proxmox-datacenter-api/main.rs     | 15 ++++++++++-----
 .../src/bin/proxmox-datacenter-privileged-api.rs  | 15 ++++++++++-----
 3 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/cli/admin/src/main.rs b/cli/admin/src/main.rs
index 9c0bc9ba..a091585d 100644
--- a/cli/admin/src/main.rs
+++ b/cli/admin/src/main.rs
@@ -3,15 +3,17 @@ use core::matches;
 use anyhow::{Context, Error};
 use serde_json::{Value, json};
 
-use proxmox_router::RpcEnvironment;
 use proxmox_router::cli::{
     CliCommand, CliCommandMap, CliEnvironment, ColumnConfig, OUTPUT_FORMAT,
     default_table_format_options, format_and_print_result_full, get_output_format,
     run_async_cli_command,
 };
+use proxmox_router::{RpcEnvironment, SharedStateRegistry};
 use proxmox_schema::api;
 use proxmox_sys::fs::CreateOptions;
 
+use server::context::PdmApplication;
+
 mod acme;
 mod cert;
 mod remotes;
@@ -36,7 +38,7 @@ async fn run() -> Result<(), Error> {
         .init()
         .context("failed to set up logger")?;
 
-    server::context::init().context("could not set up server context")?;
+    let app = server::context::init().context("could not set up server context")?;
 
     let cmd_def = CliCommandMap::new()
         .insert("acme", acme::acme_mgmt_cli())
@@ -68,7 +70,11 @@ async fn run() -> Result<(), Error> {
             .context("failed to activate the socket")?;
     }
 
+    let mut shared_state = SharedStateRegistry::default();
+    shared_state.register::<PdmApplication>(app)?;
+
     let mut rpcenv = CliEnvironment::new();
+    rpcenv.set_shared_state_registry(shared_state);
     rpcenv.set_auth_id(Some("root@pam".into()));
 
     run_async_cli_command(cmd_def, rpcenv).await;
diff --git a/server/src/bin/proxmox-datacenter-api/main.rs b/server/src/bin/proxmox-datacenter-api/main.rs
index 57aa6e22..64326b9b 100644
--- a/server/src/bin/proxmox-datacenter-api/main.rs
+++ b/server/src/bin/proxmox-datacenter-api/main.rs
@@ -18,7 +18,7 @@ use url::form_urlencoded;
 
 use proxmox_lang::try_block;
 use proxmox_rest_server::{ApiConfig, RestEnvironment, RestServer};
-use proxmox_router::{RpcEnvironment, RpcEnvironmentType};
+use proxmox_router::{RpcEnvironment, RpcEnvironmentType, SharedStateRegistry};
 use proxmox_sys::fs::CreateOptions;
 
 use pdm_buildcfg::configdir;
@@ -28,6 +28,7 @@ use proxmox_auth_api::api::assemble_csrf_prevention_token;
 
 use server::auth;
 use server::auth::csrf::csrf_secret;
+use server::context::PdmApplication;
 use server::metric_collection;
 use server::resource_cache;
 use server::task_utils;
@@ -67,9 +68,9 @@ fn main() -> Result<(), Error> {
     }
 
     proxmox_product_config::init(pdm_config::api_user()?, pdm_config::priv_user()?);
-    server::context::init()?;
+    let app = server::context::init()?;
 
-    proxmox_async::runtime::main(run(debug))
+    proxmox_async::runtime::main(run(app, debug))
 }
 
 async fn get_index_future(env: RestEnvironment, parts: Parts) -> Response<proxmox_http::Body> {
@@ -144,7 +145,7 @@ async fn get_index_future(env: RestEnvironment, parts: Parts) -> Response<proxmo
     resp
 }
 
-async fn run(debug: bool) -> Result<(), Error> {
+async fn run(app: PdmApplication, debug: bool) -> Result<(), Error> {
     auth::init(false);
 
     proxmox_acme_api::init(configdir!("/acme"), false)?;
@@ -159,6 +160,9 @@ async fn run(debug: bool) -> Result<(), Error> {
 
     let indexpath = Path::new(pdm_buildcfg::JS_DIR).join("index.hbs");
 
+    let mut shared_state = SharedStateRegistry::default();
+    shared_state.register::<PdmApplication>(app.clone())?;
+
     let config = ApiConfig::new(pdm_buildcfg::JS_DIR, RpcEnvironmentType::PUBLIC)
         .privileged_addr(
             std::os::unix::net::SocketAddr::from_pathname(
@@ -194,7 +198,8 @@ async fn run(debug: bool) -> Result<(), Error> {
             Some(dir_opts),
             Some(file_opts),
             &mut command_sock,
-        )?;
+        )?
+        .with_shared_state(shared_state);
 
     let rest_server = RestServer::new(config);
     let redirector = proxmox_rest_server::Redirector::new();
diff --git a/server/src/bin/proxmox-datacenter-privileged-api.rs b/server/src/bin/proxmox-datacenter-privileged-api.rs
index 59d30513..3815ab03 100644
--- a/server/src/bin/proxmox-datacenter-privileged-api.rs
+++ b/server/src/bin/proxmox-datacenter-privileged-api.rs
@@ -11,9 +11,10 @@ use tracing::level_filters::LevelFilter;
 
 use proxmox_lang::try_block;
 use proxmox_rest_server::{ApiConfig, RestServer};
-use proxmox_router::RpcEnvironmentType;
+use proxmox_router::{RpcEnvironmentType, SharedStateRegistry};
 use proxmox_sys::fs::CreateOptions;
 
+use server::context::PdmApplication;
 use server::{api_cache, auth};
 
 use pdm_buildcfg::configdir;
@@ -54,9 +55,9 @@ fn main() -> Result<(), Error> {
         }
     }
 
-    server::context::init()?;
+    let app = server::context::init()?;
 
-    proxmox_async::runtime::main(run())
+    proxmox_async::runtime::main(run(app))
 }
 
 fn create_directories() -> Result<(), Error> {
@@ -114,7 +115,7 @@ fn create_directories() -> Result<(), Error> {
     Ok(())
 }
 
-async fn run() -> Result<(), Error> {
+async fn run(app: PdmApplication) -> Result<(), Error> {
     auth::init(true);
 
     proxmox_acme_api::init(configdir!("/acme"), true)?;
@@ -139,6 +140,9 @@ async fn run() -> Result<(), Error> {
     let dir_opts = CreateOptions::new().owner(api_user.uid).group(api_user.gid);
     let file_opts = CreateOptions::new().owner(api_user.uid).group(api_user.gid);
 
+    let mut shared_state = SharedStateRegistry::default();
+    shared_state.register::<PdmApplication>(app)?;
+
     let config = ApiConfig::new(pdm_buildcfg::JS_DIR, RpcEnvironmentType::PRIVILEGED)
         .auth_handler_func(|h, m| Box::pin(auth::check_auth(h, m)))
         .formatted_router(&["api2"], &server::api::ROUTER)
@@ -153,7 +157,8 @@ async fn run() -> Result<(), Error> {
             Some(dir_opts),
             Some(file_opts),
             &mut command_sock,
-        )?;
+        )?
+        .with_shared_state(shared_state);
 
     let rest_server = RestServer::new(config);
     proxmox_rest_server::init_worker_tasks(pdm_buildcfg::PDM_LOG_DIR_M!().into(), file_opts)?;
-- 
2.47.3





  parent reply	other threads:[~2026-10-02  9:36 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-02  9:35 [PATCH datacenter-manager/proxmox v6 00/21] inject application context via API macro for easier integration testing Lukas Wagner
2026-10-02  9:35 ` [PATCH proxmox v6 01/21] router: introduce shared state Lukas Wagner
2026-10-02  9:35 ` [PATCH proxmox v6 02/21] rest-server: allow to inject " Lukas Wagner
2026-10-02  9:35 ` [PATCH proxmox v6 03/21] api-macro: support #[state] attribute for state injection Lukas Wagner
2026-10-02  9:35 ` [PATCH proxmox v6 04/21] product-config: add ProductConfig type Lukas Wagner
2026-10-02  9:36 ` [PATCH datacenter-manager v6 05/21] context: promote context to a dir-style module Lukas Wagner
2026-10-02  9:36 ` [PATCH datacenter-manager v6 06/21] pdm-config: remotes: rename trait methods to read/write/lock Lukas Wagner
2026-10-02  9:36 ` [PATCH datacenter-manager v6 07/21] pdm-config: subscriptions: " Lukas Wagner
2026-10-02  9:36 ` [PATCH datacenter-manager v6 08/21] context: introduce a ContextFactory to build application context Lukas Wagner
2026-10-02  9:36 ` [PATCH datacenter-manager v6 09/21] context: establish PdmApplication object Lukas Wagner
2026-10-02  9:36 ` Lukas Wagner [this message]
2026-10-02  9:36 ` [PATCH datacenter-manager v6 11/21] connection: use client factory from PdmApplication handle Lukas Wagner
2026-10-02  9:36 ` [PATCH datacenter-manager v6 12/21] remote iterator: pass remote config reader explicitly Lukas Wagner
2026-10-02  9:36 ` [PATCH datacenter-manager v6 13/21] parallel fetcher: pass arguments to closure in a single type Lukas Wagner
2026-10-02  9:36 ` [PATCH datacenter-manager v6 14/21] server: migrate existing ParallelFetcher users to use PdmApplication Lukas Wagner
2026-10-02  9:36 ` [PATCH datacenter-manager v6 15/21] tests: add helpers for building API-handler-level integration tests Lukas Wagner
2026-10-02  9:36 ` [PATCH datacenter-manager v6 16/21] tests: add example tests for SDN API routes Lukas Wagner
2026-10-02  9:36 ` [PATCH datacenter-manager v6 17/21] api-cache: add wrapper type Lukas Wagner
2026-10-02  9:36 ` [PATCH datacenter-manager v6 18/21] context: provide api-cache on the app object Lukas Wagner
2026-10-02  9:36 ` [PATCH datacenter-manager v6 19/21] api: subscriptions: use PdmApplication instead of globals Lukas Wagner
2026-10-02  9:36 ` [PATCH datacenter-manager v6 20/21] pdm-config: subscriptions: drop unused accessor functions Lukas Wagner
2026-10-02  9:36 ` [PATCH datacenter-manager v6 21/21] tests: add example tests for remote subscription management Lukas Wagner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261002093616.206933-11-l.wagner@proxmox.com \
    --to=l.wagner@proxmox.com \
    --cc=pdm-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal