Init
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
pub mod state;
|
||||
|
||||
use crate::config::AppConfig;
|
||||
use crate::database::Database;
|
||||
use migration::{Migrator, MigratorTrait};
|
||||
pub use state::AppState;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct App {
|
||||
pub state: AppState,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub async fn build(config: AppConfig) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let db_manager = Database::init(&config.database.url).await?;
|
||||
let db = db_manager.get_connection().clone();
|
||||
|
||||
Migrator::up(&db, None).await?;
|
||||
|
||||
let state = AppState {
|
||||
db,
|
||||
config: Arc::new(config),
|
||||
};
|
||||
|
||||
Ok(Self { state })
|
||||
}
|
||||
|
||||
pub async fn run(self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
tracing::info!("Starting services...");
|
||||
|
||||
tokio::select! {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
use crate::config::AppConfig;
|
||||
use sea_orm::DatabaseConnection;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AppState {
|
||||
pub db: DatabaseConnection,
|
||||
pub config: Arc<AppConfig>,
|
||||
}
|
||||
Reference in New Issue
Block a user