feat: use of raw

This commit is contained in:
Alzalia 2025-09-04 18:17:35 +02:00
parent de1ef085ba
commit 9cef4843f0
3 changed files with 982 additions and 20 deletions

965
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -6,6 +6,7 @@ edition = "2024"
[dependencies] [dependencies]
axum = "0.8.4" axum = "0.8.4"
dotenv = "0.15.0" dotenv = "0.15.0"
reqwest = { version = "0.12.23", features = ["json"] }
serde = "1.0.219" serde = "1.0.219"
serde_json = "1.0.143" serde_json = "1.0.143"
tokio = { version = "1.47.1", features = ["full"] } tokio = { version = "1.47.1", features = ["full"] }

View file

@ -1,26 +1,27 @@
use std::{fs::File, io::Read}; // use std::{fs::File, io::Read};
use axum::{Json, extract::Path}; use axum::{Json, extract::Path};
use serde_json::{Value, from_str, json}; use serde_json::{Value, json};
// =================== // ===================
// =====< UTILS >===== // =====< UTILS >=====
// =================== // ===================
fn read_json(path: &str) -> std::io::Result<String> { // fn read_json(path: &str) -> std::io::Result<String> {
let mut file = File::open(path)?; // let mut file = File::open(path)?;
let mut contents = String::new(); // let mut contents = String::new();
file.read_to_string(&mut contents)?; // file.read_to_string(&mut contents)?;
Ok(contents) // Ok(contents)
} // }
// ==================== // ====================
// =====< ROUTES >===== // =====< ROUTES >=====
// ==================== // ====================
pub async fn get_home(Path(lang): Path<String>) -> Json<Value> { pub async fn get_home(Path(lang): Path<String>) -> Json<Value> {
if let Ok(file) = read_json(&format!("res/{lang}/home.json")) { let raw_url = dotenv::var("RAW_URL").expect("http://localhost");
Json(from_str(&file).unwrap()) if let Ok(response) = reqwest::get(format!("{raw_url}/res/{lang}/home.json")).await {
Json(response.json().await.unwrap())
} else { } else {
Json( Json(
json!({"title": "Error", "cards": [{ "title": "An error occured", "content": "Try reloading the page, or going back to the main page. Sorry !"}]}), json!({"title": "Error", "cards": [{ "title": "An error occured", "content": "Try reloading the page, or going back to the main page. Sorry !"}]}),
@ -29,8 +30,9 @@ pub async fn get_home(Path(lang): Path<String>) -> Json<Value> {
} }
pub async fn get_projects(Path(lang): Path<String>) -> Json<Value> { pub async fn get_projects(Path(lang): Path<String>) -> Json<Value> {
if let Ok(file) = read_json(&format!("res/{lang}/projects.json")) { let raw_url = dotenv::var("RAW_URL").expect("http://localhost");
Json(from_str(&file).unwrap()) if let Ok(response) = reqwest::get(format!("{raw_url}/res/{lang}/projects.json")).await {
Json(response.json().await.unwrap())
} else { } else {
Json( Json(
json!({"title": "Error", "cards": [{ "title": "An error occured", "content": "Try reloading the page, or going back to the main page. Sorry !"}]}), json!({"title": "Error", "cards": [{ "title": "An error occured", "content": "Try reloading the page, or going back to the main page. Sorry !"}]}),
@ -39,8 +41,9 @@ pub async fn get_projects(Path(lang): Path<String>) -> Json<Value> {
} }
pub async fn get_lessons(Path(lang): Path<String>) -> Json<Value> { pub async fn get_lessons(Path(lang): Path<String>) -> Json<Value> {
if let Ok(file) = read_json(&format!("res/{lang}/lessons.json")) { let raw_url = dotenv::var("RAW_URL").expect("http://localhost");
Json(from_str(&file).unwrap()) if let Ok(response) = reqwest::get(format!("{raw_url}/res/{lang}/lessons.json")).await {
Json(response.json().await.unwrap())
} else { } else {
Json( Json(
json!({"title": "Error", "cards": [{ "title": "An error occured", "content": "Try reloading the page, or going back to the main page. Sorry !"}]}), json!({"title": "Error", "cards": [{ "title": "An error occured", "content": "Try reloading the page, or going back to the main page. Sorry !"}]}),
@ -49,8 +52,9 @@ pub async fn get_lessons(Path(lang): Path<String>) -> Json<Value> {
} }
pub async fn get_lessons_by_id(Path((lang, id)): Path<(String, String)>) -> Json<Value> { pub async fn get_lessons_by_id(Path((lang, id)): Path<(String, String)>) -> Json<Value> {
if let Ok(file) = read_json(&format!("res/{lang}/lessons/{id}.json")) { let raw_url = dotenv::var("RAW_URL").expect("http://localhost");
Json(from_str(&file).unwrap()) if let Ok(response) = reqwest::get(format!("{raw_url}/res/{lang}/lessons/{id}.json")).await {
Json(response.json().await.unwrap())
} else { } else {
Json( Json(
json!({"title": "Error", "cards": [{ "title": "An error occured", "content": "Try reloading the page, or going back to the main page. Sorry !"}]}), json!({"title": "Error", "cards": [{ "title": "An error occured", "content": "Try reloading the page, or going back to the main page. Sorry !"}]}),