Refactor AppState and update database connection handling; integrate SettingsService

This commit is contained in:
GW_MC
2025-12-02 16:56:01 +08:00
parent fae951c902
commit f71cf370cd
2 changed files with 31 additions and 12 deletions

View File

@@ -1,14 +1,23 @@
mod configs; mod configs;
mod errors;
mod middlewares; mod middlewares;
mod routes; mod routes;
mod services;
mod tasks; mod tasks;
use std::sync::Arc;
use axum::Router; use axum::Router;
use database::{ConnectOptions, get_connection}; use database::get_connection;
use sea_orm::ConnectOptions;
use tracing::{debug, info}; use tracing::{debug, info};
use tracing_subscriber::fmt::format::{DefaultFields, Format}; use tracing_subscriber::fmt::format::{DefaultFields, Format};
use crate::configs::{ProgramSettings, get_program_settings, logging::LoggingSettings}; use crate::{
configs::{ProgramSettings, get_program_settings, logging::LoggingSettings},
routes::{AppService, AppState},
services::settings::SettingsService,
};
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
@@ -53,18 +62,17 @@ async fn main() {
options.max_connections(settings.database.max_connections); options.max_connections(settings.database.max_connections);
}; };
let db_connection = get_connection(&settings.database.url, Some(db_options)) let db_connection = Arc::new(
get_connection(&settings.database.url, Some(db_options))
.await .await
.expect("Failed to establish database connection"); .expect("Failed to establish database connection"),
);
info!("Database connection established."); info!("Database connection established.");
// build the axum app and run the server... // build the axum app and run the server...
info!("Starting application..."); info!("Starting application...");
let app: Router = routes::get_root_router(routes::AppState { let app: Router = routes::get_root_router(Arc::new(get_app_state(&db_connection)));
database_connection: db_connection,
service: std::sync::Arc::new(routes::AppService {}),
});
let address = format!("{}:{}", settings.server.address, settings.server.port); let address = format!("{}:{}", settings.server.address, settings.server.port);
info!("Starting server at http://{}", address); info!("Starting server at http://{}", address);
@@ -101,6 +109,15 @@ fn get_global_tracing_subscriber_builder(
} }
} }
fn get_app_state(db_connection: &Arc<sea_orm::DatabaseConnection>) -> AppState {
AppState {
database_connection: db_connection.clone(),
service: Arc::new(AppService {
settings: Arc::new(SettingsService::new(db_connection.clone())),
}),
}
}
// A small wrapper that holds a boxed `FormatTime` trait object and itself // A small wrapper that holds a boxed `FormatTime` trait object and itself
// implements `FormatTime`, allowing us to use it as a concrete type with // implements `FormatTime`, allowing us to use it as a concrete type with
// `builder.with_timer` while still picking the concrete timer implementation // `builder.with_timer` while still picking the concrete timer implementation

View File

@@ -3,20 +3,22 @@ use std::sync::Arc;
use axum::{Extension, Router}; use axum::{Extension, Router};
use migration::sea_orm::DatabaseConnection; use migration::sea_orm::DatabaseConnection;
use crate::middlewares; use crate::{middlewares, services::settings::SettingsStore};
#[derive(Clone)] #[derive(Clone)]
pub struct AppState { pub struct AppState {
// TODO: remove dead_code allowances when fields are used // TODO: remove dead_code allowances when fields are used
#[allow(dead_code)] #[allow(dead_code)]
pub database_connection: DatabaseConnection, pub database_connection: Arc<DatabaseConnection>,
// TODO: remove dead_code allowances when fields are used // TODO: remove dead_code allowances when fields are used
#[allow(dead_code)] #[allow(dead_code)]
pub service: Arc<AppService>, pub service: Arc<AppService>,
} }
pub type ServiceState<T: ?Sized + Send + Sync> = Arc<T>;
pub struct AppService { pub struct AppService {
// pub settings: ServiceState<dyn SettingsStore>,
} }
pub fn get_root_router(state: impl Into<Arc<AppState>>) -> Router { pub fn get_root_router(state: impl Into<Arc<AppState>>) -> Router {