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:
GW_MC
2025-11-26 19:42:44 +08:00
parent 56c1161e97
commit e849b71a40
9 changed files with 840 additions and 4 deletions

View 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(())
}