Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async-ify everything, and Delete DO Deployment Stuff #103

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 231 additions & 11 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 0 additions & 16 deletions Dockerfile-SmitheBackend-Do

This file was deleted.

61 changes: 0 additions & 61 deletions backend-deployment-do.yml

This file was deleted.

21 changes: 0 additions & 21 deletions backend-ingress-do.yml

This file was deleted.

12 changes: 0 additions & 12 deletions backend-service-do.yml

This file was deleted.

4 changes: 1 addition & 3 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@ serde_json = "1"
thiserror = "1"
smithe_lib = { path = "../lib" }
rocket-governor = "0.2.0-rc.1"

[target.'cfg(target_os = "linux")'.dependencies]
openssl = { version = "0.10", features = ["vendored"] }
tokio = { version = "1", features = ["full"] }
43 changes: 25 additions & 18 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,27 @@ impl<'r, 'o: 'r> Responder<'r, 'o> for Error {

#[get("/")]
fn index(_limitguard: RocketGovernor<'_, RateLimitGuard>) -> &'static str {
"Hello, world!"
"Hello, world! (backend)"
}

#[get("/<tag>")]
fn search_players(
async fn search_players(
tag: String,
_limitguard: RocketGovernor<'_, RateLimitGuard>,
) -> Result<String, Error> {
Ok(serde_json::to_string(&get_all_like(&tag)?)?)
Ok(serde_json::to_string(&get_all_like(&tag).await?)?)
}

#[get("/<id>")]
fn view_player(id: i32, _limitguard: RocketGovernor<'_, RateLimitGuard>) -> Result<String, Error> {
async fn view_player(
id: i32,
_limitguard: RocketGovernor<'_, RateLimitGuard>,
) -> Result<String, Error> {
// insert player page view
smithe_lib::player_page_views::insert_player_page_view(id).unwrap();
Ok(serde_json::to_string(&get_player(id)?)?)
smithe_lib::player_page_views::insert_player_page_view(id)
.await
.unwrap();
Ok(serde_json::to_string(&get_player(id).await?)?)
}

#[get("/<id>")]
Expand All @@ -75,47 +80,49 @@ async fn get_player_set_wins_without_dqs(
id: i32,
_limitguard: RocketGovernor<'_, RateLimitGuard>,
) -> Result<String, Error> {
Ok(serde_json::to_string(&get_set_wins_without_dqs(id)?)?)
Ok(serde_json::to_string(&get_set_wins_without_dqs(id).await?)?)
}

#[get("/<id>/losses_without_dqs")]
async fn get_player_set_losses_without_dqs(
id: i32,
_limitguard: RocketGovernor<'_, RateLimitGuard>,
) -> Result<String, Error> {
Ok(serde_json::to_string(&get_set_losses_without_dqs(id)?)?)
Ok(serde_json::to_string(
&get_set_losses_without_dqs(id).await?,
)?)
}

#[get("/<id>/wins_by_dqs")]
async fn get_player_set_wins_by_dqs(
id: i32,
_limitguard: RocketGovernor<'_, RateLimitGuard>,
) -> Result<String, Error> {
Ok(serde_json::to_string(&get_set_wins_by_dq(id)?)?)
Ok(serde_json::to_string(&get_set_wins_by_dq(id).await?)?)
}

#[get("/<id>/losses_by_dqs")]
async fn get_player_set_losses_by_dqs(
id: i32,
_limitguard: RocketGovernor<'_, RateLimitGuard>,
) -> Result<String, Error> {
Ok(serde_json::to_string(&get_set_losses_by_dq(id)?)?)
Ok(serde_json::to_string(&get_set_losses_by_dq(id).await?)?)
}

#[get("/<id>/winrate")]
async fn get_player_winrate(
id: i32,
_limitguard: RocketGovernor<'_, RateLimitGuard>,
) -> Result<String, Error> {
Ok(serde_json::to_string(&get_winrate(id)?)?)
Ok(serde_json::to_string(&get_winrate(id).await?)?)
}

#[get("/<id>/competitor_type")]
async fn get_player_competitor_type(
id: i32,
_limitguard: RocketGovernor<'_, RateLimitGuard>,
) -> Result<String, Error> {
let ct = get_competitor_type(id)?;
let ct = get_competitor_type(id).await?;
Ok(serde_json::to_string(&format!("{}-{}er", ct.0, ct.1))?)
}

Expand All @@ -125,7 +132,7 @@ async fn get_player_top_two_characters(
id: i32,
_limitguard: RocketGovernor<'_, RateLimitGuard>,
) -> Result<String, Error> {
Ok(serde_json::to_string(&get_top_two_characters(id)?)?)
Ok(serde_json::to_string(&get_top_two_characters(id).await?)?)
}

// get sets by player id
Expand All @@ -134,7 +141,7 @@ async fn get_player_sets(
id: i32,
_limitguard: RocketGovernor<'_, RateLimitGuard>,
) -> Result<String, Error> {
Ok(serde_json::to_string(&get_sets_per_player_id(id)?)?)
Ok(serde_json::to_string(&get_sets_per_player_id(id).await?)?)
}

// get head to head by player id
Expand All @@ -143,14 +150,14 @@ async fn get_player_head_to_head(
id: i32,
_limitguard: RocketGovernor<'_, RateLimitGuard>,
) -> Result<String, Error> {
Ok(serde_json::to_string(&get_head_to_head_record(id)?)?)
Ok(serde_json::to_string(&get_head_to_head_record(id).await?)?)
}

fn rocket() -> Rocket<Build> {
#[cfg(debug_assertions)]
let allowed_origins = AllowedOrigins::some_exact(&[DEV_ADDRESS, DEV_ADDRESS_2]);
// #[cfg(debug_assertions)]
// let allowed_origins = AllowedOrigins::some_exact(&[DEV_ADDRESS, DEV_ADDRESS_2]);

#[cfg(not(debug_assertions))]
// #[cfg(not(debug_assertions))]
let allowed_origins = AllowedOrigins::some_exact(&[PROD_ADDRESS, PROD_ADDRESS_2]);

let cors = rocket_cors::CorsOptions {
Expand Down
4 changes: 3 additions & 1 deletion database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ serde = "1"
startgg = { path = "../startgg_api/" }
chrono = "0.4"
tracing = "0.1"
tracing-subscriber = "0.3"
tracing-subscriber = "0.3"
diesel-async = { version = "0.4.1", features = ["postgres"] }
tokio = { version = "1", features = ["full", "macros"] }
26 changes: 16 additions & 10 deletions database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
pub mod db_models;
pub mod schema;

use std::env;
use std::{env, io::Write};

use anyhow::Result;
use diesel::{Connection, ConnectionResult, PgConnection};
use diesel_async::{AsyncConnection, AsyncPgConnection};
use tracing::Level;
use tracing_subscriber::FmtSubscriber;

Expand All @@ -27,16 +27,18 @@
Ok(())
}

pub fn connect() -> Result<PgConnection> {
pub async fn connect() -> Result<AsyncPgConnection> {

Check warning on line 30 in database/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

database/src/lib.rs#L30

Added line #L30 was not covered by tests
// Try to connect forever until we succeed for a maximum of 100 (arbitraty) tries
let tries = 100;
for _ in 0..tries {
match try_connect() {
match try_connect().await {

Check warning on line 34 in database/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

database/src/lib.rs#L34

Added line #L34 was not covered by tests
Ok(conn) => return Ok(conn),
Err(e) => {
println!("Failed to connect to database: {}", e);
std::io::stdout().flush().unwrap();

Check warning on line 38 in database/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

database/src/lib.rs#L37-L38

Added lines #L37 - L38 were not covered by tests
tracing::error!("Failed to connect to database: {}", e);
tracing::info!("Retrying in 5 seconds...");
std::thread::sleep(std::time::Duration::from_secs(5));
tokio::time::sleep(std::time::Duration::from_secs(5)).await;

Check warning on line 41 in database/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

database/src/lib.rs#L41

Added line #L41 was not covered by tests
}
}
}
Expand All @@ -46,15 +48,19 @@
))
}

fn try_connect() -> ConnectionResult<PgConnection> {
PgConnection::establish(
async fn try_connect() -> Result<AsyncPgConnection> {
println!("Connecting to database...");
std::io::stdout().flush().unwrap();
AsyncPgConnection::establish(

Check warning on line 54 in database/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

database/src/lib.rs#L51-L54

Added lines #L51 - L54 were not covered by tests
&env::var(PIDGTM_DATABASE_URL_ENVVAR_NAME).unwrap_or_else(|_| {
panic!(
"{} environment variable not set",
PIDGTM_DATABASE_URL_ENVVAR_NAME
)
}),
)
.await
.map_err(|e| anyhow::anyhow!(e))

Check warning on line 63 in database/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

database/src/lib.rs#L62-L63

Added lines #L62 - L63 were not covered by tests
}

#[cfg(test)]
Expand All @@ -63,10 +69,10 @@
use super::*;
use anyhow::Result;

#[test]
fn test_connect() -> Result<()> {
#[tokio::test]
async fn test_connect() -> Result<()> {
init_logger()?;
assert!(connect().is_ok());
assert!(connect().await.is_ok());

Ok(())
}
Expand Down
2 changes: 2 additions & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ ctrlc = "3.4"
as-any = "0.3"
tokio = { version = "1.1", features = [ "rt", "macros" ] }
chrono = "0.4"
diesel-async = { version = "0.4.1", features = ["postgres"] }


[features]
default = ["skip_db_tests"]
Expand Down
Loading
Loading