Refactor AppState and update database connection handling; integrate SettingsService
This commit is contained in:
@@ -1,14 +1,23 @@
|
||||
mod configs;
|
||||
mod errors;
|
||||
mod middlewares;
|
||||
mod routes;
|
||||
mod services;
|
||||
mod tasks;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::Router;
|
||||
use database::{ConnectOptions, get_connection};
|
||||
use database::get_connection;
|
||||
use sea_orm::ConnectOptions;
|
||||
use tracing::{debug, info};
|
||||
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]
|
||||
async fn main() {
|
||||
@@ -53,18 +62,17 @@ async fn main() {
|
||||
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
|
||||
.expect("Failed to establish database connection");
|
||||
.expect("Failed to establish database connection"),
|
||||
);
|
||||
|
||||
info!("Database connection established.");
|
||||
|
||||
// build the axum app and run the server...
|
||||
info!("Starting application...");
|
||||
let app: Router = routes::get_root_router(routes::AppState {
|
||||
database_connection: db_connection,
|
||||
service: std::sync::Arc::new(routes::AppService {}),
|
||||
});
|
||||
let app: Router = routes::get_root_router(Arc::new(get_app_state(&db_connection)));
|
||||
|
||||
let address = format!("{}:{}", settings.server.address, settings.server.port);
|
||||
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
|
||||
// implements `FormatTime`, allowing us to use it as a concrete type with
|
||||
// `builder.with_timer` while still picking the concrete timer implementation
|
||||
|
||||
@@ -3,20 +3,22 @@ use std::sync::Arc;
|
||||
use axum::{Extension, Router};
|
||||
use migration::sea_orm::DatabaseConnection;
|
||||
|
||||
use crate::middlewares;
|
||||
use crate::{middlewares, services::settings::SettingsStore};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AppState {
|
||||
// TODO: remove dead_code allowances when fields are used
|
||||
#[allow(dead_code)]
|
||||
pub database_connection: DatabaseConnection,
|
||||
pub database_connection: Arc<DatabaseConnection>,
|
||||
// TODO: remove dead_code allowances when fields are used
|
||||
#[allow(dead_code)]
|
||||
pub service: Arc<AppService>,
|
||||
}
|
||||
|
||||
pub type ServiceState<T: ?Sized + Send + Sync> = Arc<T>;
|
||||
|
||||
pub struct AppService {
|
||||
//
|
||||
pub settings: ServiceState<dyn SettingsStore>,
|
||||
}
|
||||
|
||||
pub fn get_root_router(state: impl Into<Arc<AppState>>) -> Router {
|
||||
|
||||
Reference in New Issue
Block a user