gege-jdr-backend/gejdr-backend/src/lib.rs

68 lines
2.1 KiB
Rust
Raw Normal View History

2024-08-10 09:33:15 +00:00
#![deny(clippy::all)]
#![deny(clippy::pedantic)]
#![deny(clippy::nursery)]
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::unused_async)]
#![allow(clippy::useless_let_if_seq)] // Reason: prevents some OpenApi structs from compiling
use gejdr_core::sqlx;
mod api_wrapper;
mod errors;
mod oauth;
mod route;
mod settings;
mod startup;
2024-08-10 09:33:15 +00:00
type MaybeListener = Option<poem::listener::TcpListener<String>>;
async fn prepare(listener: MaybeListener, test_db: Option<sqlx::PgPool>) -> startup::Application {
dotenvy::dotenv().ok();
let settings = settings::Settings::new().expect("Failed to read settings");
if !cfg!(test) {
let subscriber = gejdr_core::telemetry::get_subscriber(settings.clone().debug);
gejdr_core::telemetry::init_subscriber(subscriber);
2024-08-10 09:33:15 +00:00
}
tracing::event!(
target: "gege-jdr-backend",
tracing::Level::DEBUG,
"Using these settings: {:?}",
settings.clone()
);
let application = startup::Application::build(settings.clone(), test_db, listener).await;
tracing::event!(
target: "gege-jdr-backend",
tracing::Level::INFO,
"Listening on {}",
application.settings.web_address()
2024-08-10 09:33:15 +00:00
);
application
}
/// # Errors
///
/// May return an error if the server encounters an error it cannot
/// recover from.
#[cfg(not(tarpaulin_include))]
pub async fn run(listener: MaybeListener) -> Result<(), std::io::Error> {
let application = prepare(listener, None).await;
application.make_app().run().await
}
#[cfg(test)]
async fn make_random_tcp_listener() -> poem::listener::TcpListener<String> {
let tcp_listener =
std::net::TcpListener::bind("127.0.0.1:0").expect("Failed to bind a random TCP listener");
let port = tcp_listener.local_addr().unwrap().port();
poem::listener::TcpListener::bind(format!("127.0.0.1:{port}"))
}
#[cfg(test)]
async fn get_test_app(test_db: Option<sqlx::PgPool>) -> startup::App {
let tcp_listener = crate::make_random_tcp_listener().await;
crate::prepare(Some(tcp_listener), test_db)
.await
.make_app()
.into()
}