From ddf51c31b66572aea9f06fc723e0437288f8139b Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sat, 23 Jul 2022 16:48:39 +0100 Subject: [PATCH] Add CORS layer to reply to OPTIONS requests --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 6 +++++- src/methods/repo/mod.rs | 5 ++++- src/methods/repo/smart_git.rs | 11 +++++++++++ 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 699921a..2f8aacd 100644 --- a/Cargo.lock +++ a/Cargo.lock @@ -2073,6 +2073,7 @@ "tokio", "tokio-util", "tower", + "tower-http", "tower-layer", "tower-service", "tracing", diff --git a/Cargo.toml b/Cargo.toml index 80c672d..2940521 100644 --- a/Cargo.toml +++ a/Cargo.toml @@ -34,6 +34,7 @@ tower = "0.4" tower-service = "0.3" tower-layer = "0.3" +tower-http = { version = "0.3.4", features = ["cors"] } tracing = "0.1" tracing-subscriber = "0.3" unix_mode = "0.1" diff --git a/src/main.rs b/src/main.rs index 1f85851..0de1a0a 100644 --- a/src/main.rs +++ a/src/main.rs @@ -1,8 +1,8 @@ #![deny(clippy::pedantic)] -use std::{sync::Arc, time::Duration}; use std::net::SocketAddr; use std::path::PathBuf; +use std::{sync::Arc, time::Duration}; use askama::Template; use axum::{ @@ -17,6 +17,7 @@ use bat::assets::HighlightingAssets; use clap::Parser; use syntect::html::ClassStyle; +use tower_http::cors::CorsLayer; use tower_layer::layer_fn; use tracing::{info, instrument}; @@ -102,7 +103,8 @@ .layer(layer_fn(LoggingMiddleware)) .layer(Extension(Arc::new(Git::new(syntax_set)))) .layer(Extension(db)) - .layer(Extension(Arc::new(args.scan_path))); + .layer(Extension(Arc::new(args.scan_path))) + .layer(CorsLayer::new()); axum::Server::bind(&args.bind_address) .serve(app.into_make_service_with_connect_info::()) diff --git a/src/methods/repo/mod.rs b/src/methods/repo/mod.rs index c27a381..e8ea4b3 100644 --- a/src/methods/repo/mod.rs +++ a/src/methods/repo/mod.rs @@ -50,7 +50,10 @@ bytes::Bytes: From, ::Error: std::error::Error + Send + Sync, { - let scan_path = request.extensions().get::>().expect("scan_path missing"); + let scan_path = request + .extensions() + .get::>() + .expect("scan_path missing"); let mut uri_parts: Vec<&str> = request .uri() diff --git a/src/methods/repo/smart_git.rs b/src/methods/repo/smart_git.rs index dc05ccb..1e077dc 100644 --- a/src/methods/repo/smart_git.rs +++ a/src/methods/repo/smart_git.rs @@ -63,7 +63,10 @@ let resp = cgi_to_response(&out.stdout)?; if out.stderr.len() > 0 { - warn!("Git returned an error: `{}`", String::from_utf8_lossy(&out.stderr)); + warn!( + "Git returned an error: `{}`", + String::from_utf8_lossy(&out.stderr) + ); } Ok(resp) @@ -100,7 +103,11 @@ ); } - if let Some(status) = response.headers_mut().remove("Status").filter(|s| s.len() >= 3) { + if let Some(status) = response + .headers_mut() + .remove("Status") + .filter(|s| s.len() >= 3) + { let status = &status.as_ref()[..3]; if let Ok(status) = StatusCode::from_bytes(status) { -- rgit 0.1.3