more specific macro rules for html css and js in router

This commit is contained in:
shockrah 2019-11-10 22:28:13 -08:00
parent e1f223f233
commit 1e525e3c05

View File

@ -27,10 +27,19 @@ struct PageAttrs {
brand_quip: &'static str
}
macro_rules! page {
($type:expr, $item:expr) => {
// TODO: verify against directory traversals
NamedFile::open(format!("static/{}/{}.{}", $type, $item, $type))
macro_rules! html {
($page:expr) => {
NamedFile::open(format!("static/html/{}", $page))
}
}
macro_rules! css {
($style:expr) => {
NamedFile::open(format!("static/css/{}", $style))
}
}
macro_rules! js {
($js:expr) => {
NamedFile::open(format!("static/js/{}", $js))
}
}
@ -51,36 +60,46 @@ fn homepage() -> Template {
favicon: "https://images.pexels.com/photos/146071/pexels-photo-146071.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260",
brand_url: "freechat.io",
brand_motto: "Freedom respecting chat platform",
brand_quip: "asdf",
brand_quip: "Join an realm",
};
Template::render("index", &context)
}
// Handles logging in a user to their home instance
#[get("/login")]
fn login_page() -> io::Result<NamedFile> {
page!("html", "login")
html!("login")
}
#[get("/servers")]
fn server_info() -> io::Result<NamedFile> {
page!("html", "servers")
fn server_info() -> Template {
let context = PageAttrs{
og_title: "title",
og_type: "title",
og_image: "asdf",
og_desc: "title",
og_url: "title",
favicon: "title",
brand_url: "title",
brand_motto: "title",
brand_quip: "title",
};
Template::render("servers", &context)
}
// Genearl resources
// General resources
#[get("/static/css/<stylesheet>")]
fn static_css(stylesheet: String) -> io::Result<NamedFile> {
page!("css", stylesheet)
println!("{}", stylesheet);
css!(stylesheet)
}
#[get("/static/js/<javascript>")]
fn static_js(javascript: String) -> io::Result<NamedFile> {
page!("js", javascript)
js!(javascript)
}
fn main() {
rocket::ignite()
.mount("/", routes![
homepage, login_page, server_info
])
.mount("/static", routes![
homepage, login_page, server_info,
static_css, static_js
])
.attach(Template::fairing())