aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs3
-rw-r--r--src/routes.rs15
-rw-r--r--src/utils.rs8
3 files changed, 19 insertions, 7 deletions
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<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])
+}