Two teams try and fill in any horizontal, vertical, or diagonal line on a bingo board by playing maps on osu!
osu.bingo
osu
1use sqlx::PgPool;
2
3use super::DbContext;
4
5impl DbContext {
6 pub(super) async fn get_pg_connection(&mut self) -> Option<&PgPool> {
7 if self.pg_pool.is_some() {
8 return self.pg_pool.as_ref();
9 }
10
11 self.pg_pool = None;
12 self.pg_connect().await;
13
14 self.pg_pool.as_ref()
15 }
16 pub(super) async fn pg_connect(&mut self) -> () {
17 // Don't connect if already connected
18 if let Some(pool) = self.pg_pool.as_ref() {
19 if !pool.is_closed() {
20 return;
21 }
22 }
23
24 let db_url = match std::env::var("DATABASE_URL") {
25 Ok(url) => url,
26 Err(_) => {
27 panic!("DATABASE_URL environment variable is missing, cannot connect to database")
28 }
29 };
30
31 self.pg_pool = match PgPool::connect(&db_url).await {
32 Ok(x) => Some(x),
33 Err(e) => {
34 log::error!("Failed to connect to database at DATABASE_URL: {e}");
35 log::warn!(
36 "Requests requiring postgres will request a connection retry, but may fail."
37 );
38 return;
39 }
40 };
41
42 log::info!("Successfully connected to Postgres instance");
43 }
44}