gege-jdr-backend/gejdr-backend/src/lib.rs
Lucien Cartier-Tilet a2ea5a157d
feat: OAuth implementation with Discord
This commit separates the core features of géjdr from the backend as
these will also be used by the bot in the future.

This commit also updates the dependencies of the project. It also
removes the dependency lettre as well as the mailpit docker service
for developers as it appears clearer this project won’t send emails
anytime soon.

The publication of a docker image is also postponed until later.
2025-01-11 22:10:16 +01:00

68 lines
2.1 KiB
Rust

#![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;
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);
}
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()
);
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()
}