refactor(app): add AppContext skeleton wiring (#1909)

This commit is contained in:
安正超
2026-02-22 22:41:42 +08:00
committed by GitHub
parent 4211652991
commit 84053484e6
3 changed files with 121 additions and 0 deletions
+113
View File
@@ -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<IamSys<ObjectStore>>;
}
/// KMS interface for application-layer use-cases.
pub trait KmsInterface: Send + Sync {
fn handle(&self) -> Arc<KmsServiceManager>;
}
/// Default IAM interface adapter.
pub struct IamHandle {
iam: Arc<IamSys<ObjectStore>>,
}
impl IamHandle {
pub fn new(iam: Arc<IamSys<ObjectStore>>) -> Self {
Self { iam }
}
}
impl IamInterface for IamHandle {
fn handle(&self) -> Arc<IamSys<ObjectStore>> {
self.iam.clone()
}
}
/// Default KMS interface adapter.
pub struct KmsHandle {
kms: Arc<KmsServiceManager>,
}
impl KmsHandle {
pub fn new(kms: Arc<KmsServiceManager>) -> Self {
Self { kms }
}
}
impl KmsInterface for KmsHandle {
fn handle(&self) -> Arc<KmsServiceManager> {
self.kms.clone()
}
}
/// Application-layer context with explicit dependencies.
#[derive(Clone)]
pub struct AppContext {
object_store: Arc<ECStore>,
iam: Arc<dyn IamInterface>,
kms: Arc<dyn KmsInterface>,
}
impl AppContext {
pub fn new(object_store: Arc<ECStore>, iam: Arc<dyn IamInterface>, kms: Arc<dyn KmsInterface>) -> Self {
Self { object_store, iam, kms }
}
pub fn with_default_interfaces(
object_store: Arc<ECStore>,
iam: Arc<IamSys<ObjectStore>>,
kms: Arc<KmsServiceManager>,
) -> Self {
Self::new(object_store, Arc::new(IamHandle::new(iam)), Arc::new(KmsHandle::new(kms)))
}
pub fn object_store(&self) -> Arc<ECStore> {
self.object_store.clone()
}
pub fn iam(&self) -> Arc<dyn IamInterface> {
self.iam.clone()
}
pub fn kms(&self) -> Arc<dyn KmsInterface> {
self.kms.clone()
}
}
static GLOBAL_APP_CONTEXT: OnceLock<Arc<AppContext>> = OnceLock::new();
/// Initialize global application context once and return the canonical instance.
pub fn init_global_app_context(context: AppContext) -> Arc<AppContext> {
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<Arc<AppContext>> {
GLOBAL_APP_CONTEXT.get().cloned()
}
+2
View File
@@ -14,3 +14,5 @@
//! Application layer module entry.
//! Concrete use-case modules will be introduced incrementally in Phase 3.
pub mod context;
+6
View File
@@ -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