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,6 +1,6 @@
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.
@ -27,9 +27,18 @@ fn set_console_logging() -> anyhow::Result<()> {
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
// Obtain the database pool
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();
// Start the logger // Start the logger
set_console_logging().unwrap(); set_console_logging().unwrap();
let _ = server::listen_accept().await; let _ = server::listen_accept(pool.clone()).await;
Ok(()) 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:?}");