its ugly but way more safe and uses the config properly

This commit is contained in:
shockrah 2020-01-29 20:16:50 -08:00
parent 955331323b
commit 5dcc6b2096

View File

@ -14,77 +14,70 @@ pub struct WebsiteConfig {
favicon: Option<String>, // uri path to the favicon i.e. /media/static/favicon.png
motto: Option<String>,
title: Option<String>,
description: Option<String>,
og_type: Option<String>,
og_description: Option<String>,
og_image: Option<String>,
stylesheet: String,
}
pub fn init() {
// 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"
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
// Default values which are later overriden
env::set_var("web_url", "freechat.io");
env::set_var("web_favicon", "/static/images/favicon.png");
env::set_var("web_motto", "Decentralized free chat platform");
env::set_var("web_title", "Freechat");
env::set_var("web_og_type", "Decentralized Chat Platform");
# 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"
"#;
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) {
// If there's a problem we're going to panic and that's fine to be completely honest
if let Ok(file) = read_to_string("configs/website.toml") {
let config: WebsiteConfig = toml::from_str(&file).unwrap();
println!("file contents: {:?}", config);
env::set_var("web_url", config.url);
env::set_var("web_favicon", config.favicon);
env::set_var("web_motto", config.motto);
env::set_var("web_url", config.url.unwrap());
env::set_var("web_favicon", config.favicon.unwrap());
env::set_var("web_motto", config.motto.unwrap());
env::set_var("web_title", config.title.unwrap());
env::set_var("web_og_type", config.og_type.unwrap());
}
}
#[get("/")]
pub fn homepage() -> Template {
unsafe {
Template::render("index", &CTX)
}
// look away
Template::render("index", WebsiteConfig {
url: Some(env::var("web_url").unwrap()),
favicon: Some(env::var("web_favicon").unwrap()),
motto: Some(env::var("web_motto").unwrap()),
title: Some(env::var("web_title").unwrap()),
description: Some(env::var("web_description").unwrap()),
og_type: Some(env::var("web_og_type").unwrap()),
stylesheet: "/static/css/index.css".to_string()
})
}
#[get("/about")]
pub fn about_page() -> Template {
unsafe {
Template::render("about", &CTX)
}
Template::render("about", WebsiteConfig {
url: Some(env::var("web_url").unwrap()),
favicon: Some(env::var("web_favicon").unwrap()),
motto: Some(env::var("web_motto").unwrap()),
title: Some(env::var("web_title").unwrap()),
description: Some(env::var("web_description").unwrap()),
og_type: Some(env::var("web_og_type").unwrap()),
stylesheet: "/static/css/about.css".to_string()
})
}
#[get("/servers")]
pub fn server_info() -> Template {
unsafe {
Template::render("servers", &CTX)
}
Template::render("servers", WebsiteConfig {
url: Some(env::var("web_url").unwrap()),
favicon: Some(env::var("web_favicon").unwrap()),
motto: Some(env::var("web_motto").unwrap()),
title: Some(env::var("web_title").unwrap()),
description: Some(env::var("web_description").unwrap()),
og_type: Some(env::var("web_og_type").unwrap()),
stylesheet: "/static/css/about.css".to_string()
})
}
#[get("/static/css/<file>")]