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,7 +30,8 @@ fn rocket() -> rocket::Rocket {
}
fn main() {
rocket().launch();
website::init();
rocket().launch();
}
// Integrating some basic tests just for this module

View File

@ -3,59 +3,98 @@
use rocket_contrib::templates::Template;
use rocket::response::NamedFile;
use std::path::Path;
use std::fs::File;
use serde_derive::Deserialize;
// Purely for backend purposes
#[derive(Serialize)]
struct PageAttrs {
og_title: &'static str,
og_type: &'static str,
og_desc: &'static str,
og_url: &'static str,
og_image: &'static str,
#[derive(Serialize, Deserialize, Debug)]
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>,
// General settings
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
opengraph: Option<OpenGraph>
}
fn context() -> PageAttrs {
PageAttrs {
og_title: "Freechat",
og_type: "Decentralized Chat",
og_desc: "Privacy and Freedom Respecting Chat Platform",
og_url: "freechat.io",
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
#[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>,
}
favicon: "/static/media/favicon.png",
brand_url: "freechat.io",
brand_motto: "Freedom respecting chat platform",
brand_quip: "Join an instance",
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
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("/")]
pub fn homepage() -> Template {
let ctx = context();
Template::render("index", &ctx)
unsafe {
Template::render("index", &CTX)
}
}
#[get("/about")]
pub fn about_page() -> Template {
let ctx = context();
Template::render("about", &ctx)
unsafe {
Template::render("about", &CTX)
}
}
#[get("/servers")]
pub fn server_info() -> Template {
let ctx = context();
Template::render("servers", &ctx)
unsafe {
Template::render("servers", &CTX)
}
}
#[get("/static/css/<file>")]