Implement API setup with configuration management and startup tasks
- Add `Cargo.toml` for API with dependencies. - Create `config.rs` for managing application settings. - Implement logging and server settings in `config.rs`. - Add `main.rs` to initialize the application and handle database connections. - Introduce `task` module with startup tasks, including database migrations. - Update `.gitignore` to exclude `config.yaml` and remove `.gitkeep`.
This commit is contained in:
25
apps/api/src/tasks/startup.rs
Normal file
25
apps/api/src/tasks/startup.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
use migration::migrate_database;
|
||||
use tracing::{debug, info};
|
||||
|
||||
use crate::config::ProgramSettings;
|
||||
|
||||
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...");
|
||||
if config.database.migrate_on_startup {
|
||||
run_database_migrations(&config.database.url).await?;
|
||||
} else {
|
||||
info!("Database migration on startup is disabled. Skipping migration.");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn run_database_migrations(db_url: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Logic to run database migrations
|
||||
info!("Running database migrations...");
|
||||
debug!("Database URL: {}", db_url);
|
||||
migrate_database(db_url).await.map_err(Box::new)?;
|
||||
info!("Database migrations completed.");
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user