diff options
author | metamuffin <metamuffin@disroot.org> | 2024-09-28 14:41:00 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-09-28 14:42:33 +0200 |
commit | 7ec219079a6268febb42adea55bc1fd86bf23183 (patch) | |
tree | 58ca6a4808b987a1bf7084b2f6c5b22f31f51502 /server/registry/src | |
parent | 6b04789a60c8204d5507e607abbcd8c422b681ed (diff) | |
download | hurrycurry-7ec219079a6268febb42adea55bc1fd86bf23183.tar hurrycurry-7ec219079a6268febb42adea55bc1fd86bf23183.tar.bz2 hurrycurry-7ec219079a6268febb42adea55bc1fd86bf23183.tar.zst |
enable cors for registry llist endpoint; fixes #188
Diffstat (limited to 'server/registry/src')
-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) + } +} |