From 84053484e6bcaf25f76923887968294464c324be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E6=AD=A3=E8=B6=85?= Date: Sun, 22 Feb 2026 22:41:42 +0800 Subject: [PATCH] refactor(app): add AppContext skeleton wiring (#1909) --- rustfs/src/app/context.rs | 113 ++++++++++++++++++++++++++++++++++++++ rustfs/src/app/mod.rs | 2 + rustfs/src/main.rs | 6 ++ 3 files changed, 121 insertions(+) create mode 100644 rustfs/src/app/context.rs diff --git a/rustfs/src/app/context.rs b/rustfs/src/app/context.rs new file mode 100644 index 000000000..10ae85e86 --- /dev/null +++ b/rustfs/src/app/context.rs @@ -0,0 +1,113 @@ +// Copyright 2024 RustFS Team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Application-layer dependency context. +//! This module introduces explicit dependency injection entry points +//! for storage, IAM, and KMS handles. +#![allow(dead_code)] + +use rustfs_ecstore::store::ECStore; +use rustfs_iam::{store::object::ObjectStore, sys::IamSys}; +use rustfs_kms::KmsServiceManager; +use std::sync::{Arc, OnceLock}; + +/// IAM interface for application-layer use-cases. +pub trait IamInterface: Send + Sync { + fn handle(&self) -> Arc>; +} + +/// KMS interface for application-layer use-cases. +pub trait KmsInterface: Send + Sync { + fn handle(&self) -> Arc; +} + +/// Default IAM interface adapter. +pub struct IamHandle { + iam: Arc>, +} + +impl IamHandle { + pub fn new(iam: Arc>) -> Self { + Self { iam } + } +} + +impl IamInterface for IamHandle { + fn handle(&self) -> Arc> { + self.iam.clone() + } +} + +/// Default KMS interface adapter. +pub struct KmsHandle { + kms: Arc, +} + +impl KmsHandle { + pub fn new(kms: Arc) -> Self { + Self { kms } + } +} + +impl KmsInterface for KmsHandle { + fn handle(&self) -> Arc { + self.kms.clone() + } +} + +/// Application-layer context with explicit dependencies. +#[derive(Clone)] +pub struct AppContext { + object_store: Arc, + iam: Arc, + kms: Arc, +} + +impl AppContext { + pub fn new(object_store: Arc, iam: Arc, kms: Arc) -> Self { + Self { object_store, iam, kms } + } + + pub fn with_default_interfaces( + object_store: Arc, + iam: Arc>, + kms: Arc, + ) -> Self { + Self::new(object_store, Arc::new(IamHandle::new(iam)), Arc::new(KmsHandle::new(kms))) + } + + pub fn object_store(&self) -> Arc { + self.object_store.clone() + } + + pub fn iam(&self) -> Arc { + self.iam.clone() + } + + pub fn kms(&self) -> Arc { + self.kms.clone() + } +} + +static GLOBAL_APP_CONTEXT: OnceLock> = OnceLock::new(); + +/// Initialize global application context once and return the canonical instance. +pub fn init_global_app_context(context: AppContext) -> Arc { + GLOBAL_APP_CONTEXT.get_or_init(|| Arc::new(context)).clone() +} + +/// Get global application context if it has been initialized. +pub fn get_global_app_context() -> Option> { + GLOBAL_APP_CONTEXT.get().cloned() +} diff --git a/rustfs/src/app/mod.rs b/rustfs/src/app/mod.rs index e5b45b024..bbbcde445 100644 --- a/rustfs/src/app/mod.rs +++ b/rustfs/src/app/mod.rs @@ -14,3 +14,5 @@ //! Application layer module entry. //! Concrete use-case modules will be introduced incrementally in Phase 3. + +pub mod context; diff --git a/rustfs/src/main.rs b/rustfs/src/main.rs index 1b148d3fd..c8b388d01 100644 --- a/rustfs/src/main.rs +++ b/rustfs/src/main.rs @@ -28,6 +28,7 @@ mod update; mod version; // Ensure the correct path for parse_license is imported +use crate::app::context::{AppContext, init_global_app_context}; use crate::init::{ add_bucket_notification_configuration, init_buffer_profile_system, init_kms_system, init_update_check, print_server_info, }; @@ -365,6 +366,11 @@ async fn run(config: config::Config) -> Result<()> { init_iam_sys(store.clone()).await.map_err(Error::other)?; readiness.mark_stage(SystemStage::IamReady); + let iam_interface = + rustfs_iam::get().map_err(|e| Error::other(format!("initialize app context IAM dependency failed: {e}")))?; + let kms_interface = rustfs_kms::get_global_kms_service_manager().unwrap_or_else(rustfs_kms::init_global_kms_service_manager); + let _app_context = init_global_app_context(AppContext::with_default_interfaces(store.clone(), iam_interface, kms_interface)); + add_bucket_notification_configuration(buckets.clone()).await; // Initialize the global notification system