diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..8bda26b --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,4 @@ +[workspace] +members = [ + 'server' +] diff --git a/server/.gitignore b/server/.gitignore new file mode 100644 index 0000000..8fce603 --- /dev/null +++ b/server/.gitignore @@ -0,0 +1 @@ +data/ diff --git a/server/Cargo.toml b/server/Cargo.toml new file mode 100644 index 0000000..c5714c1 --- /dev/null +++ b/server/Cargo.toml @@ -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"] } diff --git a/server/src/main.rs b/server/src/main.rs new file mode 100644 index 0000000..3891722 --- /dev/null +++ b/server/src/main.rs @@ -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(); +} diff --git a/server/src/repo/mod.rs b/server/src/repo/mod.rs new file mode 100644 index 0000000..d58bc68 --- /dev/null +++ b/server/src/repo/mod.rs @@ -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 { + // 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()) +}