diff options
Diffstat (limited to 'server/registry/src/list.rs')
-rw-r--r-- | server/registry/src/list.rs | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/server/registry/src/list.rs b/server/registry/src/list.rs index 1c2cd4a3..5684b473 100644 --- a/server/registry/src/list.rs +++ b/server/registry/src/list.rs @@ -20,9 +20,13 @@ use anyhow::Result; use hurrycurry_protocol::registry::Entry; use rocket::{ get, - http::MediaType, + http::{Header, MediaType}, request::{self, FromRequest, Outcome}, - response::content::{RawHtml, RawJson}, + response::{ + self, + content::{RawHtml, RawJson}, + Responder, + }, Either, Request, State, }; use std::sync::Arc; @@ -32,12 +36,12 @@ use tokio::sync::RwLock; pub(super) async fn r_list( registry: &State<Arc<RwLock<Registry>>>, json: AcceptJson, -) -> Either<RawJson<Arc<str>>, RawHtml<Arc<str>>> { - if json.0 { +) -> Cors<Either<RawJson<Arc<str>>, RawHtml<Arc<str>>>> { + Cors(if json.0 { Either::Left(RawJson(registry.read().await.json_response.clone())) } else { Either::Right(RawHtml(registry.read().await.html_response.clone())) - } + }) } pub(super) fn generate_json_list(entries: &[Entry]) -> Result<Arc<str>> { @@ -100,3 +104,13 @@ impl<'r> FromRequest<'r> for AcceptJson { }) } } + +pub struct Cors<T>(pub T); +#[rocket::async_trait] +impl<'r, T: Responder<'r, 'static>> Responder<'r, 'static> for Cors<T> { + fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> { + let mut b = self.0.respond_to(req)?; + b.set_header(Header::new("access-control-allow-origin", "*")); + Ok(b) + } +} |