diff --git a/backend/src/api/aur.rs b/backend/src/api/aur.rs new file mode 100644 index 0000000..f2ed0bc --- /dev/null +++ b/backend/src/api/aur.rs @@ -0,0 +1,25 @@ +use crate::aur::aur::query_aur; +use rocket::serde::json::Json; + +use rocket::get; + +use crate::api::types::input::ApiPackage; +use rocket_okapi::openapi; + +#[openapi(tag = "aur")] +#[get("/search?")] +pub async fn search(query: &str) -> Result>, String> { + return match query_aur(query).await { + Ok(v) => { + let mapped = v + .iter() + .map(|x| ApiPackage { + name: x.name.clone(), + version: x.version.clone(), + }) + .collect(); + Ok(Json(mapped)) + } + Err(e) => Err(format!("{}", e)), + }; +} diff --git a/backend/src/api/backend.rs b/backend/src/api/backend.rs index 3511a37..70b0be5 100644 --- a/backend/src/api/backend.rs +++ b/backend/src/api/backend.rs @@ -1,6 +1,7 @@ +use crate::api::aur::*; use crate::api::build::*; -use crate::api::list::*; use crate::api::package::*; +use crate::api::stats::*; use crate::api::version::*; use rocket::Route; use rocket_okapi::openapi_get_routes; diff --git a/backend/src/api/build.rs b/backend/src/api/build.rs index 0210446..69a21ef 100644 --- a/backend/src/api/build.rs +++ b/backend/src/api/build.rs @@ -3,29 +3,16 @@ use crate::db::prelude::Builds; use crate::db::{builds, packages, versions}; use rocket::response::status::NotFound; use rocket::serde::json::Json; -use rocket::serde::{Deserialize, Serialize}; use rocket::{delete, get, State}; -use rocket_okapi::okapi::schemars; -use rocket_okapi::{openapi, JsonSchema}; + +use crate::api::types::input::ListBuildsModel; +use rocket_okapi::openapi; use sea_orm::ColumnTrait; use sea_orm::QueryFilter; use sea_orm::{ - DatabaseConnection, EntityTrait, FromQueryResult, ModelTrait, QueryOrder, QuerySelect, - RelationTrait, + DatabaseConnection, EntityTrait, ModelTrait, QueryOrder, QuerySelect, RelationTrait, }; -#[derive(FromQueryResult, Deserialize, JsonSchema, Serialize)] -#[serde(crate = "rocket::serde")] -pub struct ListPackageModel { - id: i32, - name: String, - status: i32, - outofdate: bool, - latest_version: Option, - latest_version_id: Option, - latest_aur_version: String, -} - #[openapi(tag = "build")] #[get("/build//output?")] pub async fn build_output( @@ -46,7 +33,7 @@ pub async fn build_output( Some(v) => match startline { None => Ok(v), Some(startline) => { - let output: Vec = v.split("\n").map(|x| x.to_string()).collect(); + let output: Vec = v.split('\n').map(|x| x.to_string()).collect(); let len = output.len(); let len_missing = len as i32 - startline; @@ -59,7 +46,7 @@ pub async fn build_output( 0 }) .rev() - .map(|x1| x1.clone()) + .cloned() .collect::>(); let output = output.join("\n"); @@ -69,18 +56,6 @@ pub async fn build_output( }; } -#[derive(FromQueryResult, Deserialize, JsonSchema, Serialize)] -#[serde(crate = "rocket::serde")] -pub struct ListBuildsModel { - id: i32, - pkg_id: i32, - pkg_name: String, - version: String, - status: i32, - start_time: Option, - end_time: Option, -} - #[openapi(tag = "build")] #[get("/builds?&")] pub async fn list_builds( diff --git a/backend/src/api/mod.rs b/backend/src/api/mod.rs index 8df1443..8e5a9c3 100644 --- a/backend/src/api/mod.rs +++ b/backend/src/api/mod.rs @@ -1,7 +1,9 @@ +mod aur; pub mod backend; mod build; #[cfg(feature = "static")] pub mod embed; -mod list; mod package; +mod stats; +mod types; mod version; diff --git a/backend/src/api/package.rs b/backend/src/api/package.rs index f642219..eb127da 100644 --- a/backend/src/api/package.rs +++ b/backend/src/api/package.rs @@ -1,4 +1,3 @@ -use crate::api::build::ListPackageModel; use crate::builder::types::Action; use crate::db::migration::{JoinType, Order}; use crate::db::prelude::Packages; @@ -8,20 +7,16 @@ use crate::package::delete::package_delete; use crate::package::update::package_update; use rocket::response::status::{BadRequest, NotFound}; use rocket::serde::json::Json; -use rocket::serde::Deserialize; + use rocket::{delete, get, post, State}; -use rocket_okapi::okapi::schemars; -use rocket_okapi::{openapi, JsonSchema}; + +use crate::api::types::input::ListPackageModel; +use crate::api::types::output::{AddBody, UpdateBody}; +use rocket_okapi::openapi; use sea_orm::DatabaseConnection; use sea_orm::{ColumnTrait, EntityTrait, QueryFilter, QueryOrder, QuerySelect, RelationTrait}; use tokio::sync::broadcast::Sender; -#[derive(Deserialize, JsonSchema)] -#[serde(crate = "rocket::serde")] -pub struct AddBody { - name: String, -} - #[openapi(tag = "Packages")] #[post("/package", data = "")] pub async fn package_add_endpoint( @@ -34,12 +29,6 @@ pub async fn package_add_endpoint( .map_err(|e| BadRequest(Some(e.to_string()))) } -#[derive(Deserialize, JsonSchema)] -#[serde(crate = "rocket::serde")] -pub struct UpdateBody { - force: bool, -} - #[openapi(tag = "Packages")] #[post("/package//update", data = "")] pub async fn package_update_endpoint( @@ -50,7 +39,7 @@ pub async fn package_update_endpoint( ) -> Result, BadRequest> { package_update(db, id, input.force, tx) .await - .map(|e| Json(e)) + .map(Json) .map_err(|e| BadRequest(Some(e.to_string()))) } diff --git a/backend/src/api/list.rs b/backend/src/api/stats.rs similarity index 56% rename from backend/src/api/list.rs rename to backend/src/api/stats.rs index 7d0fc29..6c5693e 100644 --- a/backend/src/api/list.rs +++ b/backend/src/api/stats.rs @@ -1,53 +1,17 @@ -use crate::aur::aur::query_aur; +use crate::db::builds; use crate::db::prelude::{Builds, Packages}; -use crate::db::{builds}; use crate::utils::dir_size::dir_size; + use rocket::response::status::NotFound; use rocket::serde::json::Json; -use rocket::serde::{Deserialize, Serialize}; + use rocket::{get, State}; -use rocket_okapi::okapi::schemars; -use rocket_okapi::{openapi, JsonSchema}; + +use crate::api::types::input::ListStats; +use rocket_okapi::openapi; +use sea_orm::PaginatorTrait; use sea_orm::{ColumnTrait, QueryFilter}; -use sea_orm::{DatabaseConnection, EntityTrait, FromQueryResult}; -use sea_orm::{PaginatorTrait}; - -#[derive(Serialize, JsonSchema)] -#[serde(crate = "rocket::serde")] -pub struct ApiPackage { - name: String, - version: String, -} - -#[openapi(tag = "aur")] -#[get("/search?")] -pub async fn search(query: &str) -> Result>, String> { - return match query_aur(query).await { - Ok(v) => { - let mapped = v - .iter() - .map(|x| ApiPackage { - name: x.name.clone(), - version: x.version.clone(), - }) - .collect(); - Ok(Json(mapped)) - } - Err(e) => Err(format!("{}", e)), - }; -} - -#[derive(FromQueryResult, Deserialize, JsonSchema, Serialize)] -#[serde(crate = "rocket::serde")] -pub struct ListStats { - total_builds: u32, - failed_builds: u32, - avg_queue_wait_time: u32, - avg_build_time: u32, - repo_storage_size: u64, - active_builds: u32, - total_packages: u32, -} +use sea_orm::{DatabaseConnection, EntityTrait}; #[openapi(tag = "stats")] #[get("/stats")] diff --git a/backend/src/api/types/input.rs b/backend/src/api/types/input.rs new file mode 100644 index 0000000..e0a2327 --- /dev/null +++ b/backend/src/api/types/input.rs @@ -0,0 +1,47 @@ +use rocket::serde::{Deserialize, Serialize}; +use rocket_okapi::okapi::schemars; +use rocket_okapi::JsonSchema; +use sea_orm::FromQueryResult; + +#[derive(Serialize, JsonSchema)] +#[serde(crate = "rocket::serde")] +pub struct ApiPackage { + pub name: String, + pub version: String, +} + +#[derive(FromQueryResult, Deserialize, JsonSchema, Serialize)] +#[serde(crate = "rocket::serde")] +pub struct ListPackageModel { + pub id: i32, + pub name: String, + pub status: i32, + pub outofdate: bool, + pub latest_version: Option, + pub latest_version_id: Option, + pub latest_aur_version: String, +} + +#[derive(FromQueryResult, Deserialize, JsonSchema, Serialize)] +#[serde(crate = "rocket::serde")] +pub struct ListBuildsModel { + id: i32, + pkg_id: i32, + pkg_name: String, + version: String, + status: i32, + start_time: Option, + end_time: Option, +} + +#[derive(FromQueryResult, Deserialize, JsonSchema, Serialize)] +#[serde(crate = "rocket::serde")] +pub struct ListStats { + pub total_builds: u32, + pub failed_builds: u32, + pub avg_queue_wait_time: u32, + pub avg_build_time: u32, + pub repo_storage_size: u64, + pub active_builds: u32, + pub total_packages: u32, +} diff --git a/backend/src/api/types/mod.rs b/backend/src/api/types/mod.rs new file mode 100644 index 0000000..7d1a548 --- /dev/null +++ b/backend/src/api/types/mod.rs @@ -0,0 +1,2 @@ +pub mod input; +pub mod output; diff --git a/backend/src/api/types/output.rs b/backend/src/api/types/output.rs new file mode 100644 index 0000000..b2f52ca --- /dev/null +++ b/backend/src/api/types/output.rs @@ -0,0 +1,15 @@ +use rocket::serde::Deserialize; +use rocket_okapi::okapi::schemars; +use rocket_okapi::JsonSchema; + +#[derive(Deserialize, JsonSchema)] +#[serde(crate = "rocket::serde")] +pub struct AddBody { + pub(crate) name: String, +} + +#[derive(Deserialize, JsonSchema)] +#[serde(crate = "rocket::serde")] +pub struct UpdateBody { + pub(crate) force: bool, +} diff --git a/backend/src/aur/aur.rs b/backend/src/aur/aur.rs index fd063f1..eeb248a 100644 --- a/backend/src/aur/aur.rs +++ b/backend/src/aur/aur.rs @@ -52,7 +52,7 @@ pub async fn download_pkgbuild(url: &str, dest_dir: &str) -> anyhow::Result anyhow::Result<(Vec, String)> { .path_segments() .and_then(|segments| segments.last()) .ok_or(anyhow!("no segments"))? - .split(".") + .split('.') .collect::>() .first() .ok_or(anyhow!(""))? diff --git a/backend/src/main.rs b/backend/src/main.rs index 878ef38..9c6dfd5 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -34,7 +34,7 @@ fn main() { t.block_on(async move { // create folder for db stuff - if !fs::metadata("./db").is_ok() { + if fs::metadata("./db").is_err() { fs::create_dir("./db").unwrap(); } @@ -45,7 +45,7 @@ fn main() { Migrator::up(&db, None).await.unwrap(); // create repo folder - if !fs::metadata("./repo").is_ok() { + if fs::metadata("./repo").is_err() { fs::create_dir("./repo").unwrap(); let tar_gz = File::create("./repo/repo.db.tar.gz").unwrap(); @@ -74,9 +74,11 @@ fn main() { start_aur_version_checking(db.clone()); let backend_handle = tokio::spawn(async { - let mut config = Config::default(); - config.address = "0.0.0.0".parse().unwrap(); - config.port = 8081; + let config = Config { + address: "0.0.0.0".parse().unwrap(), + port: 8081, + ..Default::default() + }; let rock = rocket::custom(config) .manage(db) @@ -100,9 +102,11 @@ fn main() { }); let repo_handle = tokio::spawn(async { - let mut config = Config::default(); - config.address = "0.0.0.0".parse().unwrap(); - config.port = 8080; + let config = Config { + address: "0.0.0.0".parse().unwrap(), + port: 8080, + ..Default::default() + }; let launch_result = rocket::custom(config) .mount("/", FileServer::from("./repo")) @@ -116,6 +120,4 @@ fn main() { join_all([repo_handle, backend_handle]).await; }); - - return; } diff --git a/backend/src/package/add.rs b/backend/src/package/add.rs index e97404e..9bad7af 100644 --- a/backend/src/package/add.rs +++ b/backend/src/package/add.rs @@ -19,10 +19,11 @@ pub async fn package_add( // remove leading and trailing whitespaces let pkg_name = pkg_name.trim(); - if let Some(..) = Packages::find() + if Packages::find() .filter(packages::Column::Name.eq(pkg_name)) .one(&txn) .await? + .is_some() { return Err(anyhow!("Package already exists")); } diff --git a/backend/src/package/update.rs b/backend/src/package/update.rs index fface1e..e6e14e4 100644 --- a/backend/src/package/update.rs +++ b/backend/src/package/update.rs @@ -29,7 +29,7 @@ pub async fn package_update( let version_model = match Versions::find() .filter(versions::Column::Version.eq(pkg.version.clone())) - .filter(versions::Column::PackageId.eq(pkg.id.clone())) + .filter(versions::Column::PackageId.eq(pkg.id)) .one(&txn) .await? { diff --git a/backend/src/pkgbuild/build.rs b/backend/src/pkgbuild/build.rs index 3f3ca2f..6933eac 100644 --- a/backend/src/pkgbuild/build.rs +++ b/backend/src/pkgbuild/build.rs @@ -7,7 +7,7 @@ use tokio::sync::broadcast::Sender; // todo consider removing pkg_vers from attribute list pub async fn build_pkgbuild( folder_path: String, - pkg_vers: &str, + _pkg_vers: &str, pkg_name: &str, tx: Sender, ) -> anyhow::Result> { @@ -26,7 +26,7 @@ pub async fn build_pkgbuild( fs::write(&script_file, makepkg).expect("Unable to write script to file"); let mut child = tokio::process::Command::new("bash") - .args(&[ + .args([ script_file.as_os_str().to_str().unwrap(), "-f", "--noconfirm", @@ -106,7 +106,7 @@ fn locate_built_packages(pkg_name: String, folder_path: String) -> anyhow::Resul } } - return if pkg_names.is_empty() { + if pkg_names.is_empty() { Err(anyhow!("Built package not found")) } else { // expect at least one of the packages to start with the package name @@ -116,5 +116,5 @@ fn locate_built_packages(pkg_name: String, folder_path: String) -> anyhow::Resul )); } Ok(pkg_names) - }; + } }