From 9e799f9009c427ba731b2158a77d5396e0a9fce5 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sat, 23 Jul 2022 16:17:13 +0100 Subject: [PATCH] Add argument parsing for bind address, scan path & database path --- Cargo.lock | 38 ++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 +- src/main.rs | 24 +++++++++++++++++++++++- src/database/indexer.rs | 3 +-- src/methods/repo/mod.rs | 4 +++- 5 files changed, 63 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2d08078..699921a 100644 --- a/Cargo.lock +++ a/Cargo.lock @@ -451,6 +451,7 @@ dependencies = [ "atty", "bitflags", + "clap_derive", "clap_lex", "indexmap", "once_cell", @@ -460,6 +461,19 @@ ] [[package]] +name = "clap_derive" +version = "3.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "759bf187376e1afa7b85b959e6a664a3e7a95203415dba952ad19139e798f902" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] name = "clap_lex" version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1777,6 +1791,30 @@ "log", "wepoll-ffi", "winapi", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index a27fb88..80c672d 100644 --- a/Cargo.toml +++ a/Cargo.toml @@ -14,7 +14,7 @@ bytes = "1.1" bincode = "1.3" comrak = "0.14" -clap = { version = "3.2", features = ["cargo"] } +clap = { version = "3.2", features = ["cargo", "derive"] } futures = "0.3" git2 = "0.14" hex = "0.4" diff --git a/src/main.rs b/src/main.rs index dfc33f8..1f85851 100644 --- a/src/main.rs +++ a/src/main.rs @@ -1,6 +1,8 @@ #![deny(clippy::pedantic)] use std::{sync::Arc, time::Duration}; +use std::net::SocketAddr; +use std::path::PathBuf; use askama::Template; use axum::{ @@ -13,6 +15,7 @@ Extension, Router, }; use bat::assets::HighlightingAssets; +use clap::Parser; use syntect::html::ClassStyle; use tower_layer::layer_fn; use tracing::{info, instrument}; @@ -27,8 +30,19 @@ const CRATE_VERSION: &str = clap::crate_version!(); +#[derive(Parser, Debug)] +#[clap(author, version, about)] +pub struct Args { + #[clap(short, long, value_parser)] + db_store: PathBuf, + bind_address: SocketAddr, + scan_path: PathBuf, +} + #[tokio::main] async fn main() { + let args: Args = Args::parse(); + let subscriber = tracing_subscriber::fmt(); #[cfg(debug_assertions)] let subscriber = subscriber.pretty(); @@ -36,16 +50,17 @@ let db = sled::Config::default() .use_compression(true) - .path("/tmp/some-sled.db") + .path(&args.db_store) .open() .unwrap(); std::thread::spawn({ let db = db.clone(); + let scan_path = args.scan_path.clone(); move || loop { info!("Running periodic index"); - crate::database::indexer::run(&db); + crate::database::indexer::run(&scan_path, &db); info!("Finished periodic index"); std::thread::sleep(Duration::from_secs(300)); @@ -86,9 +101,10 @@ .fallback(methods::repo::service.into_service()) .layer(layer_fn(LoggingMiddleware)) .layer(Extension(Arc::new(Git::new(syntax_set)))) - .layer(Extension(db)); + .layer(Extension(db)) + .layer(Extension(Arc::new(args.scan_path))); - axum::Server::bind(&"127.0.0.1:3333".parse().unwrap()) + axum::Server::bind(&args.bind_address) .serve(app.into_make_service_with_connect_info::()) .await .unwrap(); diff --git a/src/database/indexer.rs b/src/database/indexer.rs index 65187ed..a7254d1 100644 --- a/src/database/indexer.rs +++ a/src/database/indexer.rs @@ -13,13 +13,12 @@ tag::Tag, }; -pub fn run(db: &sled::Db) { +pub fn run(scan_path: &Path, db: &sled::Db) { let span = info_span!("index_update"); let _entered = span.enter(); info!("Starting index update"); - let scan_path = Path::new("/Users/jordan/Code/test-git"); update_repository_metadata(scan_path, db); update_repository_reflog(scan_path, db); update_repository_tags(scan_path, db); diff --git a/src/methods/repo/mod.rs b/src/methods/repo/mod.rs index 2cf2d54..c27a381 100644 --- a/src/methods/repo/mod.rs +++ a/src/methods/repo/mod.rs @@ -50,6 +50,8 @@ bytes::Bytes: From, ::Error: std::error::Error + Send + Sync, { + let scan_path = request.extensions().get::>().expect("scan_path missing"); + let mut uri_parts: Vec<&str> = request .uri() .path() @@ -111,7 +113,7 @@ }; let uri = uri_parts.into_iter().collect::().clean(); - let path = Path::new("../test-git").canonicalize().unwrap().join(&uri); + let path = scan_path.join(&uri); request.extensions_mut().insert(ChildPath(child_path)); request.extensions_mut().insert(Repository(uri)); -- rgit 0.1.3