feat: serve repo files

This commit is contained in:
Jef Roosens 2023-07-11 13:41:56 +02:00
parent 521f3149ad
commit 3e5ac03f2d
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
5 changed files with 60 additions and 0 deletions

31
server/src/main.rs Normal file
View file

@ -0,0 +1,31 @@
mod repo;
use axum::Router;
use std::path::PathBuf;
#[derive(Clone)]
pub struct Config {
data_dir: PathBuf,
repo_dir: PathBuf,
pkg_dir: PathBuf,
}
#[tokio::main]
async fn main() {
let config = Config {
data_dir: "./data".into(),
repo_dir: "./data/repos".into(),
pkg_dir: "./data/pkgs".into(),
};
// build our application with a single route
let app = Router::new()
.merge(repo::router(&config))
.with_state(config);
// run it with hyper on localhost:3000
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}

12
server/src/repo/mod.rs Normal file
View file

@ -0,0 +1,12 @@
use axum::extract::{Path, State};
use axum::routing::get_service;
use axum::Router;
use tower_http::services::ServeDir;
pub fn router(config: &crate::Config) -> Router<crate::Config> {
// Try to serve packages by default, and try the database files instead if not found
let service = ServeDir::new(&config.pkg_dir).fallback(ServeDir::new(&config.repo_dir));
Router::new()
.route("/*path", get_service(service))
.with_state(config.clone())
}