website/src/main.rs

54 lines
1.2 KiB
Rust

#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate log;
#[macro_use]
extern crate rocket;
extern crate rocket_contrib;
#[macro_use]
extern crate serde_derive;
extern crate tera;
use std::path::{Path, PathBuf};
use rocket::response::NamedFile;
use rocket_contrib::templates::Template;
#[derive(Debug, Serialize)]
struct FlashContext {
flash_name: Option<String>,
flash_msg: Option<String>,
}
#[get("/")]
fn index() -> Template {
let context = FlashContext {
flash_name: None,
flash_msg: None,
};
Template::render("index", &context)
}
#[get("/<file..>")]
fn files(file: PathBuf) -> Option<NamedFile> {
NamedFile::open(Path::new("static/").join(file)).ok()
}
#[catch(404)]
fn not_found() -> Template {
debug!("404 Page Not Found");
let context = FlashContext {
flash_name: Some("error".to_string()),
flash_msg: Some("No resource found for given URL".to_string()),
};
Template::render("not_found", context)
}
fn main() {
rocket::ignite()
.mount("/", routes![index, files])
.register(catchers![not_found])
.attach(Template::fairing())
.launch();
}