From 4fd3e42f8c894d33d77bf9060fbcac02e4eed711 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Tue, 8 Mar 2022 04:39:55 +0000 Subject: fix: cors --- src/lib.rs | 3 ++- src/routes.rs | 15 +++++++++------ src/utils.rs | 8 ++++++++ 3 files changed, 19 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 5f6f77d..229472a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -63,7 +63,8 @@ pub async fn main( Response::from_json(&serde_json::json!({ "crate_version": env!("CARGO_PKG_VERSION"), "git_commit_hash": env!("GIT_COMMIT_HASH"), - })) + }))? + .with_cors(&utils::cors()) }) .run(request, environment) .await diff --git a/src/routes.rs b/src/routes.rs index fa71294..7418987 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -21,7 +21,7 @@ use worker::{Response, Result}; use crate::{ structures::SenpyRandom, - utils::{filter_images_by_language, filter_languages, github_api}, + utils::{cors, filter_images_by_language, filter_languages, github_api}, }; pub fn index() -> Result { @@ -64,19 +64,21 @@ license `gnu general public license v3.0 (:code:`gpl-3.0-only`) `_"#, - ) + )? + .with_cors(&cors()) } pub async fn github() -> Result { - Response::from_json(&github_api().await.unwrap()) + Response::from_json(&github_api().await.unwrap())?.with_cors(&cors()) } pub async fn languages() -> Result { - Response::from_json(&filter_languages().await) + Response::from_json(&filter_languages().await)?.with_cors(&cors()) } pub async fn language(language: &str) -> Result { - Response::from_json(&filter_images_by_language(language).await) + Response::from_json(&filter_images_by_language(language).await)? + .with_cors(&cors()) } pub async fn random() -> Result { @@ -93,5 +95,6 @@ pub async fn random() -> Result { Response::from_json(&SenpyRandom { language: random_language.clone(), image: random_image.clone(), - }) + })? + .with_cors(&cors()) } diff --git a/src/utils.rs b/src/utils.rs index fd2ba5b..d1083a9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -16,6 +16,8 @@ // Copyright (C) 2022-2022 Fuwn // SPDX-License-Identifier: GPL-3.0-only +use worker::Cors; + use crate::{constants, structures::GitHubAPIResponse}; /// # Errors @@ -87,3 +89,9 @@ pub async fn filter_images_by_language(language: &str) -> Vec { images } + +pub fn cors() -> Cors { + Cors::default() + .with_origins(vec!["*"]) + .with_methods(vec![worker::Method::Get]) +} -- cgit v1.2.3