feat: add repo group manager to global context

main
Jef Roosens 2023-07-13 14:58:27 +02:00
parent d36b3bc1b2
commit f9c5faeda8
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 50 additions and 12 deletions

View File

@ -1,7 +1,10 @@
mod repo; mod repo;
use axum::Router; use axum::Router;
use repo::RepoGroupManager;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::{RwLock, Arc};
use axum::extract::FromRef;
#[derive(Clone)] #[derive(Clone)]
pub struct Config { pub struct Config {
@ -10,6 +13,18 @@ pub struct Config {
pkg_dir: PathBuf, pkg_dir: PathBuf,
} }
#[derive(Clone)]
pub struct Global {
config: Config,
repo_manager: Arc<RwLock<RepoGroupManager>>,
}
impl FromRef<Global> for Arc<RwLock<RepoGroupManager>> {
fn from_ref(global: &Global) -> Self {
Arc::clone(&global.repo_manager)
}
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let config = Config { let config = Config {
@ -17,11 +32,17 @@ async fn main() {
repo_dir: "./data/repos".into(), repo_dir: "./data/repos".into(),
pkg_dir: "./data/pkgs".into(), pkg_dir: "./data/pkgs".into(),
}; };
let repo_manager = RepoGroupManager::new("./data/repos", "./data/pkgs");
let global = Global {
config,
repo_manager: Arc::new(RwLock::new(repo_manager)),
};
// build our application with a single route // build our application with a single route
let app = Router::new() let app = Router::new()
.merge(repo::router(&config)) .merge(repo::router(&global))
.with_state(config); .with_state(global);
// run it with hyper on localhost:3000 // run it with hyper on localhost:3000
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())

View File

@ -7,7 +7,7 @@ pub struct RepoGroupManager {
} }
impl RepoGroupManager { impl RepoGroupManager {
fn new<P1: AsRef<Path>, P2: AsRef<Path>>(repo_dir: P1, pkg_dir: P2) -> Self { pub fn new<P1: AsRef<Path>, P2: AsRef<Path>>(repo_dir: P1, pkg_dir: P2) -> Self {
RepoGroupManager { RepoGroupManager {
repo_dir: repo_dir.as_ref().to_path_buf(), repo_dir: repo_dir.as_ref().to_path_buf(),
pkg_dir: pkg_dir.as_ref().to_path_buf(), pkg_dir: pkg_dir.as_ref().to_path_buf(),

View File

@ -1,6 +1,8 @@
mod manager; mod manager;
mod package; mod package;
pub use manager::RepoGroupManager;
use axum::extract::{BodyStream, Path, State}; use axum::extract::{BodyStream, Path, State};
use axum::http::StatusCode; use axum::http::StatusCode;
use axum::response::IntoResponse; use axum::response::IntoResponse;
@ -9,29 +11,27 @@ use axum::Router;
use futures::StreamExt; use futures::StreamExt;
use futures::TryFutureExt; use futures::TryFutureExt;
use futures::TryStreamExt; use futures::TryStreamExt;
use libarchive::archive::Entry;
use libarchive::read::Archive;
use libarchive::read::Builder;
use std::io::Read; use std::io::Read;
use tokio::{fs, io, io::AsyncWriteExt}; use tokio::{fs, io, io::AsyncWriteExt};
use tower_http::services::ServeDir; use tower_http::services::ServeDir;
use uuid::Uuid; use uuid::Uuid;
pub fn router(config: &crate::Config) -> Router<crate::Config> { pub fn router(global: &crate::Global) -> Router<crate::Global> {
// Try to serve packages by default, and try the database files instead if not found // Try to serve packages by default, and try the database files instead if not found
let serve_repos = let serve_repos = get_service(
get_service(ServeDir::new(&config.pkg_dir).fallback(ServeDir::new(&config.repo_dir))); ServeDir::new(&global.config.pkg_dir).fallback(ServeDir::new(&global.config.repo_dir)),
);
Router::new() Router::new()
.route( .route(
"/:repo", "/:repo",
post(post_package_archive).get(serve_repos.clone()), post(post_package_archive).get(serve_repos.clone()),
) )
.fallback(serve_repos) .fallback(serve_repos)
.with_state(config.clone()) .with_state(global.clone())
} }
async fn post_package_archive( async fn post_package_archive(
State(config): State<crate::Config>, State(global): State<crate::Global>,
Path(repo): Path<String>, Path(repo): Path<String>,
body: BodyStream, body: BodyStream,
) -> Result<(), StatusCode> { ) -> Result<(), StatusCode> {
@ -42,7 +42,7 @@ async fn post_package_archive(
// We first stream the uploaded file to disk // We first stream the uploaded file to disk
let uuid: uuid::fmt::Simple = Uuid::new_v4().into(); let uuid: uuid::fmt::Simple = Uuid::new_v4().into();
let path = config.pkg_dir.join(uuid.to_string()); let path = global.config.pkg_dir.join(uuid.to_string());
let mut f = fs::File::create(&path) let mut f = fs::File::create(&path)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR) .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
.await?; .await?;

View File

@ -176,4 +176,21 @@ impl Package {
Ok(()) Ok(())
} }
pub fn full_name(&self) -> String {
format!(
"{}-{}-{}",
self.info.name, self.info.version, self.info.arch
)
}
pub fn file_name(&self) -> String {
// This unwrap should be safe, because we only allow passing through compressions with
// known file extensions
format!(
"{}.pkg.tar{}",
self.full_name(),
self.compression.extension().unwrap()
)
}
} }