65 lines
1.5 KiB
Rust
65 lines
1.5 KiB
Rust
use crate::{
|
|
assets::ErrorTemplate,
|
|
links::{next_url, previous_url},
|
|
sites::{ Names},
|
|
};
|
|
|
|
use rocket::{
|
|
http::Status,
|
|
response::Redirect,
|
|
serde::{json::Json, Serialize},
|
|
};
|
|
|
|
const NOT_FOUND_ERROR: ErrorTemplate = ErrorTemplate {
|
|
error: "Not Found",
|
|
error_description: "this URL could not be found on the webring.",
|
|
};
|
|
|
|
#[derive(Serialize)]
|
|
#[serde(crate = "rocket::serde")]
|
|
pub struct JsonResponse {
|
|
previous_site_name: Option<String>,
|
|
next_site_name: Option<String>,
|
|
}
|
|
|
|
#[get("/")]
|
|
pub fn index() -> &'static str {
|
|
"Like, this is a webring, meow!"
|
|
}
|
|
|
|
#[get("/previous?<source_url>")]
|
|
pub fn previous(source_url: String) -> Result<Redirect, Status> {
|
|
match previous_url(&source_url, &Names::global()) {
|
|
Some(url) => Ok(Redirect::to(format!("https://{}", url))),
|
|
None => Err(Status::NotFound),
|
|
}
|
|
}
|
|
|
|
#[get("/next?<source_url>")]
|
|
pub fn next(source_url: String) -> Result<Redirect, Status> {
|
|
match next_url(&source_url, &Names::global()) {
|
|
Some(url) => Ok(Redirect::to(format!("https://{}", url))),
|
|
None => Err(Status::NotFound),
|
|
}
|
|
}
|
|
|
|
#[get("/name?<source_url>")]
|
|
pub fn name(source_url: String) -> Result<Json<JsonResponse>, Status> {
|
|
let previous_site_name = previous_url(&source_url, &Names::global());
|
|
let next_site_name = next_url(&source_url, &Names::global());
|
|
|
|
if previous_site_name.is_none() && next_site_name.is_none() {
|
|
return Err(Status::NotFound);
|
|
}
|
|
|
|
Ok(Json(JsonResponse {
|
|
previous_site_name,
|
|
next_site_name,
|
|
}))
|
|
}
|
|
|
|
#[catch(404)]
|
|
pub fn not_found() -> ErrorTemplate<'static> {
|
|
NOT_FOUND_ERROR
|
|
}
|