Files
hrtime/src/main.rs
2026-01-04 04:08:32 -05:00

51 lines
1.4 KiB
Rust

/* hrtime - transgender survey website
* Copyright (C) 2025 Olive <hello@grasswren.net>
* 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("/<file..>")]
async fn files(file: PathBuf) -> Option<NamedFile> {
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> {
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])
}