+39
-20
menu/src/main.rs
+39
-20
menu/src/main.rs
···
13
13
use serde::Deserialize;
14
14
use sqlx::{Connection, PgConnection};
15
15
use std::{collections::HashMap, env, ops::DerefMut, sync::OnceLock};
16
16
-
use tokio::sync::Mutex;
16
16
+
use tokio::{
17
17
+
sync::Mutex,
18
18
+
time::{Duration, sleep},
19
19
+
};
17
20
use tower_http::normalize_path::NormalizePathLayer;
18
21
use tower_layer::Layer;
19
22
···
50
53
}
51
54
52
55
async fn get_redirect(default_location: &str, go: &str) -> Redirect {
53
53
-
let redirect = sqlx::query!(r#"SELECT ("to") FROM direct WHERE "from" = $1 LIMIT 1"#, go.to_lowercase())
54
54
-
.fetch_one(
55
55
-
STATE
56
56
-
.get()
57
57
-
.expect("Server must be initialized before processing connections")
58
58
-
.sqlx_connection
59
59
-
.lock()
60
60
-
.await
61
61
-
.deref_mut(),
62
62
-
)
63
63
-
.await;
56
56
+
let redirect = sqlx::query!(
57
57
+
r#"SELECT ("to") FROM direct WHERE "from" = $1 LIMIT 1"#,
58
58
+
go.to_lowercase()
59
59
+
)
60
60
+
.fetch_one(
61
61
+
STATE
62
62
+
.get()
63
63
+
.expect("Server must be initialized before processing connections")
64
64
+
.sqlx_connection
65
65
+
.lock()
66
66
+
.await
67
67
+
.deref_mut(),
68
68
+
)
69
69
+
.await;
64
70
65
71
if let Ok(record) = redirect {
66
72
Redirect::temporary(&record.to)
···
162
168
163
169
#[tokio::main]
164
170
async fn main() {
165
165
-
let mut connection = PgConnection::connect(
166
166
-
env::var("DATABASE_URL")
167
167
-
.expect(
168
168
-
"Please ensure you set your database URL in the $DATABASE_URL environment variable",
171
171
+
let mut connection = {
172
172
+
let mut maybe_connection;
173
173
+
for _ in 0..3 {
174
174
+
maybe_connection = PgConnection::connect(
175
175
+
env::var("DATABASE_URL")
176
176
+
.expect(
177
177
+
"Please ensure you set your database URL in the $DATABASE_URL environment variable",
178
178
+
)
179
179
+
.as_str(),
169
180
)
170
170
-
.as_str(),
171
171
-
)
172
172
-
.await
173
173
-
.expect("Failed to connect to database defined in $DATABASE_URL");
181
181
+
.await;
182
182
+
183
183
+
if maybe_connection.is_ok() {
184
184
+
break;
185
185
+
}
186
186
+
187
187
+
sleep(Duration::from_secs(5)).await;
188
188
+
}
189
189
+
190
190
+
maybe_connection
191
191
+
.expect("Failed to connect to database defined in $DATABASE_URL after 3 retries")
192
192
+
};
174
193
175
194
sqlx::migrate!()
176
195
.run(&mut connection)