feat: serve repo files

main
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

4
Cargo.toml 100644
View File

@ -0,0 +1,4 @@
[workspace]
members = [
'server'
]

1
server/.gitignore vendored 100644
View File

@ -0,0 +1 @@
data/

12
server/Cargo.toml 100644
View File

@ -0,0 +1,12 @@
[package]
name = "rieterd"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
axum = "0.6.18"
tokio = { version = "1.29.1", features = ["full"] }
tower = { version = "0.4.13", features = ["make"] }
tower-http = { version = "0.4.1", features = ["fs"] }

31
server/src/main.rs 100644
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();
}

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())
}