From 9ad19eb36d1dee92c5654b618490ca920fffe09c Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 17 Aug 2023 14:25:25 +0200 Subject: [PATCH] refactor(server): move some consts around --- server/src/repo/manager.rs | 14 ++++++-------- server/src/repo/mod.rs | 3 +++ server/src/web/repo.rs | 11 ++++++----- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/server/src/repo/manager.rs b/server/src/repo/manager.rs index c288f30..1c5556e 100644 --- a/server/src/repo/manager.rs +++ b/server/src/repo/manager.rs @@ -5,8 +5,6 @@ use std::fs; use std::io; use std::path::{Path, PathBuf}; -pub const ANY_ARCH: &str = "any"; - /// Overarching abstraction that orchestrates updating the repositories stored on the server pub struct RepoGroupManager { repo_dir: PathBuf, @@ -48,9 +46,9 @@ impl RepoGroupManager { // All architectures should also include the "any" architecture, except for the "any" // architecture itself. - let repo_any_dir = self.repo_dir.join(repo).join(ANY_ARCH); + let repo_any_dir = self.repo_dir.join(repo).join(super::ANY_ARCH); - let any_entries_iter = if arch != ANY_ARCH && repo_any_dir.try_exists()? { + let any_entries_iter = if arch != super::ANY_ARCH && repo_any_dir.try_exists()? { Some(repo_any_dir.read_dir()?) } else { None @@ -159,7 +157,7 @@ impl RepoGroupManager { pkg.write_files(&mut files_file)?; // If a package of type "any" is added, we need to update every existing database - if pkg.info.arch == ANY_ARCH { + if pkg.info.arch == super::ANY_ARCH { self.sync_all(repo)?; } else { self.sync(repo, &pkg.info.arch)?; @@ -193,7 +191,7 @@ impl RepoGroupManager { fs::remove_dir_all(self.pkg_dir.join(sub_path))?; // Removing the "any" architecture updates all other repositories - if arch == ANY_ARCH { + if arch == super::ANY_ARCH { self.sync_all(repo)?; } @@ -250,7 +248,7 @@ impl RepoGroupManager { })?; if sync { - if arch == ANY_ARCH { + if arch == super::ANY_ARCH { self.sync_all(repo)?; } else { self.sync(repo, arch)?; @@ -288,7 +286,7 @@ impl RepoGroupManager { fs::remove_dir_all(self.repo_dir.join(repo).join(arch).join(metadata_dir_name))?; if sync { - if arch == ANY_ARCH { + if arch == super::ANY_ARCH { self.sync_all(&repo.to_string_lossy())?; } else { self.sync(&repo.to_string_lossy(), arch)?; diff --git a/server/src/repo/mod.rs b/server/src/repo/mod.rs index 44f0e10..958420e 100644 --- a/server/src/repo/mod.rs +++ b/server/src/repo/mod.rs @@ -2,3 +2,6 @@ pub mod manager; pub mod package; pub use manager::RepoGroupManager; + +pub const DB_FILE_EXTS: [&str; 4] = [".db", ".files", ".db.tar.gz", ".files.tar.gz"]; +pub const ANY_ARCH: &str = "any"; diff --git a/server/src/web/repo.rs b/server/src/web/repo.rs index f8c3d65..9466af5 100644 --- a/server/src/web/repo.rs +++ b/server/src/web/repo.rs @@ -16,8 +16,6 @@ use tower_http::services::{ServeDir, ServeFile}; use tower_http::validate_request::ValidateRequestHeaderLayer; use uuid::Uuid; -const DB_FILE_EXTS: [&str; 4] = [".db", ".files", ".db.tar.gz", ".files.tar.gz"]; - pub fn router(api_key: &str) -> Router { Router::new() .route( @@ -51,7 +49,10 @@ async fn get_file( let repo_dir = global.config.repo_dir.join(&repo).join(&arch); let repo_exists = tokio::fs::try_exists(&repo_dir).await?; - let res = if DB_FILE_EXTS.iter().any(|ext| file_name.ends_with(ext)) { + let res = if crate::repo::DB_FILE_EXTS + .iter() + .any(|ext| file_name.ends_with(ext)) + { // Append tar extension to ensure we find the file if !file_name.ends_with(".tar.gz") { file_name.push_str(".tar.gz"); @@ -64,7 +65,7 @@ async fn get_file( .config .repo_dir .join(repo) - .join(crate::repo::manager::ANY_ARCH) + .join(crate::repo::ANY_ARCH) .join(file_name); ServeFile::new(path).oneshot(req).await @@ -74,7 +75,7 @@ async fn get_file( .config .pkg_dir .join(repo) - .join(crate::repo::manager::ANY_ARCH) + .join(crate::repo::ANY_ARCH) .join(file_name); if repo_exists {