refactor(server): move some consts around
ci/woodpecker/push/lint Pipeline was successful Details
ci/woodpecker/push/clippy Pipeline failed Details
ci/woodpecker/push/build Pipeline was successful Details

distro
Jef Roosens 2023-08-17 14:25:25 +02:00
parent 50ebffb459
commit 9ad19eb36d
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 15 additions and 13 deletions

View File

@ -5,8 +5,6 @@ use std::fs;
use std::io; use std::io;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
pub const ANY_ARCH: &str = "any";
/// Overarching abstraction that orchestrates updating the repositories stored on the server /// Overarching abstraction that orchestrates updating the repositories stored on the server
pub struct RepoGroupManager { pub struct RepoGroupManager {
repo_dir: PathBuf, repo_dir: PathBuf,
@ -48,9 +46,9 @@ impl RepoGroupManager {
// All architectures should also include the "any" architecture, except for the "any" // All architectures should also include the "any" architecture, except for the "any"
// architecture itself. // 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()?) Some(repo_any_dir.read_dir()?)
} else { } else {
None None
@ -159,7 +157,7 @@ impl RepoGroupManager {
pkg.write_files(&mut files_file)?; pkg.write_files(&mut files_file)?;
// If a package of type "any" is added, we need to update every existing database // 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)?; self.sync_all(repo)?;
} else { } else {
self.sync(repo, &pkg.info.arch)?; self.sync(repo, &pkg.info.arch)?;
@ -193,7 +191,7 @@ impl RepoGroupManager {
fs::remove_dir_all(self.pkg_dir.join(sub_path))?; fs::remove_dir_all(self.pkg_dir.join(sub_path))?;
// Removing the "any" architecture updates all other repositories // Removing the "any" architecture updates all other repositories
if arch == ANY_ARCH { if arch == super::ANY_ARCH {
self.sync_all(repo)?; self.sync_all(repo)?;
} }
@ -250,7 +248,7 @@ impl RepoGroupManager {
})?; })?;
if sync { if sync {
if arch == ANY_ARCH { if arch == super::ANY_ARCH {
self.sync_all(repo)?; self.sync_all(repo)?;
} else { } else {
self.sync(repo, arch)?; 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))?; fs::remove_dir_all(self.repo_dir.join(repo).join(arch).join(metadata_dir_name))?;
if sync { if sync {
if arch == ANY_ARCH { if arch == super::ANY_ARCH {
self.sync_all(&repo.to_string_lossy())?; self.sync_all(&repo.to_string_lossy())?;
} else { } else {
self.sync(&repo.to_string_lossy(), arch)?; self.sync(&repo.to_string_lossy(), arch)?;

View File

@ -2,3 +2,6 @@ pub mod manager;
pub mod package; pub mod package;
pub use manager::RepoGroupManager; 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";

View File

@ -16,8 +16,6 @@ use tower_http::services::{ServeDir, ServeFile};
use tower_http::validate_request::ValidateRequestHeaderLayer; use tower_http::validate_request::ValidateRequestHeaderLayer;
use uuid::Uuid; use uuid::Uuid;
const DB_FILE_EXTS: [&str; 4] = [".db", ".files", ".db.tar.gz", ".files.tar.gz"];
pub fn router(api_key: &str) -> Router<crate::Global> { pub fn router(api_key: &str) -> Router<crate::Global> {
Router::new() Router::new()
.route( .route(
@ -51,7 +49,10 @@ async fn get_file(
let repo_dir = global.config.repo_dir.join(&repo).join(&arch); let repo_dir = global.config.repo_dir.join(&repo).join(&arch);
let repo_exists = tokio::fs::try_exists(&repo_dir).await?; 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 // Append tar extension to ensure we find the file
if !file_name.ends_with(".tar.gz") { if !file_name.ends_with(".tar.gz") {
file_name.push_str(".tar.gz"); file_name.push_str(".tar.gz");
@ -64,7 +65,7 @@ async fn get_file(
.config .config
.repo_dir .repo_dir
.join(repo) .join(repo)
.join(crate::repo::manager::ANY_ARCH) .join(crate::repo::ANY_ARCH)
.join(file_name); .join(file_name);
ServeFile::new(path).oneshot(req).await ServeFile::new(path).oneshot(req).await
@ -74,7 +75,7 @@ async fn get_file(
.config .config
.pkg_dir .pkg_dir
.join(repo) .join(repo)
.join(crate::repo::manager::ANY_ARCH) .join(crate::repo::ANY_ARCH)
.join(file_name); .join(file_name);
if repo_exists { if repo_exists {