aboutsummaryrefslogtreecommitdiff
path: root/src/routes.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes.rs')
-rw-r--r--src/routes.rs41
1 files changed, 21 insertions, 20 deletions
diff --git a/src/routes.rs b/src/routes.rs
index d7512b4..8c831ac 100644
--- a/src/routes.rs
+++ b/src/routes.rs
@@ -1,17 +1,18 @@
// Copyleft (ɔ) 2021-2021 The Senpy Club
// SPDX-License-Identifier: GPL-3.0-only
+use actix_web::{HttpRequest, HttpResponse};
use rand::{thread_rng, Rng};
-use rocket_contrib::json::Json;
use crate::{
- structures::{GitHubAPIResponse, SenpyRandom},
+ structures::SenpyRandom,
utils::{filter_images_by_language, filter_languages, github_api},
};
#[get("/")]
-pub fn index() -> &'static str {
- r#"# senpy-api
+pub fn index() -> HttpResponse {
+ HttpResponse::Ok().body(
+ r#"# senpy-api
## routes
if a language requires a parameter, it will be notated like <this>.
for example; if a route is notated as "/api/v1/route?<parameter>", you can
@@ -41,38 +42,38 @@ https://github.com/fuwn
### license
gnu general public license v3.0 (gpl-3.0-only)
-https://github.com/senpy-club/api/blob/main/license"#
+https://github.com/senpy-club/api/blob/main/license"#,
+ )
}
#[get("/github")]
-pub async fn github() -> Json<GitHubAPIResponse> { Json(github_api().await.unwrap()) }
+pub async fn github() -> HttpResponse { HttpResponse::Ok().json(github_api().await.unwrap()) }
#[get("/languages")]
-pub async fn languages() -> Json<Vec<String>> { Json(filter_languages().await) }
+pub async fn languages() -> HttpResponse { HttpResponse::Ok().json(filter_languages().await) }
-#[get("/language?<lang>")]
-pub async fn language(lang: Option<String>) -> Json<Vec<String>> {
- // lang.map(async |lang| Json(filter_images_by_language(lang).await))
- // .unwrap_or_else(|| Json(vec!["invalid language or no language
- // specified".to_string()]));
+#[get("/language")]
+pub async fn language(req: HttpRequest) -> HttpResponse {
+ let queries = qstring::QString::from(req.query_string());
+ let lang = queries.get("lang").unwrap_or("null");
- return if lang.is_none() {
- Json(vec!["invalid language or no language specified".to_string()])
+ return if lang == "null" {
+ HttpResponse::Ok().json(vec!["invalid language or no language specified".to_string()])
} else {
- Json(filter_images_by_language(lang.unwrap()).await)
+ HttpResponse::Ok().json(filter_images_by_language(lang).await)
};
}
#[get("/random")]
-pub async fn random() -> Json<SenpyRandom> {
+pub async fn random() -> HttpResponse {
let filtered_languages = filter_languages().await;
let random_language =
&filtered_languages[thread_rng().gen_range(0..filtered_languages.len() - 1)];
- let filtered_images = filter_images_by_language(random_language.clone().to_owned()).await;
+ let filtered_images = filter_images_by_language(random_language).await;
let random_image = &filtered_images[thread_rng().gen_range(0..filtered_images.len() - 1)];
- Json(SenpyRandom {
- language: random_language.clone().to_owned(),
- image: random_image.clone().to_owned(),
+ HttpResponse::Ok().json(SenpyRandom {
+ language: random_language.clone(),
+ image: random_image.clone(),
})
}