/* hrtime - transgender survey website * Copyright (C) 2025 Olive * see LICENCE file for licensing information */ pub mod schema; pub mod models; pub mod form; use std::path::{Path, PathBuf}; use rocket::Request; use rocket::fs::NamedFile; use rocket_sync_db_pools::{database, diesel}; #[database("db")] pub struct Database(diesel::SqliteConnection); #[rocket::get("/")] async fn files(file: PathBuf) -> Option { let pages = vec!["about", "future", "contact", "form", "thanks"]; let other = vec!["hamburger.svg", "logo.webp", "script.js", "style.css"]; let mut path = match file.to_str() { Some(path) => path.to_owned(), None => return NamedFile::open(Path::new("/var/www/404.html")).await.ok(), }; if path == "" { path = "index.html".to_string(); } else if pages.contains(&path.as_str()) { path += ".html"; } else if !other.contains(&path.as_str()) { path = "404.html".to_string(); } NamedFile::open(Path::new("/var/www/").join(path)).await.ok() } #[rocket::catch(404)] async fn not_found(_: &Request<'_>) -> Option { NamedFile::open(Path::new("/var/www/404.html")).await.ok() } #[rocket::launch] fn rocket() -> _ { rocket::build() .attach(Database::fairing()) .mount("/form", rocket::routes![form::form]) .mount("/", rocket::routes![files]) .register("/", rocket::catchers![not_found]) }