93 lines
2.4 KiB
Rust
93 lines
2.4 KiB
Rust
use askama_rocket::Template;
|
|
use rocket::{http::Status, response::content::RawCss};
|
|
use rust_embed::{EmbeddedFile, RustEmbed};
|
|
use shared::names::Site;
|
|
use std::borrow::Cow;
|
|
|
|
#[derive(RustEmbed)]
|
|
#[folder = "public/"]
|
|
pub struct PublicAssets;
|
|
|
|
impl PublicAssets {
|
|
fn get_asset(file_path: &str) -> Result<EmbeddedFile, Status> {
|
|
match PublicAssets::get(file_path) {
|
|
Some(asset) => Ok(asset),
|
|
None => Err(Status::NotFound),
|
|
}
|
|
}
|
|
fn get_string(file_path: &str) -> Result<String, Status> {
|
|
let asset = PublicAssets::get_asset(file_path)?;
|
|
match std::str::from_utf8(&asset.data) {
|
|
Ok(string) => Ok(string.to_string()),
|
|
Err(_) => Err(Status::InternalServerError),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Responder)]
|
|
#[response(status = 200, content_type = "font/woff2")]
|
|
pub struct RawWoff2Font(pub Cow<'static, [u8]>);
|
|
|
|
#[derive(Responder)]
|
|
#[response(status = 200, content_type = "font/woff")]
|
|
pub struct RawWoffFont(pub Cow<'static, [u8]>);
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "error.html")]
|
|
pub struct ErrorTemplate<'a> {
|
|
pub error: &'a str,
|
|
pub error_description: &'a str,
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "index.html")]
|
|
pub struct IndexTemplate {
|
|
pub sites: Vec<Site>,
|
|
}
|
|
|
|
#[derive(Responder)]
|
|
pub struct ErrorTemplateResponder<'a> {
|
|
template: ErrorTemplate<'a>,
|
|
}
|
|
|
|
#[get("/css/<style>")]
|
|
pub fn style(style: &str) -> Result<rocket::response::content::RawCss<String>, Status> {
|
|
let style_name = "style.css";
|
|
let hyperlegible_name = "hyperlegible.css";
|
|
|
|
if style == style_name {
|
|
let string = PublicAssets::get_string(style_name)?;
|
|
Ok(RawCss::<String>(string))
|
|
} else if style == hyperlegible_name {
|
|
let string = PublicAssets::get_string(hyperlegible_name)?;
|
|
Ok(RawCss::<String>(string))
|
|
} else {
|
|
Err(Status::NotFound)
|
|
}
|
|
}
|
|
|
|
#[get("/woff2/<font>")]
|
|
pub fn woff2_font(font: &str) -> Result<RawWoff2Font, Status> {
|
|
let latin = "atkinson-hyperlegible-latin-400-normal.woff2";
|
|
let latin_ext = "atkinson-hyperlegible-latin-ext-400-normal.woff2";
|
|
|
|
if font == latin {
|
|
Ok(RawWoff2Font(PublicAssets::get_asset(latin)?.data))
|
|
} else if font == latin_ext {
|
|
Ok(RawWoff2Font(PublicAssets::get_asset(latin_ext)?.data))
|
|
} else {
|
|
Err(Status::NotFound)
|
|
}
|
|
}
|
|
|
|
#[get("/woff/<font>")]
|
|
pub fn woff_font(font: &str) -> Result<RawWoffFont, Status> {
|
|
let all = "atkinson-hyperlegible-all-400-normal.woff";
|
|
|
|
if font == all {
|
|
Ok(RawWoffFont(PublicAssets::get_asset(all)?.data))
|
|
} else {
|
|
Err(Status::NotFound)
|
|
}
|
|
}
|