From 2befe7c462b93a1b1520ba69798c2be654c3842c Mon Sep 17 00:00:00 2001 From: Tyler Wilding Date: Wed, 26 Feb 2025 20:22:02 -0500 Subject: [PATCH] ci: finalize ci implementation --- .gitattributes | 4 ++++ .github/workflows/lint.yml | 4 +--- ...2cf6a712213bf06aa1bf744d4ebc69636a2c2.json | 20 +++++++++++++++++++ README.md | 2 +- docker-compose.yaml | 2 +- src/api/v1.rs | 5 ++++- src/external/github.rs | 18 ++++++++++++++--- src/external/mod.rs | 2 +- src/fairings.rs | 6 +++++- src/main.rs | 11 ++++++---- src/storage/mod.rs | 2 +- src/storage/models.rs | 12 +++++++---- src/storage/sync.rs | 15 +++++++++++--- 13 files changed, 80 insertions(+), 23 deletions(-) create mode 100644 .gitattributes create mode 100644 .sqlx/query-2dd2301f84c890ffe8af5abc5822cf6a712213bf06aa1bf744d4ebc69636a2c2.json diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6f57e18 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,4 @@ +# Ensure line endings are consistently 'LF' +* text=auto + +.sqlx/**/* linguist-generated \ No newline at end of file diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 484f4cc..6aabd47 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -27,9 +27,7 @@ jobs: shared-key: web-api-build-ubuntu-latest - name: Check Rust formatting - run: | - cd src-tauri/ - cargo fmt --all --check + run: cargo fmt --all --check linter: name: Linter diff --git a/.sqlx/query-2dd2301f84c890ffe8af5abc5822cf6a712213bf06aa1bf744d4ebc69636a2c2.json b/.sqlx/query-2dd2301f84c890ffe8af5abc5822cf6a712213bf06aa1bf744d4ebc69636a2c2.json new file mode 100644 index 0000000..b58efd5 --- /dev/null +++ b/.sqlx/query-2dd2301f84c890ffe8af5abc5822cf6a712213bf06aa1bf744d4ebc69636a2c2.json @@ -0,0 +1,20 @@ +{ + "db_name": "SQLite", + "query": "\n SELECT version FROM releases;\n ", + "describe": { + "columns": [ + { + "name": "version", + "ordinal": 0, + "type_info": "Text" + } + ], + "parameters": { + "Right": 0 + }, + "nullable": [ + false + ] + }, + "hash": "2dd2301f84c890ffe8af5abc5822cf6a712213bf06aa1bf744d4ebc69636a2c2" +} diff --git a/README.md b/README.md index e31a5ae..f5e21a6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # PCSX2 API -TODO +TODO ## Development diff --git a/docker-compose.yaml b/docker-compose.yaml index 0dc114d..df1b098 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -3,7 +3,7 @@ version: '3' services: pcsx2-api: container_name: api - image: pcsx2-api:latest + image: ghcr.io/PCSX2/web-api:latest ports: - "8000:8000" volumes: diff --git a/src/api/v1.rs b/src/api/v1.rs index 00ca61d..58312d8 100644 --- a/src/api/v1.rs +++ b/src/api/v1.rs @@ -97,7 +97,10 @@ impl ReleaseV1 { } cleaned_tags = cleaned_tags .into_iter() - .filter(|tag| !tag.to_lowercase().contains("32bit") && !tag.to_lowercase().contains("64")) + .filter(|tag| { + !tag.to_lowercase().contains("32bit") + && !tag.to_lowercase().contains("64") + }) .collect(); } else if k.clone().to_lowercase().contains("linux") { display_name = "Linux".to_owned(); diff --git a/src/external/github.rs b/src/external/github.rs index fa6c470..a919ff3 100644 --- a/src/external/github.rs +++ b/src/external/github.rs @@ -1,13 +1,25 @@ pub async fn get_latest_official_version() -> Result { let octocrab = octocrab::instance(); // TODO - probably handle potential errors - let release = octocrab.repos("PCSX2", "pcsx2").releases().list().per_page(1).send().await?; + let release = octocrab + .repos("PCSX2", "pcsx2") + .releases() + .list() + .per_page(1) + .send() + .await?; return Ok(release.items.first().unwrap().tag_name.clone()); } pub async fn get_latest_archive_version() -> Result { let octocrab = octocrab::instance(); // TODO - probably handle potential errors - let release = octocrab.repos("PCSX2", "archive").releases().list().per_page(1).send().await?; + let release = octocrab + .repos("PCSX2", "archive") + .releases() + .list() + .per_page(1) + .send() + .await?; return Ok(release.items.first().unwrap().tag_name.clone()); -} \ No newline at end of file +} diff --git a/src/external/mod.rs b/src/external/mod.rs index b949282..72246d3 100644 --- a/src/external/mod.rs +++ b/src/external/mod.rs @@ -1 +1 @@ -pub mod github; \ No newline at end of file +pub mod github; diff --git a/src/fairings.rs b/src/fairings.rs index 41ffe8c..d7a4134 100644 --- a/src/fairings.rs +++ b/src/fairings.rs @@ -26,7 +26,11 @@ impl Fairing for CORSHeaderFairing { async fn on_response<'r>(&self, request: &'r Request<'_>, response: &mut Response<'r>) { if let Some(origin) = request.headers().get_one("Origin") { - if origin == "https://pcsx2.net" || origin.starts_with("http://localhost") || origin.starts_with("https://localhost") || CF_PAGES_REGEX.is_match(origin) { + if origin == "https://pcsx2.net" + || origin.starts_with("http://localhost") + || origin.starts_with("https://localhost") + || CF_PAGES_REGEX.is_match(origin) + { response.set_raw_header("Access-Control-Allow-Origin", "*"); } else { info!("Rejecting request from origin: {}", origin); diff --git a/src/main.rs b/src/main.rs index 00dcbe2..77b6337 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,10 @@ mod api; +mod external; mod fairings; mod guards; mod responders; mod storage; mod util; -mod external; use fern::colors::{Color, ColoredLevelConfig}; #[macro_use] @@ -59,7 +59,8 @@ impl RateLimiterCache { } fn setup_logging() { - let verbose_logging = dotenvy::var("VERBOSE_LOGGING").map_or(false, |val| val.to_lowercase().eq("true")); + let verbose_logging = + dotenvy::var("VERBOSE_LOGGING").map_or(false, |val| val.to_lowercase().eq("true")); let error_log_path = dotenvy::var("ERROR_LOG_PATH").expect("ERROR_LOG_PATH must be set"); let app_log_path = dotenvy::var("APP_LOG_PATH").expect("APP_LOG_PATH must be set"); let mut log_level = log::LevelFilter::Warn; @@ -86,7 +87,7 @@ fn setup_logging() { color_line = format_args!( "\x1B[{}m", colors_line.get_color(&record.level()).to_fg_str() - ), + ), date = chrono::Utc::now().to_rfc3339_opts(SecondsFormat::Millis, true), level = record.level(), target = record.target(), @@ -126,7 +127,9 @@ async fn main() -> Result<(), rocket::Error> { // Check to see if the database is out of date (pull latest releases) // do this only if we have the github api credential set if dotenvy::var("GITHUB_API_TOKEN").is_ok() { - let octocrab = octocrab::Octocrab::builder().personal_token(dotenvy::var("GITHUB_API_TOKEN").unwrap()).build(); + let octocrab = octocrab::Octocrab::builder() + .personal_token(dotenvy::var("GITHUB_API_TOKEN").unwrap()) + .build(); octocrab::initialise(octocrab.unwrap()); storage::sync::sync_database(&db).await; } diff --git a/src/storage/mod.rs b/src/storage/mod.rs index 3cdff35..5f6447d 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -1,4 +1,4 @@ pub mod models; pub mod sqlite; +pub mod sync; pub mod v1; -pub mod sync; \ No newline at end of file diff --git a/src/storage/models.rs b/src/storage/models.rs index e453c54..81e8aa9 100644 --- a/src/storage/models.rs +++ b/src/storage/models.rs @@ -82,7 +82,8 @@ impl ReleaseRow { if github_release.body.is_some() && github_release.body.contains("DATE_OVERRIDE") { let regexp = Regex::new(r"DATE_OVERRIDE:\s?(\d{4}-\d{2}-\d{2})").unwrap(); let release_body = github_release.body.clone().unwrap_or("".to_string()); - let matches: Vec<&str> = regexp.captures_iter(&release_body) + let matches: Vec<&str> = regexp + .captures_iter(&release_body) .filter_map(|cap| cap.get(1).map(|m| m.as_str())) .collect(); if let Some(first_match) = matches.first() { @@ -101,7 +102,9 @@ impl ReleaseRow { version: github_release.tag_name.clone(), version_integral: semver_integral.unwrap(), published_timestamp: match &github_release.published_at { - Some(published_at) => Some(published_at.to_rfc3339_opts(SecondsFormat::Millis, true)), + Some(published_at) => { + Some(published_at.to_rfc3339_opts(SecondsFormat::Millis, true)) + } None => None, }, created_timestamp: match &github_release.created_at { @@ -111,7 +114,7 @@ impl ReleaseRow { } else { Some(created_at.to_rfc3339_opts(SecondsFormat::Millis, true)) } - }, + } None => None, }, github_release_id: github_release.id.0 as i64, @@ -121,7 +124,8 @@ impl ReleaseRow { } else { "stable".to_owned() }, - next_audit: (Utc::now() + Duration::days(7)).to_rfc3339_opts(SecondsFormat::Millis, true), + next_audit: (Utc::now() + Duration::days(7)) + .to_rfc3339_opts(SecondsFormat::Millis, true), next_audit_days: 7, archived: 0, notes: github_release.body.clone(), diff --git a/src/storage/sync.rs b/src/storage/sync.rs index c21caec..40cb6c8 100644 --- a/src/storage/sync.rs +++ b/src/storage/sync.rs @@ -14,7 +14,10 @@ pub async fn sync_database(db: &SqlitePool) -> bool { // 0. Get a list of all current version numbers (tags) let current_version_data = storage::sqlite::list_all_release_tags(db).await; if current_version_data.is_err() { - log::error!("unable to fetch current version data: {:?}", current_version_data.err()); + log::error!( + "unable to fetch current version data: {:?}", + current_version_data.err() + ); return false; } let current_versions = current_version_data @@ -56,7 +59,10 @@ pub async fn sync_database(db: &SqlitePool) -> bool { .send() .await; if main_release_stream_req.is_err() { - log::error!("unable to retrieve PCSX2/pcsx2 releases: {:?}", main_release_stream_req.err()); + log::error!( + "unable to retrieve PCSX2/pcsx2 releases: {:?}", + main_release_stream_req.err() + ); return false; } let main_release_stream = main_release_stream_req.unwrap().into_stream(&octocrab); @@ -91,7 +97,10 @@ pub async fn sync_database(db: &SqlitePool) -> bool { .send() .await; if archive_release_stream_req.is_err() { - log::error!("unable to retrieve PCSX2/archive releases: {:?}", archive_release_stream_req.err()); + log::error!( + "unable to retrieve PCSX2/archive releases: {:?}", + archive_release_stream_req.err() + ); return false; } let archive_release_stream = archive_release_stream_req.unwrap().into_stream(&octocrab);