2023-07-12 18:05:10 +00:00
|
|
|
mod manager;
|
2023-07-12 21:51:07 +00:00
|
|
|
mod package;
|
2023-07-12 18:05:10 +00:00
|
|
|
|
2023-07-13 12:58:27 +00:00
|
|
|
pub use manager::RepoGroupManager;
|
|
|
|
|
2023-07-12 09:04:31 +00:00
|
|
|
use axum::extract::{BodyStream, Path, State};
|
|
|
|
use axum::http::StatusCode;
|
2023-07-16 17:59:47 +00:00
|
|
|
use axum::routing::{delete, get_service, post};
|
2023-07-11 11:41:56 +00:00
|
|
|
use axum::Router;
|
2023-07-12 09:04:31 +00:00
|
|
|
use futures::StreamExt;
|
2023-07-13 18:24:45 +00:00
|
|
|
use std::sync::Arc;
|
2023-07-16 17:59:47 +00:00
|
|
|
use tokio::{fs, io::AsyncWriteExt};
|
2023-07-11 11:41:56 +00:00
|
|
|
use tower_http::services::ServeDir;
|
2023-07-12 09:04:31 +00:00
|
|
|
use uuid::Uuid;
|
2023-07-11 11:41:56 +00:00
|
|
|
|
2023-07-13 12:58:27 +00:00
|
|
|
pub fn router(global: &crate::Global) -> Router<crate::Global> {
|
2023-07-11 11:41:56 +00:00
|
|
|
// Try to serve packages by default, and try the database files instead if not found
|
2023-07-13 12:58:27 +00:00
|
|
|
let serve_repos = get_service(
|
|
|
|
ServeDir::new(&global.config.pkg_dir).fallback(ServeDir::new(&global.config.repo_dir)),
|
|
|
|
);
|
2023-07-11 11:41:56 +00:00
|
|
|
Router::new()
|
2023-07-12 09:04:31 +00:00
|
|
|
.route(
|
|
|
|
"/:repo",
|
2023-07-16 17:59:47 +00:00
|
|
|
post(post_package_archive)
|
|
|
|
.delete(delete_repo)
|
|
|
|
.get(serve_repos.clone()),
|
2023-07-12 09:04:31 +00:00
|
|
|
)
|
2023-07-16 17:59:47 +00:00
|
|
|
.route("/:repo/:arch", delete(delete_arch_repo))
|
2023-07-12 09:04:31 +00:00
|
|
|
.fallback(serve_repos)
|
2023-07-13 12:58:27 +00:00
|
|
|
.with_state(global.clone())
|
2023-07-11 11:41:56 +00:00
|
|
|
}
|
2023-07-12 09:04:31 +00:00
|
|
|
|
|
|
|
async fn post_package_archive(
|
2023-07-13 12:58:27 +00:00
|
|
|
State(global): State<crate::Global>,
|
2023-07-12 09:04:31 +00:00
|
|
|
Path(repo): Path<String>,
|
2023-07-14 11:46:53 +00:00
|
|
|
mut body: BodyStream,
|
|
|
|
) -> crate::Result<()> {
|
2023-07-12 09:04:31 +00:00
|
|
|
// We first stream the uploaded file to disk
|
|
|
|
let uuid: uuid::fmt::Simple = Uuid::new_v4().into();
|
2023-07-13 12:58:27 +00:00
|
|
|
let path = global.config.pkg_dir.join(uuid.to_string());
|
2023-07-14 11:46:53 +00:00
|
|
|
let mut f = fs::File::create(&path).await?;
|
2023-07-12 09:04:31 +00:00
|
|
|
|
|
|
|
while let Some(chunk) = body.next().await {
|
2023-07-14 11:46:53 +00:00
|
|
|
f.write_all(&chunk?).await?;
|
2023-07-12 09:04:31 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 18:24:45 +00:00
|
|
|
let clone = Arc::clone(&global.repo_manager);
|
|
|
|
tokio::task::spawn_blocking(move || clone.write().unwrap().add_pkg_from_path(&repo, &path))
|
2023-07-16 17:59:47 +00:00
|
|
|
.await??;
|
2023-07-14 11:46:53 +00:00
|
|
|
|
|
|
|
Ok(())
|
2023-07-12 09:04:31 +00:00
|
|
|
}
|
2023-07-16 17:59:47 +00:00
|
|
|
|
|
|
|
async fn delete_repo(
|
|
|
|
State(global): State<crate::Global>,
|
|
|
|
Path(repo): Path<String>,
|
|
|
|
) -> crate::Result<StatusCode> {
|
|
|
|
let clone = Arc::clone(&global.repo_manager);
|
|
|
|
|
|
|
|
let repo_removed =
|
|
|
|
tokio::task::spawn_blocking(move || clone.write().unwrap().remove_repo(&repo)).await??;
|
|
|
|
|
|
|
|
if repo_removed {
|
|
|
|
Ok(StatusCode::OK)
|
|
|
|
} else {
|
|
|
|
Ok(StatusCode::NOT_FOUND)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn delete_arch_repo(
|
|
|
|
State(global): State<crate::Global>,
|
|
|
|
Path((repo, arch)): Path<(String, String)>,
|
|
|
|
) -> crate::Result<StatusCode> {
|
|
|
|
let clone = Arc::clone(&global.repo_manager);
|
|
|
|
|
|
|
|
let repo_removed =
|
|
|
|
tokio::task::spawn_blocking(move || clone.write().unwrap().remove_arch_repo(&repo, &arch))
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
if repo_removed {
|
|
|
|
Ok(StatusCode::OK)
|
|
|
|
} else {
|
|
|
|
Ok(StatusCode::NOT_FOUND)
|
|
|
|
}
|
|
|
|
}
|