diff --git a/Cargo.lock b/Cargo.lock
@@ -451,6 +451,7 @@ checksum = "190814073e85d238f31ff738fcb0bf6910cedeb73376c87cd69291028966fd83"
dependencies = [
"atty",
"bitflags",
+ "clap_derive",
"clap_lex",
"indexmap",
"once_cell",
@@ -459,6 +460,19 @@ dependencies = [
"textwrap 0.15.0",
]
+[[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"
@@ -1779,6 +1793,30 @@ dependencies = [
"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]]
name = "proc-macro2"
version = "1.0.40"
diff --git a/Cargo.toml b/Cargo.toml
@@ -14,7 +14,7 @@ bat = { version = "0.21", default-features = false, features = ["build-assets"]
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/database/indexer.rs b/src/database/indexer.rs
@@ -13,13 +13,12 @@ use crate::database::schema::{
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/main.rs b/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 @@ use axum::{
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 @@ mod syntax_highlight;
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 @@ async fn main() {
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 @@ async fn main() {
.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::<std::net::SocketAddr>())
.await
.unwrap();
diff --git a/src/methods/repo/mod.rs b/src/methods/repo/mod.rs
@@ -50,6 +50,8 @@ where
bytes::Bytes: From<ReqBody::Data>,
<ReqBody as HttpBody>::Error: std::error::Error + Send + Sync,
{
+ let scan_path = request.extensions().get::<Arc<PathBuf>>().expect("scan_path missing");
+
let mut uri_parts: Vec<&str> = request
.uri()
.path()
@@ -111,7 +113,7 @@ where
};
let uri = uri_parts.into_iter().collect::<PathBuf>().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));