use ructe
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Cadey Ratio 2020-07-16 16:10:45 -04:00
parent 6d152cde84
commit 2f8debe859
13 changed files with 269 additions and 156 deletions

5
CHANGELOG.md Normal file
View File

@ -0,0 +1,5 @@
# Changelog
## 0.2.0
This site uses `ructe` instead of handlebars.

253
Cargo.lock generated
View File

@ -1,5 +1,11 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]]
name = "adler32"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567b077b825e468cc974f0020d4082ee6e03132512f207ef1a02fd5d00d1f32d"
[[package]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "0.7.13" version = "0.7.13"
@ -15,6 +21,12 @@ version = "1.0.31"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85bb70cc08ec97ca5450e6eba421deeea5f172c0fc61f78b5357b2a8e8be195f" checksum = "85bb70cc08ec97ca5450e6eba421deeea5f172c0fc61f78b5357b2a8e8be195f"
[[package]]
name = "arrayvec"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8"
[[package]] [[package]]
name = "atty" name = "atty"
version = "0.2.14" version = "0.2.14"
@ -93,6 +105,12 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7"
[[package]]
name = "bytecount"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0017894339f586ccb943b01b9555de56770c11cda818e7e3d8bd93f4ed7f46e"
[[package]] [[package]]
name = "byteorder" name = "byteorder"
version = "1.3.4" version = "1.3.4"
@ -120,6 +138,15 @@ dependencies = [
"bitflags", "bitflags",
] ]
[[package]]
name = "crc32fast"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1"
dependencies = [
"cfg-if",
]
[[package]] [[package]]
name = "digest" name = "digest"
version = "0.8.1" version = "0.8.1"
@ -135,6 +162,12 @@ version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4358a9e11b9a09cf52383b451b49a169e8d797b68aa02301ff586d70d9661ea3" checksum = "4358a9e11b9a09cf52383b451b49a169e8d797b68aa02301ff586d70d9661ea3"
[[package]]
name = "either"
version = "1.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3"
[[package]] [[package]]
name = "env_logger" name = "env_logger"
version = "0.7.1" version = "0.7.1"
@ -316,20 +349,6 @@ dependencies = [
"tokio-util", "tokio-util",
] ]
[[package]]
name = "handlebars"
version = "3.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8ae96a0e0dacf151557ccba95a7a80889f8e74a784484377739628fcdb3996"
dependencies = [
"log 0.4.8",
"pest",
"pest_derive",
"quick-error",
"serde",
"serde_json",
]
[[package]] [[package]]
name = "headers" name = "headers"
version = "0.3.2" version = "0.3.2"
@ -364,6 +383,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "hex"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35"
[[package]] [[package]]
name = "http" name = "http"
version = "0.2.1" version = "0.2.1"
@ -462,6 +487,15 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "itertools"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "0.4.5" version = "0.4.5"
@ -484,12 +518,43 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lexical-core"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db65c6da02e61f55dae90a0ae427b2a5f6b3e8db09f58d10efab23af92592616"
dependencies = [
"arrayvec",
"bitflags",
"cfg-if",
"ryu",
"static_assertions",
]
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.70" version = "0.2.70"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3baa92041a6fec78c687fa0cc2b3fae8884f743d672cf551bed1d6dac6988d0f" checksum = "3baa92041a6fec78c687fa0cc2b3fae8884f743d672cf551bed1d6dac6988d0f"
[[package]]
name = "libflate"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9bac9023e1db29c084f9f8cd9d3852e5e8fddf98fb47c4964a0ea4663d95949"
dependencies = [
"adler32",
"crc32fast",
"libflate_lz77",
"rle-decode-fast",
]
[[package]]
name = "libflate_lz77"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3286f09f7d4926fc486334f28d8d2e6ebe4f7f9994494b6dab27ddfad2c9b11b"
[[package]] [[package]]
name = "log" name = "log"
version = "0.3.9" version = "0.3.9"
@ -508,18 +573,18 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "maplit"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d"
[[package]] [[package]]
name = "matches" name = "matches"
version = "0.1.8" version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
[[package]]
name = "md5"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771"
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.3.3" version = "2.3.3"
@ -623,6 +688,17 @@ dependencies = [
"winapi 0.3.8", "winapi 0.3.8",
] ]
[[package]]
name = "nom"
version = "5.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af"
dependencies = [
"lexical-core",
"memchr",
"version_check 0.9.1",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.3.1" version = "1.3.1"
@ -641,49 +717,6 @@ version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
[[package]]
name = "pest"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10f4872ae94d7b90ae48754df22fd42ad52ce740b8f370b03da4835417403e53"
dependencies = [
"ucd-trie",
]
[[package]]
name = "pest_derive"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "833d1ae558dc601e9a60366421196a8d94bc0ac980476d0b67e1d0988d72b2d0"
dependencies = [
"pest",
"pest_generator",
]
[[package]]
name = "pest_generator"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99b8db626e31e5b81787b9783425769681b347011cc59471e33ea46d2ea0cf55"
dependencies = [
"pest",
"pest_meta",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "pest_meta"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54be6e404f5317079812fc8f9f5279de376d8856929e21c184ecf6bbd692a11d"
dependencies = [
"maplit",
"pest",
"sha-1",
]
[[package]] [[package]]
name = "pfacts" name = "pfacts"
version = "0.1.0" version = "0.1.0"
@ -785,14 +818,17 @@ dependencies = [
[[package]] [[package]]
name = "printerfacts" name = "printerfacts"
version = "0.1.0" version = "0.2.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"handlebars", "lazy_static",
"log 0.4.8", "log 0.4.8",
"mime 0.3.16",
"pfacts", "pfacts",
"pretty_env_logger", "pretty_env_logger",
"prometheus",
"rand 0.7.3", "rand 0.7.3",
"ructe",
"serde", "serde",
"tokio", "tokio",
"warp", "warp",
@ -819,6 +855,35 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "procfs"
version = "0.7.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c434e93ef69c216e68e4f417c927b4f31502c3560b72cfdb6827e2321c5c6b3e"
dependencies = [
"bitflags",
"byteorder",
"hex",
"lazy_static",
"libc",
"libflate",
]
[[package]]
name = "prometheus"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd0ced56dee39a6e960c15c74dc48849d614586db2eaada6497477af7c7811cd"
dependencies = [
"cfg-if",
"fnv",
"lazy_static",
"libc",
"procfs",
"spin",
"thiserror",
]
[[package]] [[package]]
name = "quick-error" name = "quick-error"
version = "1.2.3" version = "1.2.3"
@ -1023,6 +1088,26 @@ dependencies = [
"winapi 0.3.8", "winapi 0.3.8",
] ]
[[package]]
name = "rle-decode-fast"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cabe4fa914dec5870285fa7f71f602645da47c486e68486d2b4ceb4a343e90ac"
[[package]]
name = "ructe"
version = "0.11.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f615d1e172dcc01a7cd78c7f77f21a5669c6de4341548ad2e7764e9045d06657"
dependencies = [
"base64 0.12.1",
"bytecount",
"itertools",
"md5",
"mime 0.3.16",
"nom",
]
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.4" version = "1.0.4"
@ -1114,6 +1199,18 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7cb5678e1615754284ec264d9bb5b4c27d2018577fd90ac0ceb578591ed5ee4" checksum = "c7cb5678e1615754284ec264d9bb5b4c27d2018577fd90ac0ceb578591ed5ee4"
[[package]]
name = "spin"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.21" version = "1.0.21"
@ -1148,6 +1245,26 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "thiserror"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dfdd070ccd8ccb78f4ad66bf1982dc37f620ef696c6b5028fe2ed83dd3d0d08"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd80fc12f73063ac132ac92aceea36734f04a1d93c1240c6944e23a3b8841793"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "thread_local" name = "thread_local"
version = "1.0.1" version = "1.0.1"
@ -1269,12 +1386,6 @@ version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33"
[[package]]
name = "ucd-trie"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c"
[[package]] [[package]]
name = "unicase" name = "unicase"
version = "1.4.2" version = "1.4.2"

View File

@ -1,18 +1,24 @@
[package] [package]
name = "printerfacts" name = "printerfacts"
version = "0.1.0" version = "0.2.0"
authors = ["Christine Dodrill <me@christine.website>"] authors = ["Christine Dodrill <me@christine.website>"]
edition = "2018" edition = "2018"
build = "build.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
anyhow = "1" anyhow = "1"
handlebars = "3.2.1" lazy_static = "1.4"
log = "0" log = "0"
mime = "0.3.0"
pfacts = "0.1.0" pfacts = "0.1.0"
pretty_env_logger = "0" pretty_env_logger = "0"
prometheus = { version = "0.9", default-features = false, features = ["process"] }
rand = "0" rand = "0"
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
tokio = { version = "0.2", features = ["macros"] } tokio = { version = "0.2", features = ["macros"] }
warp = "0.2" warp = "0.2"
[build-dependencies]
ructe = { version = "0.11", features = ["warp02"] }

1
VERSION Normal file
View File

@ -0,0 +1 @@
0.2.0

5
build.rs Normal file
View File

@ -0,0 +1,5 @@
use ructe::{Result, Ructe};
fn main() -> Result<()> {
Ructe::from_env()?.compile_templates("templates")
}

View File

@ -1,94 +1,70 @@
use handlebars::Handlebars; use crate::templates::RenderRucte;
use lazy_static::lazy_static;
use pfacts::Facts; use pfacts::Facts;
use prometheus::{opts, register_int_counter_vec, IntCounterVec};
use rand::prelude::*; use rand::prelude::*;
use serde::Serialize; use std::convert::Infallible;
use std::{convert::Infallible, sync::Arc}; use warp::{http::Response, Filter, Rejection, Reply};
use warp::Filter;
include!(concat!(env!("OUT_DIR"), "/templates.rs"));
const APPLICATION_NAME: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
lazy_static! {
static ref HIT_COUNTER: IntCounterVec =
register_int_counter_vec!(opts!("hits", "Number of hits to various pages"), &["page"])
.unwrap();
}
async fn give_fact(facts: Facts) -> Result<String, Infallible> { async fn give_fact(facts: Facts) -> Result<String, Infallible> {
HIT_COUNTER.with_label_values(&["fact"]).inc();
Ok(facts.choose(&mut thread_rng()).unwrap().clone()) Ok(facts.choose(&mut thread_rng()).unwrap().clone())
} }
#[derive(Serialize)] async fn index(facts: Facts) -> Result<impl Reply, Rejection> {
struct TemplateContext { HIT_COUNTER.with_label_values(&["index"]).inc();
title: &'static str, Response::builder()
fact: Option<String>, .html(|o| templates::index_html(o, facts.choose(&mut thread_rng()).unwrap().clone()))
// This key tells handlebars which template is the parent.
parent: &'static str,
} }
struct WithTemplate<T: Serialize> { async fn not_found() -> Result<impl Reply, Rejection> {
name: &'static str, HIT_COUNTER.with_label_values(&["not_found"]).inc();
value: T, Response::builder()
} .status(404)
.html(|o| templates::not_found_html(o))
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)
} }
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
pretty_env_logger::init(); pretty_env_logger::init();
let facts = pfacts::make(); let facts = pfacts::make();
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 fact = {
let facts = facts.clone(); let facts = facts.clone();
warp::any().map(move || facts.clone()) warp::any().map(move || facts.clone())
}; };
let hb = Arc::new(hb);
let handlebars = move |with_template| render(with_template, hb.clone());
let files = warp::path("static").and(warp::fs::dir("./static")); let files = warp::path("static").and(warp::fs::dir("./static"));
let fact_handler = warp::get() let fact_handler = warp::get()
.and(warp::path("fact")) .and(warp::path("fact"))
.and(fact) .and(fact.clone())
.and_then(give_fact); .and_then(give_fact);
let index_handler = warp::get() let index_handler = warp::get()
.and(warp::path::end()) .and(warp::path::end())
.map(move || WithTemplate { .and(fact.clone())
name: "index", .and_then(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() let not_found_handler = warp::any().and_then(not_found);
.map(move || WithTemplate {
name: "error/404",
value: TemplateContext {
title: "Not Found",
fact: None,
parent: "layout",
},
})
.map(handlebars.clone());
log::info!("listening on port 5000"); log::info!("listening on port 5000");
warp::serve( warp::serve(
fact_handler fact_handler
.or(index_handler) .or(index_handler)
.or(files) .or(files)
.or(not_found_handler), .or(not_found_handler)
.with(warp::log(APPLICATION_NAME)),
) )
.run(([0, 0, 0, 0], 5000)) .run(([0, 0, 0, 0], 5000))
.await; .await;

View File

@ -1,6 +0,0 @@
{{#*inline "page"}}
<p>This page does not exist!</p>
<a href="/">Go home</a>
{{/inline}}
{{~> (parent)~}}

View File

@ -1,3 +0,0 @@
<footer>
<p>Be well, Creator - <a href="https://tulpa.dev/cadey/printerfacts">Source code here</a> - From <a href="https://christine.website">Within</a></p>
</footer>

8
templates/footer.rs.html Normal file
View File

@ -0,0 +1,8 @@
@()
</main>
<footer>
<p>Be well, Creator - <a href="https://tulpa.dev/cadey/printerfacts">Source code here</a> - From <a href="https://christine.website">Within</a></p>
</footer>
</body>
</html>

View File

@ -1,17 +1,12 @@
@(title: &str)
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>{{ title }}</title> <title>@title</title>
<link rel="stylesheet" href="/static/gruvbox.css"> <link rel="stylesheet" href="/static/gruvbox.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head> </head>
<body id="top"> <body id="top">
<main> <main>
<h1>{{ title }}</h1> <h1>@title</h1>
{{~> page}}
{{> footer }}
</main>
</body>
</html>

View File

@ -1,10 +0,0 @@
{{#*inline "page"}}
<p>
<blockquote>
{{ fact }}
</blockquote>
</p>
{{/inline}}
{{~> (parent)~}}

13
templates/index.rs.html Normal file
View File

@ -0,0 +1,13 @@
@use super::{header_html, footer_html};
@(fact: String)
@:header_html("Printer Facts")
<p>
<blockquote>
@fact
</blockquote>
</p>
@:footer_html()

View File

@ -0,0 +1,12 @@
@use super::{header_html, footer_html};
@()
@:header_html("Not Found")
<h1>Not Found</h1>
<p>This page does not exist!</p>
<a href="/">Go home</a>
@:footer_html()