67 lines
1.7 KiB
Rust
67 lines
1.7 KiB
Rust
use rocket::fs::FileServer;
|
|
|
|
#[macro_use]
|
|
extern crate rocket;
|
|
|
|
pub mod items {
|
|
include!(concat!(env!("OUT_DIR"), "/items.rs"));
|
|
}
|
|
|
|
#[get("/<name>")]
|
|
fn get_user(user_list: &rocket::State<Vec<items::Person>>, name: String) -> Option<items::Person> {
|
|
user_list.iter().find(|user| user.name == name).cloned()
|
|
}
|
|
|
|
#[get("/")]
|
|
fn get_users(user_list: &rocket::State<Vec<items::Person>>) -> items::PersonList {
|
|
items::PersonList {
|
|
person: user_list
|
|
.inner()
|
|
.to_vec()
|
|
.iter_mut()
|
|
.map(|x| {
|
|
x.opinion.clear();
|
|
x.clone()
|
|
})
|
|
.collect(),
|
|
}
|
|
}
|
|
|
|
#[get("/<_..>", rank = 20)]
|
|
async fn index_fallback() -> Option<rocket::fs::NamedFile> {
|
|
// Try multiple paths for robustness
|
|
let paths = ["../frontend/dist/index.html", "frontend/dist/index.html"];
|
|
for path in paths {
|
|
if let Ok(file) = rocket::fs::NamedFile::open(path).await {
|
|
return Some(file);
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
#[launch]
|
|
fn rocket() -> _ {
|
|
let mut user_list: Vec<items::Person> = Vec::new();
|
|
|
|
user_list.push(items::Person {
|
|
name: "John".to_string(),
|
|
opinion: vec![items::Opinion {
|
|
game: Some(items::Game {
|
|
title: "Naramo Nuclear Plant V2".to_string(),
|
|
source: items::Source::Roblox.into(),
|
|
multiplayer: true,
|
|
min_players: 1,
|
|
max_players: 90,
|
|
price: 0,
|
|
}),
|
|
would_play: true,
|
|
}],
|
|
});
|
|
|
|
rocket::build()
|
|
.manage(user_list)
|
|
.mount("/api", routes![get_users, get_user])
|
|
.mount("/", routes![index_fallback])
|
|
.mount("/", FileServer::new("../frontend/dist"))
|
|
}
|