From 2fef0f5c9c09b4df40f05452351208a09f15f585 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 5 Dec 2024 11:42:16 +0100 Subject: [PATCH] httpd: Move `CorsLayer` to `/` --- radicle-httpd/src/api.rs | 11 ----------- radicle-httpd/src/lib.rs | 13 ++++++++++++- radicle-httpd/src/raw.rs | 11 +---------- 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/radicle-httpd/src/api.rs b/radicle-httpd/src/api.rs index e42ac5840..1aa2d889e 100644 --- a/radicle-httpd/src/api.rs +++ b/radicle-httpd/src/api.rs @@ -1,14 +1,10 @@ use std::collections::BTreeMap; use std::sync::Arc; -use std::time::Duration; -use axum::http::header::CONTENT_TYPE; -use axum::http::Method; use axum::response::{IntoResponse, Json}; use axum::routing::get; use axum::Router; use serde_json::{json, Value}; -use tower_http::cors::{self, CorsLayer}; use radicle::identity::doc::PayloadId; use radicle::identity::{DocAt, RepoId}; @@ -122,13 +118,6 @@ pub fn router(ctx: Context) -> Router { Router::new() .route("/", get(root_handler)) .merge(v1::router(ctx)) - .layer( - CorsLayer::new() - .max_age(Duration::from_secs(86400)) - .allow_origin(cors::Any) - .allow_methods([Method::GET]) - .allow_headers([CONTENT_TYPE]), - ) } async fn root_handler() -> impl IntoResponse { diff --git a/radicle-httpd/src/lib.rs b/radicle-httpd/src/lib.rs index ac055244b..c1c1aa7d7 100644 --- a/radicle-httpd/src/lib.rs +++ b/radicle-httpd/src/lib.rs @@ -17,7 +17,11 @@ use axum::http::{Request, Response}; use axum::response::IntoResponse; use axum::routing::get; use axum::{middleware, Json, Router}; +use hyper::header::CONTENT_TYPE; +use hyper::Method; use tokio::net::TcpListener; +use tower_http::cors; +use tower_http::cors::CorsLayer; use tower_http::trace::TraceLayer; use tracing::Span; @@ -119,7 +123,14 @@ fn router(options: Options, profile: Profile) -> anyhow::Result { .route("/", get(root_index_handler)) .merge(git_router) .nest("/api", api_router) - .nest("/raw", raw_router); + .nest("/raw", raw_router) + .layer( + CorsLayer::new() + .max_age(Duration::from_secs(86400)) + .allow_origin(cors::Any) + .allow_methods([Method::GET]) + .allow_headers([CONTENT_TYPE]), + ); Ok(app) } diff --git a/radicle-httpd/src/raw.rs b/radicle-httpd/src/raw.rs index 4609c9454..256b8bce1 100644 --- a/radicle-httpd/src/raw.rs +++ b/radicle-httpd/src/raw.rs @@ -1,14 +1,12 @@ use std::sync::Arc; -use std::time::Duration; use axum::extract::{Query, State}; -use axum::http::{header, HeaderValue, Method, StatusCode}; +use axum::http::{header, HeaderValue, StatusCode}; use axum::response::IntoResponse; use axum::routing::get; use axum::Router; use hyper::HeaderMap; use radicle_surf::blob::{Blob, BlobRef}; -use tower_http::cors; use radicle::prelude::RepoId; use radicle::profile::Profile; @@ -99,13 +97,6 @@ pub fn router(profile: Arc) -> Router { .route("/:rid/head/*path", get(file_by_canonical_head_handler)) .route("/:rid/blobs/:oid", get(file_by_oid_handler)) .with_state(profile) - .layer( - cors::CorsLayer::new() - .max_age(Duration::from_secs(86400)) - .allow_origin(cors::Any) - .allow_methods([Method::GET]) - .allow_headers([header::CONTENT_TYPE]), - ) } async fn file_by_commit_handler(