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;
|
|
|
|
use axum::response::IntoResponse;
|
|
|
|
use axum::routing::{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;
|
|
|
|
use futures::TryFutureExt;
|
|
|
|
use futures::TryStreamExt;
|
2023-07-12 18:05:10 +00:00
|
|
|
use std::io::Read;
|
2023-07-13 18:24:45 +00:00
|
|
|
use std::sync::Arc;
|
2023-07-12 09:04:31 +00:00
|
|
|
use tokio::{fs, io, 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",
|
|
|
|
post(post_package_archive).get(serve_repos.clone()),
|
|
|
|
)
|
|
|
|
.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>,
|
|
|
|
body: BodyStream,
|
|
|
|
) -> Result<(), StatusCode> {
|
|
|
|
// let mut body_reader = tokio_util::io::StreamReader::new(
|
|
|
|
// body.map_err(|err| io::Error::new(io::ErrorKind::Other, err)),
|
|
|
|
// );
|
|
|
|
let mut body = body.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR);
|
|
|
|
|
|
|
|
// 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-12 09:04:31 +00:00
|
|
|
let mut f = fs::File::create(&path)
|
|
|
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
while let Some(chunk) = body.next().await {
|
|
|
|
f.write_all(&chunk?)
|
|
|
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
.map_err(|err| {
|
|
|
|
println!("{}", err);
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR
|
|
|
|
})
|
|
|
|
.await?
|
|
|
|
.map_err(|err| {
|
|
|
|
println!("{}", err);
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR
|
|
|
|
})
|
2023-07-12 09:04:31 +00:00
|
|
|
}
|