freechat/server/src/main.rs
2019-12-05 11:15:42 -08:00

93 lines
2.7 KiB
Rust

// This client code really just serves as the url router for the main website where we describe what the project is about
#![feature(proc_macro_hygiene, decl_macro, plugin)]
//#[macro_use] extern crate serde_derive;
#[macro_use] extern crate serde_derive;
#[macro_use] extern crate rocket;
extern crate rocket_contrib;
use rocket_contrib::serve::StaticFiles;
use rocket_contrib::templates::Template;
mod website;
use website::*;
// 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,
// 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
}
fn rocket() -> rocket::Rocket {
rocket::ignite()
.mount("/static", StaticFiles::from("/static"))
.mount("/", routes![
homepage, about_page, server_info,
static_css, static_js, static_media
])
.attach(Template::fairing())
}
fn main() {
rocket().launch();
}
// Integrating some basic tests just for this module
#[cfg(test)]
mod test {
use super::rocket;
use rocket::local::Client;
use rocket::http::Status;
macro_rules! check_get {
($client:expr, $file:expr) => {
let response = $client.get($file).dispatch();
assert_eq!(response.status(), Status::Ok);
};
($client:expr, $file:expr, $code:expr) => {
let response = $client.get($file).dispatch();
assert_eq!(response.status(), $code);
};
}
#[test]
fn pageroutes_get() {
// Just make sure that when request the home page we actually get the html back
let client = Client::new(rocket()).expect("Valid rocket instance");
check_get!(client, "/");
check_get!(client, "/about");
}
// Next we test the static resource routes
#[test]
fn static_css_get() {
let client = Client::new(rocket()).expect("Valid rocket instance");
check_get!(client, "/static/css/index.css");
check_get!(client, "/static/css/about.css");
}
// this one is meant to fail for now because we don't have any js to serve
#[test]
fn static_media_get() {
let client = Client::new(rocket()).expect("Valid rocket instance");
check_get!(client, "/static/media/favicon.png");
}
#[test]
fn static_js_get() {
let client = Client::new(rocket()).expect("Valid rocket instance");
check_get!(client, "/static/js/jquery.js", Status::NotFound);
}
}