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 {
|
|
|
|
|
2023-04-18 19:33:44 -05:00
|
|
|
use once_cell::sync::OnceCell;
|
2023-04-08 12:40:57 -05:00
|
|
|
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};
|
2024-03-15 15:53:40 -05:00
|
|
|
use void_fe::utils::prop_structs::{PoemDatabase, VoidProps, ContentProps};
|
2023-04-18 19:33:44 -05:00
|
|
|
use void_fe::utils::user_prefs::*;
|
2023-04-13 12:51:25 -05:00
|
|
|
use void_fe::void_app::{self, VirtualDom};
|
2024-03-15 15:53:40 -05:00
|
|
|
pub use void_fe::components::void_content::RenderContent;
|
|
|
|
use void_fe::utils::helpers::get_homepage_paragraph;
|
2023-04-08 12:40:57 -05:00
|
|
|
|
2023-04-18 19:33:44 -05:00
|
|
|
static POEM_DATABASE: OnceCell<PoemDatabase> = OnceCell::new();
|
2023-04-16 21:46:10 -05:00
|
|
|
|
|
|
|
async fn get_user_prefs(cookies: &CookieJar<'_>) -> UserPrefs {
|
|
|
|
let user_theme = match cookies.get("theme") {
|
|
|
|
Some(c) => match c.value() {
|
|
|
|
"auto" => ThemePref::Auto,
|
|
|
|
"light" => ThemePref::Light,
|
|
|
|
"dark" => ThemePref::Dark,
|
|
|
|
_ => {
|
2024-03-15 15:53:40 -05:00
|
|
|
cookies.remove("theme");
|
2023-04-16 21:46:10 -05:00
|
|
|
cookies.add(Cookie::new("theme", "auto"));
|
|
|
|
ThemePref::Auto
|
2023-04-11 19:42:46 -05:00
|
|
|
}
|
2023-04-16 21:46:10 -05:00
|
|
|
},
|
|
|
|
None => {
|
|
|
|
cookies.add(Cookie::new("theme", "auto"));
|
|
|
|
ThemePref::Auto
|
2023-04-11 19:42:46 -05:00
|
|
|
}
|
2023-04-10 18:19:02 -05:00
|
|
|
};
|
2023-04-16 21:46:10 -05:00
|
|
|
let user_font = match cookies.get("font") {
|
|
|
|
Some(c) => match c.value() {
|
|
|
|
"nerd" => FontPref::NerdFont,
|
|
|
|
"open" => FontPref::OpenDyslexic,
|
|
|
|
_ => {
|
2024-03-15 15:53:40 -05:00
|
|
|
cookies.remove("font");
|
2023-04-16 21:46:10 -05:00
|
|
|
cookies.add(Cookie::new("font", "open"));
|
|
|
|
FontPref::OpenDyslexic
|
|
|
|
}
|
2023-04-11 19:42:46 -05:00
|
|
|
},
|
2023-04-16 21:46:10 -05:00
|
|
|
None => {
|
|
|
|
cookies.add(Cookie::new("font", "open"));
|
|
|
|
FontPref::OpenDyslexic
|
|
|
|
}
|
|
|
|
};
|
|
|
|
UserPrefs::new(user_theme, user_font)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn set_user_theme(cookies: &CookieJar<'_>, theme: &str) {
|
|
|
|
if theme == "light" || theme == "dark" || theme == "auto" {
|
2024-03-15 15:53:40 -05:00
|
|
|
cookies.remove("theme");
|
2023-04-16 21:46:10 -05:00
|
|
|
cookies.add(Cookie::new("theme", format!("{theme}")));
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn set_user_font(cookies: &CookieJar<'_>, font: &str) {
|
|
|
|
if font == "nerd" || font == "open" {
|
2024-03-15 15:53:40 -05:00
|
|
|
cookies.remove("font");
|
2023-04-16 21:46:10 -05:00
|
|
|
cookies.add(Cookie::new("font", format!("{font}")));
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/")]
|
|
|
|
async fn index(cookies: &CookieJar<'_>) -> Template {
|
|
|
|
let user_prefs = get_user_prefs(cookies).await;
|
2023-04-18 19:33:44 -05:00
|
|
|
let void_props = VoidProps {
|
|
|
|
slug: None,
|
|
|
|
poem_database: POEM_DATABASE
|
|
|
|
.get()
|
|
|
|
.expect("Poem database is not initialized")
|
|
|
|
.clone(),
|
|
|
|
user_prefs: user_prefs,
|
|
|
|
};
|
2024-03-15 15:53:40 -05:00
|
|
|
// let content_props = ContentProps {
|
|
|
|
// content: get_homepage_paragraph(),
|
|
|
|
// user_prefs: user_prefs
|
|
|
|
// };
|
|
|
|
// let mut vdom = VirtualDom::new_with_props(void_app::HomePage, void_props);
|
|
|
|
let mut vdom = VirtualDom::new_with_props(void_app::HomePage, void_fe::void_app::HomePageProps { props: void_props });
|
|
|
|
let _ = vdom.rebuild_in_place();
|
2023-04-08 12:40:57 -05:00
|
|
|
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,
|
2023-04-16 21:46:10 -05:00
|
|
|
dark_mode: "",
|
2023-04-08 12:40:57 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2023-04-07 07:41:48 -05:00
|
|
|
|
2023-04-16 21:46:10 -05:00
|
|
|
#[get("/", rank = 3)]
|
|
|
|
async fn settings(cookies: &CookieJar<'_>) -> Template {
|
|
|
|
let user_prefs = get_user_prefs(cookies).await;
|
|
|
|
let mut vdom = VirtualDom::new_with_props(void_app::SettingsPage, user_prefs);
|
2024-03-15 15:53:40 -05:00
|
|
|
let _ = vdom.rebuild_in_place();
|
2023-04-16 21:46:10 -05:00
|
|
|
let output = dioxus_ssr::render(&vdom);
|
|
|
|
Template::render(
|
|
|
|
"index",
|
|
|
|
context! {
|
|
|
|
app_title: "Settings",
|
|
|
|
style_include: "<link href=/styles/tailwind.min.css rel=stylesheet />",
|
|
|
|
test: &output,
|
|
|
|
dark_mode: ""
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/?<theme>", rank = 2)]
|
|
|
|
async fn theme(cookies: &CookieJar<'_>, theme: &str) -> Redirect {
|
|
|
|
set_user_theme(cookies, theme).await;
|
|
|
|
Redirect::to("/settings")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/?<font>")]
|
|
|
|
async fn font(cookies: &CookieJar<'_>, font: &str) -> Redirect {
|
|
|
|
set_user_font(cookies, font).await;
|
|
|
|
Redirect::to("/settings")
|
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 {
|
2023-04-16 21:46:10 -05:00
|
|
|
let user_prefs = get_user_prefs(cookies).await;
|
2023-04-18 19:33:44 -05:00
|
|
|
let void_props = VoidProps {
|
|
|
|
slug: None,
|
|
|
|
poem_database: POEM_DATABASE
|
|
|
|
.get()
|
|
|
|
.expect("Poem database is not initialized")
|
|
|
|
.clone(),
|
|
|
|
user_prefs: user_prefs,
|
|
|
|
};
|
|
|
|
let mut vdom = VirtualDom::new_with_props(void_app::PoemListPage, void_props);
|
2024-03-15 15:53:40 -05:00
|
|
|
let _ = vdom.rebuild_in_place();
|
2023-04-10 20:37:40 -05:00
|
|
|
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,
|
2023-04-16 21:46:10 -05:00
|
|
|
dark_mode: "",
|
2023-04-10 20:37:40 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-04-16 21:46:10 -05:00
|
|
|
let user_prefs = get_user_prefs(cookies).await;
|
2023-04-18 19:33:44 -05:00
|
|
|
let void_props = VoidProps {
|
|
|
|
slug: Some(entry.to_string()),
|
|
|
|
poem_database: POEM_DATABASE
|
|
|
|
.get()
|
|
|
|
.expect("Poem database is not initialized")
|
|
|
|
.clone(),
|
|
|
|
user_prefs: user_prefs,
|
|
|
|
};
|
|
|
|
let mut vdom = VirtualDom::new_with_props(void_app::PoemPage, void_props);
|
2024-03-15 15:53:40 -05:00
|
|
|
let _ = vdom.rebuild_in_place();
|
2023-04-09 12:34:40 -05:00
|
|
|
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,
|
2023-04-16 21:46:10 -05:00
|
|
|
dark_mode: "",
|
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> {
|
2023-04-18 19:33:44 -05:00
|
|
|
let mut poem_database = PoemDatabase::new();
|
|
|
|
poem_database.build_poem_database().await;
|
|
|
|
POEM_DATABASE.set(poem_database).expect("Could not initialize poem database.");
|
2023-04-08 12:40:57 -05:00
|
|
|
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])
|
2023-04-16 21:46:10 -05:00
|
|
|
.mount("/settings", routes![settings, theme, font])
|
|
|
|
.mount("/", routes![index])
|
2023-04-09 12:34:40 -05:00
|
|
|
.attach(Template::fairing())
|
2023-04-07 07:41:48 -05:00
|
|
|
}
|
|
|
|
}
|