Move the database pool creation to the main function. Failure is fatal.

This commit is contained in:
Herbert Wolverson 2023-08-12 15:37:22 +00:00
parent 6f6d746d43
commit 71135aa0b2
2 changed files with 35 additions and 34 deletions

View File

@ -1,35 +1,44 @@
use tracing_subscriber::fmt::format::FmtSpan; use tracing_subscriber::fmt::format::FmtSpan;
mod server;
mod pki; mod pki;
mod server;
fn set_console_logging() -> anyhow::Result<()> { fn set_console_logging() -> anyhow::Result<()> {
// install global collector configured based on RUST_LOG env var. // install global collector configured based on RUST_LOG env var.
let subscriber = tracing_subscriber::fmt() let subscriber = tracing_subscriber::fmt()
// Use a more compact, abbreviated log format // Use a more compact, abbreviated log format
.compact() .compact()
// Display source code file paths // Display source code file paths
.with_file(true) .with_file(true)
// Display source code line numbers // Display source code line numbers
.with_line_number(true) .with_line_number(true)
// Display the thread ID an event was recorded on // Display the thread ID an event was recorded on
.with_thread_ids(true) .with_thread_ids(true)
// Don't display the event's target (module path) // Don't display the event's target (module path)
.with_target(false) .with_target(false)
// Include per-span timings // Include per-span timings
.with_span_events(FmtSpan::CLOSE) .with_span_events(FmtSpan::CLOSE)
// Build the subscriber // Build the subscriber
.finish(); .finish();
// Set the subscriber as the default // Set the subscriber as the default
tracing::subscriber::set_global_default(subscriber)?; tracing::subscriber::set_global_default(subscriber)?;
Ok(()) Ok(())
} }
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
// Start the logger // Obtain the database pool
set_console_logging().unwrap(); let pool = pgdb::get_connection_pool(10).await;
if pool.is_err() {
tracing::error!("Unable to connect to the database");
tracing::error!("{pool:?}");
return Err(anyhow::Error::msg("Unable to connect to the database"));
}
let pool = pool.unwrap();
let _ = server::listen_accept().await; // Start the logger
Ok(()) set_console_logging().unwrap();
let _ = server::listen_accept(pool.clone()).await;
Ok(())
} }

View File

@ -10,18 +10,10 @@ use tokio::{
/// Entry point for the main license server system. /// Entry point for the main license server system.
/// Starts listening on port 9126 for license requests. /// Starts listening on port 9126 for license requests.
pub async fn listen_accept() -> anyhow::Result<()> { pub async fn listen_accept(pool: Pool<Postgres>) -> anyhow::Result<()> {
let listener = TcpListener::bind(":::9126").await?; let listener = TcpListener::bind(":::9126").await?;
tracing::info!("Listening on :::9126"); tracing::info!("Listening on :::9126");
let pool = pgdb::get_connection_pool(10).await;
if pool.is_err() {
tracing::error!("Unable to connect to the database");
tracing::error!("{pool:?}");
return Err(anyhow::Error::msg("Unable to connect to the database"));
}
let pool = pool.unwrap();
loop { loop {
let (socket, address) = listener.accept().await?; let (socket, address) = listener.accept().await?;
tracing::info!("Connection from {address:?}"); tracing::info!("Connection from {address:?}");