35 lines
1.0 KiB
Rust
35 lines
1.0 KiB
Rust
mod db_migrate;
|
|
mod init_admin;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use sea_orm::ConnectOptions;
|
|
use tracing::info;
|
|
|
|
use crate::configs::ProgramSettings;
|
|
use database::get_connection;
|
|
|
|
pub async fn run_startup_tasks(config: &ProgramSettings) -> Result<(), Box<dyn std::error::Error>> {
|
|
// Here you can add any startup tasks you want to run when the application starts.
|
|
info!("Running startup tasks...");
|
|
|
|
let db_options = |options: &mut ConnectOptions| {
|
|
options.max_connections(config.database.max_connections);
|
|
};
|
|
|
|
let db_connection = Arc::new(
|
|
get_connection(&config.database.url, Some(db_options))
|
|
.await
|
|
.map_err(|err| format!("Failed to establish database connection: {}", err))?,
|
|
);
|
|
|
|
if config.database.migrate_on_startup {
|
|
db_migrate::run_database_migrations(&config.database.url).await?;
|
|
} else {
|
|
info!("Database migration on startup is disabled. Skipping migration.");
|
|
}
|
|
init_admin::init_admin(config, db_connection.clone()).await?;
|
|
|
|
Ok(())
|
|
}
|