cleanup code
This commit is contained in:
+26
-9
@@ -1,6 +1,6 @@
|
||||
mod api;
|
||||
mod auth;
|
||||
mod app;
|
||||
mod auth;
|
||||
mod config;
|
||||
mod database;
|
||||
mod db;
|
||||
@@ -45,8 +45,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
asset_version = %config.asset_version,
|
||||
"configuration loaded"
|
||||
);
|
||||
if let Some(path) = config.database_url.strip_prefix("sqlite://").and_then(|v| v.split('?').next()) {
|
||||
if let Some(parent) = std::path::Path::new(path).parent() { std::fs::create_dir_all(parent)?; }
|
||||
if let Some(path) = config
|
||||
.database_url
|
||||
.strip_prefix("sqlite://")
|
||||
.and_then(|v| v.split('?').next())
|
||||
{
|
||||
if let Some(parent) = std::path::Path::new(path).parent() {
|
||||
std::fs::create_dir_all(parent)?;
|
||||
}
|
||||
}
|
||||
info!("connecting to database");
|
||||
let db = Database::connect(&config.database_url, config.database_max_connections).await?;
|
||||
@@ -55,7 +61,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
info!(database_kind = ?db.kind(), "database migrations completed");
|
||||
|
||||
let storage = storage::Storage::from_config(config.storage.clone()).await?;
|
||||
info!(storage_driver = storage.backend_name(), "file storage ready");
|
||||
info!(
|
||||
storage_driver = storage.backend_name(),
|
||||
"file storage ready"
|
||||
);
|
||||
let state = Arc::new(AppState::new(
|
||||
db,
|
||||
config.asset_version.clone(),
|
||||
@@ -124,12 +133,20 @@ async fn run_migrations(db: &Database) -> Result<(), sqlx::migrate::MigrateError
|
||||
DatabaseKind::Postgres => std::path::Path::new("migrations/postgres"),
|
||||
DatabaseKind::MySql => std::path::Path::new("migrations/mysql"),
|
||||
};
|
||||
sqlx::migrate::Migrator::new(path).await?.run(db.pool()).await
|
||||
sqlx::migrate::Migrator::new(path)
|
||||
.await?
|
||||
.run(db.pool())
|
||||
.await
|
||||
}
|
||||
|
||||
fn database_kind_label(url: &str) -> &'static str {
|
||||
if url.starts_with("sqlite:") { "sqlite" }
|
||||
else if url.starts_with("postgres:") || url.starts_with("postgresql:") { "postgres" }
|
||||
else if url.starts_with("mysql:") { "mysql" }
|
||||
else { "unknown" }
|
||||
if url.starts_with("sqlite:") {
|
||||
"sqlite"
|
||||
} else if url.starts_with("postgres:") || url.starts_with("postgresql:") {
|
||||
"postgres"
|
||||
} else if url.starts_with("mysql:") {
|
||||
"mysql"
|
||||
} else {
|
||||
"unknown"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user