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 favicon: Option<String>, // uri path to the favicon i.e. /media/static/favicon.png
motto: Option<String>, motto: Option<String>,
title: Option<String>, title: Option<String>,
description: Option<String>,
og_type: Option<String>, og_type: Option<String>,
og_description: Option<String>, stylesheet: String,
og_image: Option<String>,
} }
pub fn init() { pub fn init() {
// Website config only read/written once ever on init // Default values which are later overriden
let default_path = "configs/website.toml"; env::set_var("web_url", "freechat.io");
// Default config will set all the env vars for us and later we override things env::set_var("web_favicon", "/static/images/favicon.png");
// from the user config in config/website.toml env::set_var("web_motto", "Decentralized free chat platform");
let default_config = r#" env::set_var("web_title", "Freechat");
url="freechat.io" env::set_var("web_og_type", "Decentralized Chat Platform");
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 // If there's a problem we're going to panic and that's fine to be completely honest
# Especially when the server's site is linked in another freechat/slack/discord server if let Ok(file) = read_to_string("configs/website.toml") {
[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) {
let config: WebsiteConfig = toml::from_str(&file).unwrap(); let config: WebsiteConfig = toml::from_str(&file).unwrap();
println!("file contents: {:?}", config); println!("file contents: {:?}", config);
env::set_var("web_url", config.url.unwrap());
env::set_var("web_url", config.url); env::set_var("web_favicon", config.favicon.unwrap());
env::set_var("web_favicon", config.favicon); env::set_var("web_motto", config.motto.unwrap());
env::set_var("web_motto", config.motto); env::set_var("web_title", config.title.unwrap());
env::set_var("web_og_type", config.og_type.unwrap());
} }
} }
#[get("/")] #[get("/")]
pub fn homepage() -> Template { pub fn homepage() -> Template {
unsafe { // look away
Template::render("index", &CTX) 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")] #[get("/about")]
pub fn about_page() -> Template { pub fn about_page() -> Template {
unsafe { Template::render("about", WebsiteConfig {
Template::render("about", &CTX) 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")] #[get("/servers")]
pub fn server_info() -> Template { pub fn server_info() -> Template {
unsafe { Template::render("servers", WebsiteConfig {
Template::render("servers", &CTX) 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>")] #[get("/static/css/<file>")]