diff --git a/Cargo.lock b/Cargo.lock index ea0f48f..66a32db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -873,6 +873,7 @@ dependencies = [ "rocket_codegen", "rocket_http", "serde", + "serde_json", "state", "tempfile", "time", diff --git a/Cargo.toml b/Cargo.toml index 20b7d6b..523f72e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -rocket = "=0.5.0-rc.3" \ No newline at end of file +rocket = { version = "=0.5.0-rc.3", features = ["json"] } diff --git a/src/main.rs b/src/main.rs index da72255..c64bb7d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,34 +1,74 @@ #[macro_use] extern crate rocket; 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, + next_site_name: Option, +} #[get("/")] fn index() -> &'static str { "Like, this is a webring, meow!" } -#[get("/next?")] -fn next(source_url: &str) -> Redirect { - // this is gay +fn previous_url(source_url: &String, overflow: bool) -> Option { match NAMES.iter().position(|&r| r == source_url) { - Some(index) if index == NAMES.len() - 1 => Redirect::to(format!("https://{}", NAMES[0])), - Some(index) => Redirect::to(format!("https://{}", NAMES[index + 1])), - None => Redirect::to(uri!("/404.html")), - } -} - -#[get("/prev?")] -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])), + Some(index) if index == 0 => { + if !overflow { + return None; + } + Some(NAMES[NAMES.len() - 1].to_string()) + }, + Some(index) => Some(NAMES[index - 1].to_string()), None => todo!(), } } +fn next_url(source_url: &String, overflow: bool) -> Option { + // 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?")] +fn previous(source_url: String) -> Redirect { + match previous_url(&source_url, true) { + Some(url) => Redirect::to(format!("https://{}", url)), + None => todo!(), + } +} + +#[get("/next?")] +fn next(source_url: String) -> Redirect { + match next_url(&source_url, true) { + Some(url) => Redirect::to(format!("https://{}", url)), + None => todo!(), + } +} + +#[get("/name?")] +fn name(source_url: String) -> Json { + Json(JsonResponse { + previous_site_name: previous_url(&source_url, false), + next_site_name: next_url(&source_url, false), + }) +} + #[launch] fn rocket() -> _ { - rocket::build().mount("/", routes![index, next, prev]) + rocket::build().mount("/", routes![index, previous, next, name]) }