From a142ea435bc994d0b4dbe8200d24b5b8b321046d Mon Sep 17 00:00:00 2001 From: shockrah Date: Sat, 4 Jan 2020 16:25:18 -0800 Subject: [PATCH] ready to ready toml in ./configs/ for website at least --- server/src/main.rs | 3 +- server/src/website.rs | 107 ++++++++++++++++++++++++++++-------------- 2 files changed, 75 insertions(+), 35 deletions(-) diff --git a/server/src/main.rs b/server/src/main.rs index d51aa26..8ba5873 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -30,7 +30,8 @@ fn rocket() -> rocket::Rocket { } fn main() { - rocket().launch(); + website::init(); + rocket().launch(); } // Integrating some basic tests just for this module diff --git a/server/src/website.rs b/server/src/website.rs index 929f165..37c76fb 100644 --- a/server/src/website.rs +++ b/server/src/website.rs @@ -3,59 +3,98 @@ use rocket_contrib::templates::Template; use rocket::response::NamedFile; use std::path::Path; +use std::fs::File; +use serde_derive::Deserialize; // Purely for backend purposes -#[derive(Serialize)] -struct PageAttrs { - og_title: &'static str, - og_type: &'static str, - og_desc: &'static str, - og_url: &'static str, - og_image: &'static str, +#[derive(Serialize, Deserialize, Debug)] +pub struct WebsiteConfig { + url: Option, // default is freechat.io - clean url of domain + favicon: Option, // uri path to the favicon i.e. /media/static/favicon.png + motto: Option, - // General settings - favicon: &'static str, // path to the favicon - - // Branding things - brand_url: &'static str, // default is freechat.io - clean url of domain - brand_motto: &'static str, - brand_quip: &'static str + opengraph: Option } -fn context() -> PageAttrs { - PageAttrs { - og_title: "Freechat", - og_type: "Decentralized Chat", - og_desc: "Privacy and Freedom Respecting Chat Platform", - og_url: "freechat.io", - og_image: "https://images.pexels.com/photos/146071/pexels-photo-146071.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260",// recall this is meant to be a path to the favicon +#[derive(Serialize, Deserialize, Debug)] +struct OpenGraph { + title: Option, + og_type: Option, // prefixing this because type is a keyword in rust smh + url: Option, + description: Option, + image: Option, +} - favicon: "/static/media/favicon.png", - brand_url: "freechat.io", - brand_motto: "Freedom respecting chat platform", - brand_quip: "Join an instance", +pub static mut CTX: WebsiteConfig = WebsiteConfig { + url: None, + favicon: None, + motto: None, + + opengraph: None, +}; +// Pages themselves + +pub fn init() { + // Sets up ctx so before the rest of the dispatchers are called + let default_path = "configs/website.toml"; + let default_config = r#" + url="freechat.io" + favicon="/static/media/favicon.png" + motto="Freely chat with freechat!" + + # NOTE: discord/slack/linked(especially) love caching things so make sure you + # write something that yo're proud of the first time lest u doom yourself to have the + # wrong things cahced for a few months + + + # Used to help sites embed some information about your site + # Especially when the server's site is linked in another freechat/slack/discord server + + [opengraph] + title="Freechat" + # again this has a prefix because `type` is a keyword in rust so just yea + og_type="Decentralized chat" + url="freechat.io" + description="Free and open chat platform" + image="/static/media/logo.png" + "#; + + if let Ok(mut cfg_file) = File::open(default_path) { + // here we take the file and parse it as toml + unsafe { + println!("config found ok{:?}", &CTX); + } + } + else { + let config: WebsiteConfig = toml::from_str(default_config).unwrap(); + unsafe { + CTX.url = config.url; + CTX.favicon = config.favicon; + CTX.motto = config.motto; + } } } -// Pages themselves - #[get("/")] pub fn homepage() -> Template { - let ctx = context(); - Template::render("index", &ctx) + unsafe { + Template::render("index", &CTX) + } } #[get("/about")] pub fn about_page() -> Template { - let ctx = context(); - Template::render("about", &ctx) + unsafe { + Template::render("about", &CTX) + } } #[get("/servers")] pub fn server_info() -> Template { - let ctx = context(); - Template::render("servers", &ctx) + unsafe { + Template::render("servers", &CTX) + } } #[get("/static/css/")] @@ -81,4 +120,4 @@ pub fn static_media(file: String) -> Option { f = f.replace("..", ""); f = f.replace("%2e", ""); NamedFile::open(Path::new("static/media/").join(f)).ok() -} \ No newline at end of file +}