diff options
| author | Fuwn <[email protected]> | 2022-03-08 04:39:55 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-03-08 04:39:55 +0000 |
| commit | 4fd3e42f8c894d33d77bf9060fbcac02e4eed711 (patch) | |
| tree | 250daecc2eef6c4934af6aa1a8bc2a8ba92c7029 /src | |
| parent | fix(routes): index version (diff) | |
| download | api-worker-4fd3e42f8c894d33d77bf9060fbcac02e4eed711.tar.xz api-worker-4fd3e42f8c894d33d77bf9060fbcac02e4eed711.zip | |
fix: cors
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 3 | ||||
| -rw-r--r-- | src/routes.rs | 15 | ||||
| -rw-r--r-- | src/utils.rs | 8 |
3 files changed, 19 insertions, 7 deletions
@@ -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<Response> { @@ -64,19 +64,21 @@ license `gnu general public license v3.0 (:code:`gpl-3.0-only`) <https://github.com/senpy-club/api-worker/blob/main/LICENSE>`_"#, - ) + )? + .with_cors(&cors()) } pub async fn github() -> Result<Response> { - Response::from_json(&github_api().await.unwrap()) + Response::from_json(&github_api().await.unwrap())?.with_cors(&cors()) } pub async fn languages() -> Result<Response> { - Response::from_json(&filter_languages().await) + Response::from_json(&filter_languages().await)?.with_cors(&cors()) } pub async fn language(language: &str) -> Result<Response> { - 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<Response> { @@ -93,5 +95,6 @@ pub async fn random() -> Result<Response> { 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 <[email protected]> // 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<String> { images } + +pub fn cors() -> Cors { + Cors::default() + .with_origins(vec!["*"]) + .with_methods(vec![worker::Method::Get]) +} |