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:
parent
ebe7b5cbc5
commit
515e5ed199
3 changed files with 58 additions and 17 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -873,6 +873,7 @@ dependencies = [
|
|||
"rocket_codegen",
|
||||
"rocket_http",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"state",
|
||||
"tempfile",
|
||||
"time",
|
||||
|
|
|
@ -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"
|
||||
rocket = { version = "=0.5.0-rc.3", features = ["json"] }
|
||||
|
|
70
src/main.rs
70
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<String>,
|
||||
next_site_name: Option<String>,
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
fn index() -> &'static str {
|
||||
"Like, this is a webring, meow!"
|
||||
}
|
||||
|
||||
#[get("/next?<source_url>")]
|
||||
fn next(source_url: &str) -> Redirect {
|
||||
// this is gay
|
||||
fn previous_url(source_url: &String, overflow: bool) -> Option<String> {
|
||||
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")),
|
||||
Some(index) if index == 0 => {
|
||||
if !overflow {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/prev?<source_url>")]
|
||||
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(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<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]
|
||||
fn rocket() -> _ {
|
||||
rocket::build().mount("/", routes![index, next, prev])
|
||||
rocket::build().mount("/", routes![index, previous, next, name])
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue