logging
This commit is contained in:
+32
-2
@@ -14,7 +14,7 @@ use config::Config;
|
||||
use database::{Database, DatabaseKind};
|
||||
use state::AppState;
|
||||
use tokio::net::TcpListener;
|
||||
use tracing::info;
|
||||
use tracing::{info, warn};
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
#[tokio::main]
|
||||
@@ -23,13 +23,31 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
init_tracing();
|
||||
|
||||
let config = Config::from_env()?;
|
||||
info!(
|
||||
host = %config.host,
|
||||
port = config.port,
|
||||
database_kind = %database_kind_label(&config.database_url),
|
||||
database_max_connections = config.database_max_connections,
|
||||
static_dir = %config.static_dir,
|
||||
files_dir = %config.files_dir,
|
||||
upload_max_size_bytes = config.upload_max_size_bytes,
|
||||
registration_enabled = config.registration_enabled,
|
||||
frontend_log_level = %config.frontend_log_level,
|
||||
smtp_configured = config.smtp.is_some(),
|
||||
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)?; }
|
||||
}
|
||||
info!("connecting to database");
|
||||
let db = Database::connect(&config.database_url, config.database_max_connections).await?;
|
||||
info!(database_kind = ?db.kind(), "database connection established");
|
||||
run_migrations(&db).await?;
|
||||
info!(database_kind = ?db.kind(), "database migrations completed");
|
||||
|
||||
std::fs::create_dir_all(&config.files_dir)?;
|
||||
info!(files_dir = %config.files_dir, "file storage ready");
|
||||
let state = Arc::new(AppState::new(
|
||||
db,
|
||||
config.asset_version.clone(),
|
||||
@@ -37,6 +55,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
config.upload_max_size_bytes,
|
||||
config.smtp.clone(),
|
||||
config.registration_enabled,
|
||||
config.frontend_log_level.clone(),
|
||||
));
|
||||
let app = app::router(
|
||||
state,
|
||||
@@ -50,6 +69,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
axum::serve(listener, app)
|
||||
.with_graceful_shutdown(shutdown_signal())
|
||||
.await?;
|
||||
info!("RustPad stopped cleanly");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -78,7 +98,10 @@ async fn shutdown_signal() {
|
||||
};
|
||||
#[cfg(not(unix))]
|
||||
let terminate = std::future::pending::<()>();
|
||||
tokio::select! { () = ctrl_c => {}, () = terminate => {} }
|
||||
tokio::select! {
|
||||
() = ctrl_c => warn!("shutdown requested by Ctrl+C"),
|
||||
() = terminate => warn!("shutdown requested by SIGTERM"),
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_migrations(db: &Database) -> Result<(), sqlx::migrate::MigrateError> {
|
||||
@@ -89,3 +112,10 @@ async fn run_migrations(db: &Database) -> Result<(), sqlx::migrate::MigrateError
|
||||
};
|
||||
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" }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user