2025-07-21 14:00:04 +02:00

80 lines
2.0 KiB
Rust

mod data;
mod redirecter;
mod status;
mod template;
use data::Data;
use redirecter::Redirecter;
use rocket::{
fs::{FileServer, Options},
get,
response::content::RawHtml,
routes, State,
};
use std::process::exit;
use template::Template;
#[get("/")]
async fn index(data: &State<Data>) -> RawHtml<String> {
index_gen(data, false).await
}
#[get("/dbg")]
async fn index_dbg(data: &State<Data>) -> RawHtml<String> {
index_gen(data, true).await
}
async fn index_gen(data: &State<Data>, dbg: bool) -> RawHtml<String> {
let status = data.status_async(dbg).await;
return RawHtml(data.index.gen(&status, dbg, data.globals.clone()));
}
#[get("/int")]
async fn int(data: &State<Data>) -> RawHtml<&str> {
RawHtml(&data.int_html)
}
#[rocket::launch]
fn rocket() -> _ {
let int_file = match std::fs::read_to_string("int.html") {
Ok(v) => v,
Err(e) => {
eprintln!("Could not read int.html: {e}");
exit(1);
}
};
let index_file = match std::fs::read_to_string("index.html") {
Ok(v) => v,
Err(e) => {
eprintln!("Could not read index.html: {e}");
exit(1);
}
};
let (int_html, variables, globals) = match Template::parse(&int_file, None) {
Ok(int_template) => int_template.gen_int(),
Err(e) => {
eprintln!("Could not parse int.html: {e}");
exit(1);
}
};
let index = match Template::parse(&index_file, Some(variables)) {
Ok(index_template) => index_template,
Err(e) => {
eprintln!("Could not parse index.html: {e}");
exit(1);
}
};
let data = Data::new_sync(int_html, index, globals);
rocket::build()
.manage(data)
.mount("/", routes![index, index_dbg, int])
.mount(
"/info",
FileServer::new(
"/srv/tomatenmhark-slashinfo/",
Options::Index | Options::NormalizeDirs | Options::Missing,
),
)
.mount("/", Redirecter)
}