From 10e4986ef2e9f72d080e05f2551c89332e7ed658 Mon Sep 17 00:00:00 2001 From: shockrah Date: Wed, 29 Jan 2020 16:41:46 -0800 Subject: [PATCH] beginning of change to new config for website --- server/src/website.rs | 62 ++++++++++++------------------------------- 1 file changed, 17 insertions(+), 45 deletions(-) diff --git a/server/src/website.rs b/server/src/website.rs index c8168c5..469e2ba 100644 --- a/server/src/website.rs +++ b/server/src/website.rs @@ -2,6 +2,7 @@ // All new servers come with this as a default so that use rocket_contrib::templates::Template; use rocket::response::NamedFile; +use std::env; use std::path::Path; use std::fs::read_to_string; use serde_derive::Deserialize; @@ -12,32 +13,19 @@ 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, - - opengraph: Option -} - - -#[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, + + og_type: Option, + og_description: Option, + og_image: Option, } -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 + // Website config only read/written once ever on init let default_path = "configs/website.toml"; + // Default config will set all the env vars for us and later we override things + // from the user config in config/website.toml let default_config = r#" url="freechat.io" favicon="/static/media/favicon.png" @@ -59,38 +47,22 @@ pub fn init() { description="Free and open chat platform" image="/static/media/logo.png" "#; + let config: WebsiteConfig = toml::from_str(default_config).unwrap(); + env::set_var("web_url", config.url); + env::set_var("web_favicon", config.favicon); + env::set_var("web_motto", config.motto); + // TODO: when we read from the file we should elegantly say there are errors in the config // instead of going through the basic panic if let Ok(file) = read_to_string(default_path) { - // This is the only time we should ever write to CTX thus the unsafe is mostly fine let config: WebsiteConfig = toml::from_str(&file).unwrap(); println!("file contents: {:?}", config); - unsafe { - CTX.url = config.url; - CTX.favicon = config.favicon; - CTX.motto = config.motto; - if let Some(og) = config.opengraph { - CTX.opengraph = Some(OpenGraph { - title: config.opengraph.title, - config.opengraph_type: config.opengraph.config.opengraph_type, - url: config.opengraph.url, - description: config.opengraph.description, - image: config.opengraph.image, - }); - } - } - } - else { - // No check because default_config is hardcoded into place - let config: WebsiteConfig = toml::from_str(default_config).unwrap(); - unsafe { - CTX.url = config.url; - CTX.favicon = config.favicon; - CTX.motto = config.motto; - - } + env::set_var("web_url", config.url); + env::set_var("web_favicon", config.favicon); + env::set_var("web_motto", config.motto); + } }