use crate::content_type::get_content_type; use actix_web::{http::header::ContentType, HttpRequest, HttpResponse}; struct Routes<'a> { login: &'a str, login_qr: &'a str, settings: &'a str, dochazka: &'a str, hodnoceni: &'a str, vyuka: &'a str, komunikace: &'a str, sprava_aplikace: &'a str, kalendar: &'a str, not_found: &'a str, } impl Routes<'static> { pub fn new() -> Self { Routes { login: "login/login.html", login_qr: "login/login_qr.html", settings: "pages/settings.html", dochazka: "pages/dochazka/dochazka.html", hodnoceni: "pages/hodnoceni/hodnoceni.html", vyuka: "pages/vyuka/vyuka.html", komunikace: "pages/komunikace/komunikace.html", sprava_aplikace: "pages/sprava-aplikace/sprava-aplikace.html", kalendar: "pages/calendar.html", not_found: "404.html", } } } pub async fn responder(req: HttpRequest) -> HttpResponse { let filename = req.match_info().get("filename").unwrap_or("index.html"); // let cookies = req.cookies().unwrap(); // println!("{:?}", cookies); let mut not_found_error: bool = false; let routes = Routes::new(); let filename_wread = if filename != "index.html" { match filename { "login" => routes.login, "login_qr" => routes.login_qr, "settings" => routes.settings, "dochazka" => routes.dochazka, "hodnoceni" => routes.hodnoceni, "vyuka" => routes.vyuka, "komunikace" => routes.komunikace, "sprava-aplikace" => routes.sprava_aplikace, "kalendar" => routes.kalendar, _ => { not_found_error = true; routes.not_found } } } else { filename }; let content_type = get_content_type(filename_wread.to_string()); let content = std::fs::read_to_string(format!("web/{filename_wread}")).unwrap(); if not_found_error { return HttpResponse::NotFound() .content_type(content_type) .body(content); } HttpResponse::Ok().content_type(content_type).body(content) } pub async fn message_responder() -> HttpResponse { let content = std::fs::read_to_string("web/pages/message.html"); HttpResponse::Ok() .content_type(ContentType::html()) .body(content.unwrap()) } pub async fn subject_info_responder() -> HttpResponse { let content = std::fs::read_to_string("web/pages/subject-info.html"); HttpResponse::Ok() .content_type(ContentType::html()) .body(content.unwrap()) } pub async fn grade_responder() -> HttpResponse { let content = std::fs::read_to_string("web/pages/grade.html"); HttpResponse::Ok() .content_type(ContentType::html()) .body(content.unwrap()) } pub async fn homework_responder() -> HttpResponse { let content = std::fs::read_to_string("web/pages/homework-info.html"); HttpResponse::Ok() .content_type(ContentType::html()) .body(content.unwrap()) } pub async fn manifest_responder() -> HttpResponse { let content = std::fs::read_to_string("web/manifest.json"); HttpResponse::Ok() .content_type(ContentType::json()) .body(content.unwrap()) } pub async fn sw_responder() -> HttpResponse { let content = std::fs::read_to_string("web/sw.js"); HttpResponse::Ok() .content_type(ContentType::html()) .body(content.unwrap()) }