Skip to content

Tera groundwork #805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
335 changes: 335 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ params = "0.8"
staticfile = { version = "0.4", features = [ "cache" ] }
tempfile = "3.1.0"

# Templating
tera = { version = "1.3.0", features = ["builtins"] }

# Template hot-reloading
arc-swap = "0.4.6"
notify = "4.0.15"

[target.'cfg(not(windows))'.dependencies]
libc = "0.2"

Expand Down
11 changes: 9 additions & 2 deletions src/bin/cratesfyi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ enum CommandLine {
StartWebServer {
#[structopt(name = "SOCKET_ADDR", default_value = "0.0.0.0:3000")]
socket_addr: String,

/// Reload templates when they're changed
#[structopt(long = "reload-templates")]
reload_templates: bool,
},

/// Starts cratesfyi daemon
Expand All @@ -78,8 +82,11 @@ impl CommandLine {
pub fn handle_args(self) {
match self {
Self::Build(build) => build.handle_args(),
Self::StartWebServer { socket_addr } => {
Server::start(Some(&socket_addr));
Self::StartWebServer {
socket_addr,
reload_templates,
} => {
Server::start(Some(&socket_addr), reload_templates);
}
Self::Daemon { foreground } => cratesfyi::utils::start_daemon(!foreground),
Self::Database { subcommand } => subcommand.handle_args(),
Expand Down
2 changes: 1 addition & 1 deletion src/utils/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ pub fn start_daemon(background: bool) {
// at least start web server
info!("Starting web server");

crate::Server::start(None);
crate::Server::start(None, false);
}

fn opts() -> DocBuilderOptions {
Expand Down
8 changes: 7 additions & 1 deletion src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,13 @@ pub struct Server {
}

impl Server {
pub fn start(addr: Option<&str>) -> Self {
pub fn start(addr: Option<&str>, reload_templates: bool) -> Self {
// Initialize templates
let _: &page::TemplateData = &*page::TEMPLATE_DATA;
if reload_templates {
page::TemplateData::start_template_reloading();
}

let server = Self::start_inner(addr.unwrap_or(DEFAULT_BIND), Pool::new());
info!("Running docs.rs web server on http://{}", server.addr());
server
Expand Down
36 changes: 6 additions & 30 deletions src/web/page.rs → src/web/page/handlebars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
use handlebars_iron::Template;
use iron::response::Response;
use iron::{status, IronResult, Set};
use serde::ser::{Serialize, SerializeStruct, Serializer};
use serde::{
ser::{SerializeStruct, Serializer},
Serialize,
};
use serde_json::Value;
use std::collections::BTreeMap;

Expand Down Expand Up @@ -32,14 +35,6 @@ fn load_rustc_resource_suffix() -> Result<String, failure::Error> {
failure::bail!("failed to parse the rustc version");
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Serialize)]
pub(crate) struct GlobalAlert {
pub(crate) url: &'static str,
pub(crate) text: &'static str,
pub(crate) css_class: &'static str,
pub(crate) fa_icon: &'static str,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Page<T: Serialize> {
title: Option<String>,
Expand Down Expand Up @@ -155,8 +150,8 @@ fn build_version_safe(version: &str) -> String {

#[cfg(test)]
mod tests {
use super::super::releases::{self, Release};
use super::*;
use crate::web::releases::{self, Release};
use iron::Url;
use serde_json::json;

Expand Down Expand Up @@ -193,7 +188,7 @@ mod tests {
"description": null,
"target_name": null,
"rustdoc_status": false,
"release_time": super::super::duration_to_str(time),
"release_time": super::super::super::duration_to_str(time),
"release_time_rfc3339": time::at(time).rfc3339().to_string(),
"stars": 0
}],
Expand Down Expand Up @@ -240,25 +235,6 @@ mod tests {
})
}

#[test]
fn serialize_global_alert() {
let alert = GlobalAlert {
url: "http://www.hasthelargehadroncolliderdestroyedtheworldyet.com/",
text: "THE WORLD WILL SOON END",
css_class: "THE END IS NEAR",
fa_icon: "https://gph.is/1uOvmqR",
};

let correct_json = json!({
"url": "http://www.hasthelargehadroncolliderdestroyedtheworldyet.com/",
"text": "THE WORLD WILL SOON END",
"css_class": "THE END IS NEAR",
"fa_icon": "https://gph.is/1uOvmqR"
});

assert_eq!(correct_json, serde_json::to_value(&alert).unwrap());
}

#[test]
fn build_version_url_safe() {
let safe = format!(
Expand Down
45 changes: 45 additions & 0 deletions src/web/page/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
mod handlebars;
mod templates;

pub use handlebars::*;
pub(crate) use templates::TemplateData;

use serde::Serialize;

lazy_static::lazy_static! {
/// Holds all data relevant to templating
pub(crate) static ref TEMPLATE_DATA: TemplateData = TemplateData::new().expect("Failed to load template data");
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize)]
pub(crate) struct GlobalAlert {
pub(crate) url: &'static str,
pub(crate) text: &'static str,
pub(crate) css_class: &'static str,
pub(crate) fa_icon: &'static str,
}

#[cfg(test)]
mod tera_tests {
use super::*;
use serde_json::json;

#[test]
fn serialize_global_alert() {
let alert = GlobalAlert {
url: "http://www.hasthelargehadroncolliderdestroyedtheworldyet.com/",
text: "THE WORLD WILL SOON END",
css_class: "THE END IS NEAR",
fa_icon: "https://gph.is/1uOvmqR",
};

let correct_json = json!({
"url": "http://www.hasthelargehadroncolliderdestroyedtheworldyet.com/",
"text": "THE WORLD WILL SOON END",
"css_class": "THE END IS NEAR",
"fa_icon": "https://gph.is/1uOvmqR"
});

assert_eq!(correct_json, serde_json::to_value(&alert).unwrap());
}
}
204 changes: 204 additions & 0 deletions src/web/page/templates.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
use super::TEMPLATE_DATA;
use crate::error::Result;
use arc_swap::ArcSwap;
use serde_json::Value;
use std::collections::HashMap;
use tera::{Result as TeraResult, Tera};

/// Holds all data relevant to templating
///
/// Most data is stored as a pre-serialized `Value` so that we don't have to
/// re-serialize them every time they're needed. The values themselves are exposed
/// to templates via custom functions
pub(crate) struct TemplateData {
/// The actual templates, stored in an `ArcSwap` so that they're hot-swappable
// TODO: Conditional compilation so it's not always wrapped, the `ArcSwap` is unneeded overhead for prod
pub templates: ArcSwap<Tera>,
/// The current global alert, serialized into a json value
global_alert: Value,
/// The version of docs.rs, serialized into a json value
docsrs_version: Value,
/// The current resource suffix of rustc, serialized into a json value
resource_suffix: Value,
}

impl TemplateData {
pub fn new() -> Result<Self> {
log::trace!("Loading templates");

let data = Self {
templates: ArcSwap::from_pointee(load_templates()?),
global_alert: serde_json::to_value(crate::GLOBAL_ALERT)?,
docsrs_version: Value::String(crate::BUILD_VERSION.to_owned()),
resource_suffix: Value::String(load_rustc_resource_suffix().unwrap_or_else(|err| {
log::error!("Failed to load rustc resource suffix: {:?}", err);
String::from("???")
})),
};

log::trace!("Finished loading templates");

Ok(data)
}

pub fn start_template_reloading() {
use notify::{watcher, RecursiveMode, Watcher};
use std::{
sync::{mpsc::channel, Arc},
thread,
time::Duration,
};

let (tx, rx) = channel();
// Set a 2 second event debounce for the watcher
let mut watcher = watcher(tx, Duration::from_secs(2)).unwrap();

watcher
.watch("tera-templates", RecursiveMode::Recursive)
.unwrap();

thread::spawn(move || {
// The watcher needs to be moved into the thread so that it's not dropped (when dropped,
// all updates cease)
let _watcher = watcher;

while rx.recv().is_ok() {
match load_templates() {
Ok(templates) => {
log::info!("Reloaded templates");
super::TEMPLATE_DATA.templates.swap(Arc::new(templates));
}

Err(err) => log::error!("Error reloading templates: {:?}", err),
}
}
});
}
}

// TODO: Is there a reason this isn't fatal? If the rustc version is incorrect (Or "???" as used by default), then
// all pages will be served *really* weird because they'll lack all CSS
fn load_rustc_resource_suffix() -> Result<String> {
let conn = crate::db::connect_db()?;

let res = conn.query(
"SELECT value FROM config WHERE name = 'rustc_version';",
&[],
)?;
if res.is_empty() {
failure::bail!("missing rustc version");
}

if let Some(Ok(vers)) = res.get(0).get_opt::<_, Value>("value") {
if let Some(vers_str) = vers.as_str() {
return Ok(crate::utils::parse_rustc_version(vers_str)?);
}
}

failure::bail!("failed to parse the rustc version");
}

pub(super) fn load_templates() -> TeraResult<Tera> {
let mut tera = Tera::new("templates-tera/**/*")?;

// Custom functions
tera.register_function("global_alert", global_alert);
tera.register_function("docsrs_version", docsrs_version);
tera.register_function("rustc_resource_suffix", rustc_resource_suffix);

// Custom filters
tera.register_filter("timeformat", timeformat);
tera.register_filter("dbg", dbg);
tera.register_filter("dedent", dedent);

Ok(tera)
}

/// Returns an `Option<GlobalAlert>` in json form for templates
fn global_alert(args: &HashMap<String, Value>) -> TeraResult<Value> {
debug_assert!(args.is_empty(), "global_alert takes no args");

Ok(TEMPLATE_DATA.global_alert.clone())
}

/// Returns the version of docs.rs, takes the `safe` parameter which can be `true` to get a url-safe version
fn docsrs_version(args: &HashMap<String, Value>) -> TeraResult<Value> {
debug_assert!(
args.is_empty(),
"docsrs_version only takes no args, to get a safe version use `docsrs_version() | slugify`",
);

Ok(TEMPLATE_DATA.docsrs_version.clone())
}

/// Returns the current rustc resource suffix
fn rustc_resource_suffix(args: &HashMap<String, Value>) -> TeraResult<Value> {
debug_assert!(args.is_empty(), "rustc_resource_suffix takes no args");

Ok(TEMPLATE_DATA.resource_suffix.clone())
}

/// Prettily format a timestamp
// TODO: This can be replaced by chrono
fn timeformat(value: &Value, args: &HashMap<String, Value>) -> TeraResult<Value> {
let fmt = if let Some(Value::Bool(true)) = args.get("relative") {
let value = time::strptime(value.as_str().unwrap(), "%Y-%m-%dT%H:%M:%S%z").unwrap();

super::super::duration_to_str(value.to_timespec())
} else {
const TIMES: &[&str] = &["seconds", "minutes", "hours"];

let mut value = value.as_f64().unwrap();
let mut chosen_time = &TIMES[0];

for time in &TIMES[1..] {
if value / 60.0 >= 1.0 {
chosen_time = time;
value /= 60.0;
} else {
break;
}
}

// TODO: This formatting section can be optimized, two string allocations aren't needed
let mut value = format!("{:.1}", value);
if value.ends_with(".0") {
value.truncate(value.len() - 2);
}

format!("{} {}", value, chosen_time)
};

Ok(Value::String(fmt))
}

/// Print a tera value to stdout
fn dbg(value: &Value, _args: &HashMap<String, Value>) -> TeraResult<Value> {
println!("{:?}", value);

Ok(value.clone())
}

/// Dedent a string by removing all leading whitespace
fn dedent(value: &Value, _args: &HashMap<String, Value>) -> TeraResult<Value> {
let string = value.as_str().expect("dedent takes a string");

Ok(Value::String(
string
.lines()
.map(|l| l.trim_start())
.collect::<Vec<&str>>()
.join("\n"),
))
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_templates_are_valid() {
let tera = load_templates().unwrap();
tera.check_macro_files().unwrap();
}
}
Empty file added tera-templates/.gitkeep
Empty file.