mirror of
https://github.com/house-of-vanity/yggman.git
synced 2025-12-16 19:17:56 +00:00
first commit
This commit is contained in:
53
src/core/app.rs
Normal file
53
src/core/app.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use std::sync::Arc;
|
||||
use crate::config::{AppConfig, ConfigManager};
|
||||
use crate::core::context::AppContext;
|
||||
use crate::core::module::ModuleManager;
|
||||
use crate::error::Result;
|
||||
use tokio::signal;
|
||||
|
||||
pub struct Application {
|
||||
module_manager: ModuleManager,
|
||||
}
|
||||
|
||||
impl Application {
|
||||
pub fn new(config: AppConfig) -> Self {
|
||||
let config_manager = Arc::new(ConfigManager::new(config));
|
||||
let context = Arc::new(AppContext::new(config_manager));
|
||||
let module_manager = ModuleManager::new(context);
|
||||
|
||||
Self {
|
||||
module_manager,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register_module(&mut self, module: Box<dyn crate::core::module::Module>) {
|
||||
self.module_manager.register(module);
|
||||
}
|
||||
|
||||
pub async fn run(mut self) -> Result<()> {
|
||||
tracing::info!("Starting application");
|
||||
|
||||
self.module_manager.init_all().await?;
|
||||
|
||||
self.module_manager.start_all().await?;
|
||||
|
||||
tokio::select! {
|
||||
_ = signal::ctrl_c() => {
|
||||
tracing::info!("Received SIGINT, shutting down");
|
||||
}
|
||||
}
|
||||
|
||||
self.shutdown().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn shutdown(self) -> Result<()> {
|
||||
tracing::info!("Shutting down application");
|
||||
|
||||
self.module_manager.stop_all().await?;
|
||||
|
||||
tracing::info!("Application shutdown complete");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
14
src/core/context.rs
Normal file
14
src/core/context.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use std::sync::Arc;
|
||||
use crate::config::ConfigManager;
|
||||
|
||||
pub struct AppContext {
|
||||
pub config_manager: Arc<ConfigManager>,
|
||||
}
|
||||
|
||||
impl AppContext {
|
||||
pub fn new(config_manager: Arc<ConfigManager>) -> Self {
|
||||
Self {
|
||||
config_manager,
|
||||
}
|
||||
}
|
||||
}
|
||||
3
src/core/mod.rs
Normal file
3
src/core/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub mod app;
|
||||
pub mod context;
|
||||
pub mod module;
|
||||
57
src/core/module.rs
Normal file
57
src/core/module.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
use async_trait::async_trait;
|
||||
use std::sync::Arc;
|
||||
use crate::core::context::AppContext;
|
||||
use crate::error::Result;
|
||||
|
||||
#[async_trait]
|
||||
pub trait Module: Send + Sync {
|
||||
fn name(&self) -> &str;
|
||||
|
||||
async fn init(&mut self, context: Arc<AppContext>) -> Result<()>;
|
||||
|
||||
async fn start(&self) -> Result<()>;
|
||||
|
||||
async fn stop(&self) -> Result<()>;
|
||||
}
|
||||
|
||||
pub struct ModuleManager {
|
||||
modules: Vec<Box<dyn Module>>,
|
||||
context: Arc<AppContext>,
|
||||
}
|
||||
|
||||
impl ModuleManager {
|
||||
pub fn new(context: Arc<AppContext>) -> Self {
|
||||
Self {
|
||||
modules: Vec::new(),
|
||||
context,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register(&mut self, module: Box<dyn Module>) {
|
||||
self.modules.push(module);
|
||||
}
|
||||
|
||||
pub async fn init_all(&mut self) -> Result<()> {
|
||||
for module in &mut self.modules {
|
||||
tracing::info!("Initializing module: {}", module.name());
|
||||
module.init(self.context.clone()).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn start_all(&self) -> Result<()> {
|
||||
for module in &self.modules {
|
||||
tracing::info!("Starting module: {}", module.name());
|
||||
module.start().await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn stop_all(&self) -> Result<()> {
|
||||
for module in self.modules.iter().rev() {
|
||||
tracing::info!("Stopping module: {}", module.name());
|
||||
module.stop().await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user