2023-04-08 12:40:57 -05:00
|
|
|
//! # Rust Letter Backend
|
2023-04-09 12:34:40 -05:00
|
|
|
//!
|
2023-04-08 12:40:57 -05:00
|
|
|
//! `rust_letter_be` handles the backend execution using Rocket.
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate rocket;
|
|
|
|
|
|
|
|
/// A module that handles the backend for the site.
|
|
|
|
pub mod web_app_backend {
|
|
|
|
|
|
|
|
use rocket::fs::FileServer;
|
2023-04-10 18:19:02 -05:00
|
|
|
use rocket::http::{Cookie, CookieJar};
|
|
|
|
use rocket::response::Redirect;
|
2023-04-09 12:34:40 -05:00
|
|
|
use rocket::{Build, Rocket};
|
|
|
|
use rocket_dyn_templates::{context, Template};
|
2023-04-13 12:51:25 -05:00
|
|
|
use void_fe::void_app::{self, VirtualDom};
|
|
|
|
use void_fe::utils::prop_structs::{DarkModeProps, PoemRequest};
|
2023-04-08 12:40:57 -05:00
|
|
|
|
|
|
|
#[get("/")]
|
2023-04-10 18:19:02 -05:00
|
|
|
async fn index(cookies: &CookieJar<'_>) -> Template {
|
|
|
|
let dark_mode = match cookies.get("dark-mode") {
|
2023-04-11 19:42:46 -05:00
|
|
|
Some(c) => {
|
|
|
|
if c.value() == "true" {
|
|
|
|
true
|
|
|
|
} else if c.value() == "false" {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2023-04-10 18:19:02 -05:00
|
|
|
None => false,
|
|
|
|
};
|
2023-04-11 19:42:46 -05:00
|
|
|
let mut vdom = VirtualDom::new_with_props(
|
|
|
|
void_app::HomePage,
|
|
|
|
DarkModeProps {
|
2023-04-12 00:42:59 -05:00
|
|
|
slug: Some("/".to_string()),
|
2023-04-11 19:42:46 -05:00
|
|
|
dark_mode,
|
|
|
|
},
|
|
|
|
);
|
2023-04-08 12:40:57 -05:00
|
|
|
let _ = vdom.rebuild();
|
|
|
|
let output = dioxus_ssr::render(&vdom);
|
|
|
|
Template::render(
|
|
|
|
"index",
|
|
|
|
context! {
|
|
|
|
app_title: "A Letter to the Void",
|
|
|
|
style_include: "<link href=/styles/tailwind.min.css rel=stylesheet />",
|
2023-04-10 18:19:02 -05:00
|
|
|
test: &output,
|
|
|
|
dark_mode: match dark_mode {
|
|
|
|
true => "dark",
|
|
|
|
false => ""
|
|
|
|
},
|
2023-04-08 12:40:57 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2023-04-07 07:41:48 -05:00
|
|
|
|
2023-04-12 00:42:59 -05:00
|
|
|
#[get("/?dark_mode&<callback>")]
|
|
|
|
async fn dark_mode(cookies: &CookieJar<'_>, callback: &str) -> Redirect {
|
2023-04-10 18:19:02 -05:00
|
|
|
match cookies.get("dark-mode") {
|
|
|
|
Some(_) => cookies.remove(Cookie::named("dark-mode")),
|
|
|
|
None => cookies.add(Cookie::new("dark-mode", "true")),
|
|
|
|
};
|
2023-04-12 00:42:59 -05:00
|
|
|
let callback_uri = format!("{callback}");
|
|
|
|
Redirect::to(callback_uri)
|
2023-04-10 18:19:02 -05:00
|
|
|
}
|
|
|
|
|
2023-04-11 19:42:46 -05:00
|
|
|
#[get("/")]
|
2023-04-10 20:37:40 -05:00
|
|
|
async fn poem_list(cookies: &CookieJar<'_>) -> Template {
|
|
|
|
let dark_mode = match cookies.get("dark-mode") {
|
2023-04-11 19:42:46 -05:00
|
|
|
Some(c) => {
|
|
|
|
if c.value() == "true" {
|
|
|
|
true
|
|
|
|
} else if c.value() == "false" {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2023-04-10 20:37:40 -05:00
|
|
|
None => false,
|
|
|
|
};
|
2023-04-11 19:42:46 -05:00
|
|
|
let mut vdom = VirtualDom::new_with_props(
|
|
|
|
void_app::PoemListPage,
|
|
|
|
DarkModeProps {
|
|
|
|
slug: Some(String::from("/poems")),
|
|
|
|
dark_mode,
|
|
|
|
},
|
|
|
|
);
|
2023-04-10 20:37:40 -05:00
|
|
|
let _ = vdom.rebuild();
|
|
|
|
let output = dioxus_ssr::render(&vdom);
|
|
|
|
Template::render(
|
|
|
|
"index",
|
|
|
|
context! {
|
|
|
|
app_title: "A Letter to the Void",
|
|
|
|
style_include: "<link href=/styles/tailwind.min.css rel=stylesheet />",
|
|
|
|
test: &output,
|
|
|
|
dark_mode: match dark_mode {
|
|
|
|
true => "dark",
|
|
|
|
false => ""
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-04-11 19:42:46 -05:00
|
|
|
#[get("/<entry>")]
|
2023-04-10 18:19:02 -05:00
|
|
|
async fn poem(cookies: &CookieJar<'_>, entry: &str) -> Template {
|
|
|
|
let dark_mode = match cookies.get("dark-mode") {
|
2023-04-11 19:42:46 -05:00
|
|
|
Some(c) => {
|
|
|
|
if c.value() == "true" {
|
|
|
|
true
|
|
|
|
} else if c.value() == "false" {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2023-04-10 18:19:02 -05:00
|
|
|
None => false,
|
|
|
|
};
|
2023-04-09 12:34:40 -05:00
|
|
|
let mut vdom = VirtualDom::new_with_props(
|
|
|
|
void_app::PoemPage,
|
|
|
|
PoemRequest {
|
2023-04-12 00:42:59 -05:00
|
|
|
slug: format!("{entry}"),
|
2023-04-10 18:19:02 -05:00
|
|
|
dark_mode: Some(dark_mode),
|
2023-04-09 12:34:40 -05:00
|
|
|
},
|
|
|
|
);
|
|
|
|
let _ = vdom.rebuild();
|
|
|
|
let output = dioxus_ssr::render(&vdom);
|
|
|
|
Template::render(
|
|
|
|
"index",
|
|
|
|
context! {
|
|
|
|
app_title: "A Letter to the Void",
|
|
|
|
style_include: "<link href=/styles/tailwind.min.css rel=stylesheet />",
|
2023-04-10 18:19:02 -05:00
|
|
|
test: &output,
|
|
|
|
dark_mode: match dark_mode {
|
|
|
|
true => "dark",
|
|
|
|
false => ""
|
|
|
|
},
|
2023-04-09 12:34:40 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-04-08 12:40:57 -05:00
|
|
|
/// This runs `rocket::build()` with the needed mounts and routes.
|
|
|
|
pub async fn build_rocket() -> Rocket<Build> {
|
|
|
|
rocket::build()
|
|
|
|
.mount("/images", FileServer::from("public/images"))
|
|
|
|
.mount("/styles", FileServer::from("public/styles"))
|
|
|
|
.mount("/fonts", FileServer::from("public/fonts"))
|
2023-04-12 00:42:59 -05:00
|
|
|
.mount("/poems", routes![poem_list, poem])
|
|
|
|
.mount("/", routes![dark_mode, index])
|
2023-04-09 12:34:40 -05:00
|
|
|
.attach(Template::fairing())
|
2023-04-07 07:41:48 -05:00
|
|
|
}
|
|
|
|
}
|