beginning of change to new config for website
This commit is contained in:
parent
5f281e587b
commit
10e4986ef2
@ -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<String>, // default is freechat.io - clean url of domain
|
||||
favicon: Option<String>, // uri path to the favicon i.e. /media/static/favicon.png
|
||||
motto: Option<String>,
|
||||
|
||||
opengraph: Option<OpenGraph>
|
||||
}
|
||||
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct OpenGraph {
|
||||
title: Option<String>,
|
||||
og_type: Option<String>, // prefixing this because type is a keyword in rust smh
|
||||
url: Option<String>,
|
||||
description: Option<String>,
|
||||
image: Option<String>,
|
||||
|
||||
og_type: Option<String>,
|
||||
og_description: 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() {
|
||||
// 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,40 +47,24 @@ 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);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
pub fn homepage() -> Template {
|
||||
|
Loading…
Reference in New Issue
Block a user