2023-07-03 08:29:03 +00:00
|
|
|
use crate::{
|
2023-07-11 08:04:53 +00:00
|
|
|
links::{next_name, next_url, previous_name, previous_url},
|
2023-07-13 05:10:08 +00:00
|
|
|
responders::CorsResponse,
|
2023-07-04 05:14:10 +00:00
|
|
|
sites::get_global_names,
|
2023-07-03 08:29:03 +00:00
|
|
|
};
|
2023-07-13 05:10:08 +00:00
|
|
|
use meowy_assets::{
|
|
|
|
files::{get_file_wrapper, FileMetadata},
|
|
|
|
templates::{BaseTemplate, ErrorTemplate, IndexTemplate},
|
|
|
|
};
|
2023-06-30 02:45:41 +00:00
|
|
|
use rocket::{
|
|
|
|
http::Status,
|
|
|
|
response::Redirect,
|
|
|
|
serde::{json::Json, Serialize},
|
|
|
|
};
|
|
|
|
|
2023-07-11 08:04:53 +00:00
|
|
|
fn get_hash_filename(metadata: &FileMetadata) -> Result<String, Status> {
|
|
|
|
Ok(metadata.get_hash_filename())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_base_template() -> Result<BaseTemplate, Status> {
|
|
|
|
let files = get_file_wrapper()?;
|
|
|
|
|
2023-07-13 01:41:39 +00:00
|
|
|
let hyperlegible_filename = get_hash_filename(&files.hyperlegible.metadata)?;
|
2023-07-11 08:04:53 +00:00
|
|
|
let style_filename = get_hash_filename(&files.style.metadata)?;
|
|
|
|
|
|
|
|
let template = BaseTemplate {
|
2023-07-13 01:41:39 +00:00
|
|
|
hyperlegible_filename,
|
2023-07-11 08:04:53 +00:00
|
|
|
style_filename,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(template)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn not_found_error() -> Result<ErrorTemplate<'static>, Status> {
|
|
|
|
let base_template = get_base_template()?;
|
|
|
|
let template = ErrorTemplate {
|
|
|
|
error: "Not Found",
|
|
|
|
error_description: "this URL could not be found on the webring.",
|
|
|
|
base_template,
|
|
|
|
};
|
|
|
|
Ok(template)
|
|
|
|
}
|
2023-06-30 02:45:41 +00:00
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(crate = "rocket::serde")]
|
|
|
|
pub struct JsonResponse {
|
|
|
|
previous_site_name: Option<String>,
|
|
|
|
next_site_name: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/")]
|
2023-07-11 08:04:53 +00:00
|
|
|
pub async fn index() -> Result<IndexTemplate, Status> {
|
|
|
|
let base_template = get_base_template()?;
|
|
|
|
let template = IndexTemplate {
|
2023-07-05 09:10:31 +00:00
|
|
|
sites: get_global_names().await,
|
2023-07-11 08:04:53 +00:00
|
|
|
base_template,
|
|
|
|
};
|
|
|
|
Ok(template)
|
2023-06-30 02:45:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/previous?<source_url>")]
|
2023-07-04 05:14:10 +00:00
|
|
|
pub async fn previous(source_url: String) -> Result<Redirect, Status> {
|
|
|
|
match previous_url(&source_url, &get_global_names().await) {
|
2023-06-30 02:45:41 +00:00
|
|
|
Some(url) => Ok(Redirect::to(format!("https://{}", url))),
|
|
|
|
None => Err(Status::NotFound),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/next?<source_url>")]
|
2023-07-04 05:14:10 +00:00
|
|
|
pub async fn next(source_url: String) -> Result<Redirect, Status> {
|
|
|
|
match next_url(&source_url, &get_global_names().await) {
|
2023-06-30 02:45:41 +00:00
|
|
|
Some(url) => Ok(Redirect::to(format!("https://{}", url))),
|
|
|
|
None => Err(Status::NotFound),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/name?<source_url>")]
|
2023-07-13 05:10:08 +00:00
|
|
|
pub async fn name(source_url: String) -> Result<CorsResponse<Json<JsonResponse>>, Status> {
|
2023-07-05 09:10:31 +00:00
|
|
|
let previous_site_name = previous_name(&source_url, &get_global_names().await);
|
|
|
|
let next_site_name = next_name(&source_url, &get_global_names().await);
|
2023-06-30 02:45:41 +00:00
|
|
|
|
|
|
|
if previous_site_name.is_none() && next_site_name.is_none() {
|
2023-07-13 05:10:08 +00:00
|
|
|
return Err(Status::NotFound);
|
2023-06-30 02:45:41 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 05:10:08 +00:00
|
|
|
Ok(Json(JsonResponse {
|
2023-06-30 02:45:41 +00:00
|
|
|
previous_site_name,
|
|
|
|
next_site_name,
|
2023-07-13 05:10:08 +00:00
|
|
|
})
|
|
|
|
.into())
|
2023-06-30 02:45:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[catch(404)]
|
2023-07-11 08:04:53 +00:00
|
|
|
pub fn not_found() -> Result<ErrorTemplate<'static>, Status> {
|
|
|
|
not_found_error()
|
2023-06-30 02:45:41 +00:00
|
|
|
}
|