This commit is contained in:
Ivan Bushchik 2023-06-21 09:36:49 +03:00
parent 44e3fa8f5e
commit 26fa8d9aa4
No known key found for this signature in database
GPG key ID: 9F6DDABE11A2674D

View file

@ -25,12 +25,12 @@
#[macro_use] #[macro_use]
extern crate rocket; extern crate rocket;
use rocket::http::Status;
use std::cell::OnceCell; use std::cell::OnceCell;
use std::path::PathBuf; use std::path::PathBuf;
use rocket::http::Status;
use rocket::response::content::RawText; use rocket::response::content::RawText;
use rocket::response::Redirect; use rocket::response::{Redirect, Responder};
use serde::Deserialize; use serde::Deserialize;
const INDEX_REDIRECT: &'static str = "https://ivabus.dev"; const INDEX_REDIRECT: &'static str = "https://ivabus.dev";
@ -44,8 +44,15 @@ struct Alias {
is_url: Option<bool>, is_url: Option<bool>,
} }
#[derive(Responder)]
enum Response {
Text(RawText<String>),
Redirect(Redirect),
Status(Status),
}
#[get("/<page>")] #[get("/<page>")]
async fn get_page(page: String) -> Result<RawText<String>, Redirect> { async fn get_page(page: String) -> Response {
let mut decoded_page = String::new(); let mut decoded_page = String::new();
url_escape::decode_to_string(page, &mut decoded_page); url_escape::decode_to_string(page, &mut decoded_page);
let alias = unsafe { ALIAS.get().unwrap() }; let alias = unsafe { ALIAS.get().unwrap() };
@ -53,13 +60,14 @@ async fn get_page(page: String) -> Result<RawText<String>, Redirect> {
for i in alias { for i in alias {
if i.uri == decoded_page { if i.uri == decoded_page {
return match i.is_url { return match i.is_url {
Some(true) => Err(Redirect::to(i.alias.clone())), Some(true) => Response::Redirect(Redirect::to(i.alias.clone())),
_ => Ok(RawText(smurf::io::read_file_to_str(&PathBuf::from(&i.alias)).unwrap())), _ => Response::Text(RawText(
smurf::io::read_file_to_str(&PathBuf::from(&i.alias)).unwrap(),
)),
}; };
} }
} }
Response::Status(Status::NotFound)
Err(Redirect::to("/404"))
} }
#[get("/")] #[get("/")]