diff --git a/apps/api/src/main.rs b/apps/api/src/main.rs index cb1256d..dd1af30 100644 --- a/apps/api/src/main.rs +++ b/apps/api/src/main.rs @@ -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)) - .await - .expect("Failed to establish database connection"); + let db_connection = Arc::new( + get_connection(&settings.database.url, Some(db_options)) + .await + .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) -> 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 diff --git a/apps/api/src/routes.rs b/apps/api/src/routes.rs index 89328a2..ddb1b51 100644 --- a/apps/api/src/routes.rs +++ b/apps/api/src/routes.rs @@ -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, // TODO: remove dead_code allowances when fields are used #[allow(dead_code)] pub service: Arc, } +pub type ServiceState = Arc; + pub struct AppService { - // + pub settings: ServiceState, } pub fn get_root_router(state: impl Into>) -> Router {