add a json api.

i added a json api called /name which lets you get the previous and next
site name. i also modified the internal url functions to return a None
value if overflow is turned off and next or previous is nothing.
This commit is contained in:
Fries 2023-06-28 16:12:55 -07:00
parent ebe7b5cbc5
commit 515e5ed199
3 changed files with 58 additions and 17 deletions

1
Cargo.lock generated
View file

@ -873,6 +873,7 @@ dependencies = [
"rocket_codegen", "rocket_codegen",
"rocket_http", "rocket_http",
"serde", "serde",
"serde_json",
"state", "state",
"tempfile", "tempfile",
"time", "time",

View file

@ -6,4 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
rocket = "=0.5.0-rc.3" rocket = { version = "=0.5.0-rc.3", features = ["json"] }

View file

@ -1,34 +1,74 @@
#[macro_use] #[macro_use]
extern crate rocket; extern crate rocket;
use rocket::response::Redirect; use rocket::response::Redirect;
use rocket::serde::{json::Json, Serialize};
static NAMES: [&str; 2] = ["mossfet.xyz", "fries.gay"]; static NAMES: [&str; 3] = ["mossfet.xyz", "fries.gay", "ta-kev.digital"];
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
struct JsonResponse {
previous_site_name: Option<String>,
next_site_name: Option<String>,
}
#[get("/")] #[get("/")]
fn index() -> &'static str { fn index() -> &'static str {
"Like, this is a webring, meow!" "Like, this is a webring, meow!"
} }
#[get("/next?<source_url>")] fn previous_url(source_url: &String, overflow: bool) -> Option<String> {
fn next(source_url: &str) -> Redirect {
// this is gay
match NAMES.iter().position(|&r| r == source_url) { match NAMES.iter().position(|&r| r == source_url) {
Some(index) if index == NAMES.len() - 1 => Redirect::to(format!("https://{}", NAMES[0])), Some(index) if index == 0 => {
Some(index) => Redirect::to(format!("https://{}", NAMES[index + 1])), if !overflow {
None => Redirect::to(uri!("/404.html")), return None;
} }
} Some(NAMES[NAMES.len() - 1].to_string())
},
#[get("/prev?<source_url>")] Some(index) => Some(NAMES[index - 1].to_string()),
fn prev(source_url: &str) -> Redirect {
match NAMES.iter().position(|&r| r == source_url) {
Some(index) if index == 0 => Redirect::to(format!("https://{}", NAMES[NAMES.len() - 1])),
Some(index) => Redirect::to(format!("https://{}", NAMES[index - 1])),
None => todo!(), None => todo!(),
} }
} }
fn next_url(source_url: &String, overflow: bool) -> Option<String> {
// this is gay
match NAMES.iter().position(|&r| r == source_url) {
Some(index) if index == NAMES.len() - 1 => {
if !overflow {
return None;
}
Some(NAMES[0].to_string())
}
Some(index) => Some(NAMES[index + 1].to_string()),
None => todo!(),
}
}
#[get("/previous?<source_url>")]
fn previous(source_url: String) -> Redirect {
match previous_url(&source_url, true) {
Some(url) => Redirect::to(format!("https://{}", url)),
None => todo!(),
}
}
#[get("/next?<source_url>")]
fn next(source_url: String) -> Redirect {
match next_url(&source_url, true) {
Some(url) => Redirect::to(format!("https://{}", url)),
None => todo!(),
}
}
#[get("/name?<source_url>")]
fn name(source_url: String) -> Json<JsonResponse> {
Json(JsonResponse {
previous_site_name: previous_url(&source_url, false),
next_site_name: next_url(&source_url, false),
})
}
#[launch] #[launch]
fn rocket() -> _ { fn rocket() -> _ {
rocket::build().mount("/", routes![index, next, prev]) rocket::build().mount("/", routes![index, previous, next, name])
} }