aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs39
1 files changed, 28 insertions, 11 deletions
diff --git a/src/main.rs b/src/main.rs
index 9251003..f1f9caa 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -49,6 +49,7 @@ mod get_routes;
mod origins;
mod post_routes;
mod shares;
+mod tests;
pub enum Response {
Raw(HyperResponse),
@@ -94,17 +95,22 @@ struct Args {
#[tokio::main]
async fn main() {
let args = Args::parse();
- let repo = Repository::open_bare(env::current_dir().unwrap())
+ let repo_path: &'static _ = Box::leak(Box::new(env::current_dir().unwrap()));
+ let repo = Repository::open_bare(repo_path)
.expect("expected current directory to be a bare Git repository");
if args.multiuser {
- serve(MultiUserController::new(&repo), args).await;
+ serve(repo_path, MultiUserController::new(&repo), args).await;
} else {
- serve(SoloController::new(&repo), args).await;
+ serve(repo_path, SoloController::new(&repo), args).await;
}
}
-async fn serve<C: Controller + Send + Sync + 'static>(controller: C, args: Args) {
+async fn serve<C: Controller + Send + Sync + 'static>(
+ repo_path: &'static Path,
+ controller: C,
+ args: Args,
+) {
let controller = Arc::new(controller);
#[cfg(unix)]
@@ -121,7 +127,7 @@ async fn serve<C: Controller + Send + Sync + 'static>(controller: C, args: Args)
async move {
Ok::<_, hyper::Error>(service_fn(move |req| {
- service(origin, controller.clone(), req)
+ service_wrapper(repo_path, origin, controller.clone(), req)
}))
}
});
@@ -173,7 +179,7 @@ async fn serve<C: Controller + Send + Sync + 'static>(controller: C, args: Args)
async move {
Ok::<_, hyper::Error>(service_fn(move |req| {
- service(origin, controller.clone(), req)
+ service_wrapper(repo_path, origin, controller.clone(), req)
}))
}
});
@@ -182,17 +188,27 @@ async fn serve<C: Controller + Send + Sync + 'static>(controller: C, args: Args)
server.await.expect("server error");
}
-async fn service<C: Controller>(
+async fn service_wrapper<C: Controller>(
+ repo_path: &Path,
origin: &HttpOrigin,
controller: Arc<C>,
request: Request,
) -> Result<HyperResponse, Infallible> {
+ Ok(service(repo_path, origin, &*controller, request).await)
+}
+
+async fn service<C: Controller>(
+ repo_path: &Path,
+ origin: &HttpOrigin,
+ controller: &C,
+ request: Request,
+) -> HyperResponse {
let (mut parts, body) = request.into_parts();
let mut script_csp = "'none'".into();
let mut frame_csp = "'none'";
- let mut resp = build_response(origin, &*controller, &mut parts, body)
+ let mut resp = build_response(repo_path, origin, controller, &mut parts, body)
.await
.map(|resp| match resp {
Response::Raw(resp) => resp,
@@ -205,7 +221,7 @@ async fn service<C: Controller>(
}
Builder::new()
.content_type(mime::TEXT_HTML)
- .body(render_page(&page, &*controller, &parts).into())
+ .body(render_page(&page, controller, &parts).into())
.unwrap()
}
})
@@ -224,7 +240,7 @@ async fn service<C: Controller>(
.parse()
.unwrap()
});
- Ok(resp)
+ resp
}
#[derive(Default)]
@@ -284,6 +300,7 @@ impl Branch {
}
async fn build_response<C: Controller>(
+ repo_path: &Path,
origin: &HttpOrigin,
controller: &C,
parts: &mut Parts,
@@ -309,7 +326,7 @@ async fn build_response<C: Controller>(
.map_err(|_| Error::BadRequest("failed to percent-decode path as UTF-8".into()))?
.into_owned();
- let repo = Repository::open_bare(env::current_dir().unwrap()).unwrap();
+ let repo = Repository::open_bare(repo_path).unwrap();
let (rev, unsanitized_path) = match controller.parse_url_path(&unsanitized_path, parts, &repo) {
Ok(parsed) => parsed,