feat: serve repo files
parent
521f3149ad
commit
3e5ac03f2d
|
@ -0,0 +1,4 @@
|
|||
[workspace]
|
||||
members = [
|
||||
'server'
|
||||
]
|
|
@ -0,0 +1 @@
|
|||
data/
|
|
@ -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"] }
|
|
@ -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();
|
||||
}
|
|
@ -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())
|
||||
}
|
Loading…
Reference in New Issue