Compare commits

...

2 Commits

Author SHA1 Message Date
Jef Roosens 9ad19eb36d
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
2023-08-17 14:25:25 +02:00
Jef Roosens 50ebffb459
feat(server): POST request to create distros 2023-08-17 10:56:08 +02:00
7 changed files with 35 additions and 17 deletions

View File

@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize};
#[sea_orm(table_name = "distro")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
pub id: i32,
#[sea_orm(unique)]
pub slug: String,

View File

@ -43,4 +43,10 @@ impl DistroQuery {
Distro::insert(model).exec(&self.conn).await
}
pub async fn insert_model(&self, model: distro::Model) -> Result<distro::Model> {
let mut model: distro::ActiveModel = model.into();
model.id = NotSet;
model.insert(&self.conn).await
}
}

View File

@ -35,10 +35,12 @@ impl IntoResponse for ServerError {
ServerError::IO(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
ServerError::Axum(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
ServerError::Status(status) => status.into_response(),
ServerError::Db(sea_orm::DbErr::RecordNotFound(_)) => {
StatusCode::NOT_FOUND.into_response()
ServerError::Db(err) => match err {
sea_orm::DbErr::RecordNotFound(_) => StatusCode::NOT_FOUND,
sea_orm::DbErr::Query(_) => StatusCode::BAD_REQUEST,
_ => StatusCode::INTERNAL_SERVER_ERROR,
}
ServerError::Db(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
.into_response(),
}
}
}

View File

@ -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)?;

View File

@ -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";

View File

@ -9,7 +9,7 @@ use crate::db;
pub fn router() -> Router<crate::Global> {
Router::new()
.route("/", get(get_distros))
.route("/", get(get_distros).post(post_distro))
.route("/:id", get(get_single_distro))
}
@ -41,3 +41,10 @@ async fn get_single_distro(
Ok(Json(repo))
}
async fn post_distro(
State(global): State<crate::Global>,
Json(model): Json<db::distro::Model>,
) -> crate::Result<Json<db::distro::Model>> {
Ok(Json(global.db.distro.insert_model(model).await?))
}

View File

@ -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<crate::Global> {
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 {