2020-07-09 18:19:03 -04:00
|
|
|
use handlebars::Handlebars;
|
2020-05-15 09:16:16 -04:00
|
|
|
use pfacts::Facts;
|
2020-05-13 16:38:16 -04:00
|
|
|
use rand::prelude::*;
|
2020-07-09 18:19:03 -04:00
|
|
|
use serde::Serialize;
|
|
|
|
use std::{convert::Infallible, sync::Arc};
|
2020-05-13 16:38:16 -04:00
|
|
|
use warp::Filter;
|
|
|
|
|
2020-05-15 09:16:16 -04:00
|
|
|
async fn give_fact(facts: Facts) -> Result<String, Infallible> {
|
2020-05-13 16:38:16 -04:00
|
|
|
Ok(facts.choose(&mut thread_rng()).unwrap().clone())
|
|
|
|
}
|
|
|
|
|
2020-07-09 18:19:03 -04:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct TemplateContext {
|
|
|
|
title: &'static str,
|
|
|
|
fact: Option<String>,
|
|
|
|
// This key tells handlebars which template is the parent.
|
|
|
|
parent: &'static str,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct WithTemplate<T: Serialize> {
|
|
|
|
name: &'static str,
|
|
|
|
value: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render<T>(template: WithTemplate<T>, hbs: Arc<Handlebars>) -> impl warp::Reply
|
|
|
|
where
|
|
|
|
T: Serialize,
|
|
|
|
{
|
|
|
|
let render = hbs
|
|
|
|
.render(template.name, &template.value)
|
|
|
|
.unwrap_or_else(|err| err.to_string());
|
|
|
|
warp::reply::html(render)
|
|
|
|
}
|
|
|
|
|
2020-05-13 16:38:16 -04:00
|
|
|
#[tokio::main]
|
2020-07-09 18:19:03 -04:00
|
|
|
async fn main() -> anyhow::Result<()> {
|
2020-07-09 17:07:01 -04:00
|
|
|
pretty_env_logger::init();
|
2020-05-15 09:16:16 -04:00
|
|
|
let facts = pfacts::make();
|
2020-07-09 18:19:03 -04:00
|
|
|
let mut hb = Handlebars::new();
|
|
|
|
|
|
|
|
hb.register_template_file("layout", "./templates/layout.hbs")?;
|
|
|
|
hb.register_template_file("footer", "./templates/footer.hbs")?;
|
|
|
|
hb.register_template_file("index", "./templates/index.hbs")?;
|
|
|
|
hb.register_template_file("error/404", "./templates/error/404.hbs")?;
|
|
|
|
|
|
|
|
let fact = {
|
|
|
|
let facts = facts.clone();
|
|
|
|
warp::any().map(move || facts.clone())
|
|
|
|
};
|
|
|
|
let hb = Arc::new(hb);
|
|
|
|
let handlebars = move |with_template| render(with_template, hb.clone());
|
2020-05-13 16:38:16 -04:00
|
|
|
|
2020-07-09 18:19:03 -04:00
|
|
|
let files = warp::path("static").and(warp::fs::dir("./static"));
|
2020-05-13 16:38:16 -04:00
|
|
|
|
2020-07-09 18:19:03 -04:00
|
|
|
let fact_handler = warp::get()
|
|
|
|
.and(warp::path("fact"))
|
|
|
|
.and(fact)
|
2020-05-13 16:38:16 -04:00
|
|
|
.and_then(give_fact);
|
|
|
|
|
2020-07-09 18:19:03 -04:00
|
|
|
let index_handler = warp::get()
|
|
|
|
.and(warp::path::end())
|
|
|
|
.map(move || WithTemplate {
|
|
|
|
name: "index",
|
|
|
|
value: TemplateContext {
|
|
|
|
title: "Printer Facts",
|
|
|
|
fact: {
|
|
|
|
let ref facts = facts.clone();
|
|
|
|
Some(facts.choose(&mut thread_rng()).unwrap().clone())
|
|
|
|
},
|
|
|
|
parent: "layout",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.map(handlebars.clone());
|
|
|
|
|
|
|
|
let not_found_handler = warp::any()
|
|
|
|
.map(move || WithTemplate {
|
|
|
|
name: "error/404",
|
|
|
|
value: TemplateContext {
|
|
|
|
title: "Not Found",
|
|
|
|
fact: None,
|
|
|
|
parent: "layout",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.map(handlebars.clone());
|
|
|
|
|
2020-07-09 17:07:01 -04:00
|
|
|
log::info!("listening on port 5000");
|
2020-07-09 18:19:03 -04:00
|
|
|
warp::serve(
|
|
|
|
fact_handler
|
|
|
|
.or(index_handler)
|
|
|
|
.or(files)
|
|
|
|
.or(not_found_handler),
|
|
|
|
)
|
|
|
|
.run(([0, 0, 0, 0], 5000))
|
|
|
|
.await;
|
|
|
|
Ok(())
|
2020-05-13 16:38:16 -04:00
|
|
|
}
|