generated from phundrak/rust-poem-openapi-template
	
		
			
	
	
		
			64 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
		
		
			
		
	
	
			64 lines
		
	
	
		
			2.0 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
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								pub mod route;
							 | 
						||
| 
								 | 
							
								pub mod settings;
							 | 
						||
| 
								 | 
							
								pub mod startup;
							 | 
						||
| 
								 | 
							
								pub mod telemetry;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								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 = telemetry::get_subscriber(settings.clone().debug);
							 | 
						||
| 
								 | 
							
								        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 http://127.0.0.1:{}/",
							 | 
						||
| 
								 | 
							
								        application.port()
							 | 
						||
| 
								 | 
							
								    );
							 | 
						||
| 
								 | 
							
								    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()
							 | 
						||
| 
								 | 
							
								}
							 |