ready to ready toml in ./configs/ for website at least

This commit is contained in:
shockrah 2020-01-04 16:25:18 -08:00
parent 6c88b04d6a
commit a142ea435b
2 changed files with 75 additions and 35 deletions

View File

@ -30,6 +30,7 @@ fn rocket() -> rocket::Rocket {
} }
fn main() { fn main() {
website::init();
rocket().launch(); rocket().launch();
} }

View File

@ -3,59 +3,98 @@
use rocket_contrib::templates::Template; use rocket_contrib::templates::Template;
use rocket::response::NamedFile; use rocket::response::NamedFile;
use std::path::Path; use std::path::Path;
use std::fs::File;
use serde_derive::Deserialize;
// Purely for backend purposes // Purely for backend purposes
#[derive(Serialize)] #[derive(Serialize, Deserialize, Debug)]
struct PageAttrs { pub struct WebsiteConfig {
og_title: &'static str, url: Option<String>, // default is freechat.io - clean url of domain
og_type: &'static str, favicon: Option<String>, // uri path to the favicon i.e. /media/static/favicon.png
og_desc: &'static str, motto: Option<String>,
og_url: &'static str,
og_image: &'static str,
// General settings opengraph: Option<OpenGraph>
favicon: &'static str, // path to the favicon
// Branding things
brand_url: &'static str, // default is freechat.io - clean url of domain
brand_motto: &'static str,
brand_quip: &'static str
} }
fn context() -> PageAttrs { #[derive(Serialize, Deserialize, Debug)]
PageAttrs { struct OpenGraph {
og_title: "Freechat", title: Option<String>,
og_type: "Decentralized Chat", og_type: Option<String>, // prefixing this because type is a keyword in rust smh
og_desc: "Privacy and Freedom Respecting Chat Platform", url: Option<String>,
og_url: "freechat.io", description: Option<String>,
og_image: "https://images.pexels.com/photos/146071/pexels-photo-146071.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260",// recall this is meant to be a path to the favicon image: Option<String>,
}
favicon: "/static/media/favicon.png", pub static mut CTX: WebsiteConfig = WebsiteConfig {
brand_url: "freechat.io", url: None,
brand_motto: "Freedom respecting chat platform", favicon: None,
brand_quip: "Join an instance", motto: None,
opengraph: None,
};
// Pages themselves
pub fn init() {
// Sets up ctx so before the rest of the dispatchers are called
let default_path = "configs/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
# 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"
"#;
if let Ok(mut cfg_file) = File::open(default_path) {
// here we take the file and parse it as toml
unsafe {
println!("config found ok{:?}", &CTX);
}
}
else {
let config: WebsiteConfig = toml::from_str(default_config).unwrap();
unsafe {
CTX.url = config.url;
CTX.favicon = config.favicon;
CTX.motto = config.motto;
}
} }
} }
// Pages themselves
#[get("/")] #[get("/")]
pub fn homepage() -> Template { pub fn homepage() -> Template {
let ctx = context(); unsafe {
Template::render("index", &ctx) Template::render("index", &CTX)
}
} }
#[get("/about")] #[get("/about")]
pub fn about_page() -> Template { pub fn about_page() -> Template {
let ctx = context(); unsafe {
Template::render("about", &ctx) Template::render("about", &CTX)
}
} }
#[get("/servers")] #[get("/servers")]
pub fn server_info() -> Template { pub fn server_info() -> Template {
let ctx = context(); unsafe {
Template::render("servers", &ctx) Template::render("servers", &CTX)
}
} }
#[get("/static/css/<file>")] #[get("/static/css/<file>")]