beginning of change to new config for website

This commit is contained in:
shockrah 2020-01-29 16:41:46 -08:00
parent 5f281e587b
commit 10e4986ef2

View File

@ -2,6 +2,7 @@
// All new servers come with this as a default so that // All new servers come with this as a default so that
use rocket_contrib::templates::Template; use rocket_contrib::templates::Template;
use rocket::response::NamedFile; use rocket::response::NamedFile;
use std::env;
use std::path::Path; use std::path::Path;
use std::fs::read_to_string; use std::fs::read_to_string;
use serde_derive::Deserialize; use serde_derive::Deserialize;
@ -12,32 +13,19 @@ pub struct WebsiteConfig {
url: Option<String>, // default is freechat.io - clean url of domain url: Option<String>, // default is freechat.io - clean url of domain
favicon: Option<String>, // uri path to the favicon i.e. /media/static/favicon.png favicon: Option<String>, // uri path to the favicon i.e. /media/static/favicon.png
motto: Option<String>, motto: Option<String>,
opengraph: Option<OpenGraph>
}
#[derive(Serialize, Deserialize, Debug)]
struct OpenGraph {
title: Option<String>, title: Option<String>,
og_type: Option<String>, // prefixing this because type is a keyword in rust smh
url: Option<String>, og_type: Option<String>,
description: Option<String>, og_description: Option<String>,
image: Option<String>, og_image: Option<String>,
} }
pub static mut CTX: WebsiteConfig = WebsiteConfig {
url: None,
favicon: None,
motto: None,
opengraph: None,
};
// Pages themselves
pub fn init() { 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"; 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#" let default_config = r#"
url="freechat.io" url="freechat.io"
favicon="/static/media/favicon.png" favicon="/static/media/favicon.png"
@ -59,39 +47,23 @@ pub fn init() {
description="Free and open chat platform" description="Free and open chat platform"
image="/static/media/logo.png" 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 // TODO: when we read from the file we should elegantly say there are errors in the config
// instead of going through the basic panic // instead of going through the basic panic
if let Ok(file) = read_to_string(default_path) { 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(); let config: WebsiteConfig = toml::from_str(&file).unwrap();
println!("file contents: {:?}", config); println!("file contents: {:?}", config);
unsafe {
CTX.url = config.url;
CTX.favicon = config.favicon;
CTX.motto = config.motto;
if let Some(og) = config.opengraph { env::set_var("web_url", config.url);
CTX.opengraph = Some(OpenGraph { env::set_var("web_favicon", config.favicon);
title: config.opengraph.title, env::set_var("web_motto", config.motto);
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;
} }
}
} }
#[get("/")] #[get("/")]