[Add] Console Features, QOL

This commit is contained in:
Alex Emmet 2026-02-19 22:58:22 +01:00
commit faf60b144a
11 changed files with 707 additions and 560 deletions

View file

@ -1,5 +1,4 @@
use actix_web::{HttpRequest, HttpResponse, web};
use json::JsonValue;
use actix_web::{HttpRequest, HttpResponse};
use std::path::{Path, PathBuf};
use crate::util::file_util::load_file_vec;
@ -17,32 +16,21 @@ fn codec_for_ext(ext: &str) -> &'static str {
}
}
pub async fn handle(req: HttpRequest, body: web::Bytes) -> HttpResponse {
// Optional JSON parsing
let _body_json: Option<JsonValue> = if !body.is_empty() {
json::parse(std::str::from_utf8(&body).unwrap_or("")).ok()
} else {
None
};
pub async fn handle(req: HttpRequest) -> HttpResponse {
let req_path = req.path().trim_start_matches('/');
// 1⃣ Resolve the filesystem path
let mut fs_path = PathBuf::from("web");
// Boolean P: no path provided → redirect to index.html
if req_path.is_empty() {
fs_path.push("index.html");
} else {
fs_path.extend(req_path.split('/'));
}
// Boolean D: path is directory → serve index.html inside
if fs_path.is_dir() {
fs_path.push("index.html");
}
// Boolean E: extension provided
let ext_opt = fs_path.extension().and_then(|e| e.to_str());
let mut final_name = fs_path
.file_name()
@ -51,7 +39,6 @@ pub async fn handle(req: HttpRequest, body: web::Bytes) -> HttpResponse {
.to_string();
if ext_opt.is_none() {
// No extension provided → try HTML
if final_name.is_empty() {
final_name = "index.html".to_string();
} else {
@ -68,12 +55,10 @@ pub async fn handle(req: HttpRequest, body: web::Bytes) -> HttpResponse {
let dir = fs_path.parent().unwrap_or(Path::new("web"));
// 2⃣ Try to load the resolved file
match load_file_vec(dir.to_str().unwrap_or("web"), &final_name) {
Ok(content) => HttpResponse::Ok().content_type(content_type).body(content),
Err(_) => {
// For static assets, return plain 404
let ext = fs_path.extension().and_then(|e| e.to_str()).unwrap_or("");
if matches!(ext, "js" | "css" | "woff2") {
return HttpResponse::NotFound()
@ -81,7 +66,6 @@ pub async fn handle(req: HttpRequest, body: web::Bytes) -> HttpResponse {
.body("Not found");
}
// 3⃣ Try to serve 404.html from web folder
let fallback = load_file_vec("web", "404.html")
.unwrap_or_else(|_| include_bytes!("../../static/web/404.html").to_vec());