Skip to content

[WIP] Migrate to Tera for templating #786

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

Closed
wants to merge 4 commits into from
Closed
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
357 changes: 253 additions & 104 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,16 @@ serde_json = "1.0"
# iron dependencies
iron = "0.5"
router = "0.5"
handlebars-iron = "0.25"
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"

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

Expand All @@ -71,6 +76,7 @@ kuchiki = "0.8"
criterion = "0.3"
rand = "0.7.3"


[[bench]]
name = "html5ever"
harness = false
Expand Down
10 changes: 8 additions & 2 deletions src/bin/cratesfyi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ 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")]
reload: bool,
},

/// Starts cratesfyi daemon
Expand All @@ -78,8 +81,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,
} => {
Server::start(Some(&socket_addr), reload);
}
Self::Daemon { foreground } => cratesfyi::utils::start_daemon(!foreground),
Self::Database { subcommand } => subcommand.handle_args(),
Expand Down
102 changes: 2 additions & 100 deletions src/docbuilder/limits.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::error::Result;
use postgres::Connection;
use std::collections::BTreeMap;
use serde::Serialize;
use std::time::Duration;

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub(crate) struct Limits {
memory: usize,
targets: usize,
Expand Down Expand Up @@ -67,52 +67,6 @@ impl Limits {
pub(crate) fn targets(&self) -> usize {
self.targets
}

pub(crate) fn for_website(&self) -> BTreeMap<String, String> {
let mut res = BTreeMap::new();
res.insert("Available RAM".into(), SIZE_SCALE(self.memory));
res.insert(
"Maximum rustdoc execution time".into(),
TIME_SCALE(self.timeout.as_secs() as usize),
);
res.insert(
"Maximum size of a build log".into(),
SIZE_SCALE(self.max_log_size),
);
if self.networking {
res.insert("Network access".into(), "allowed".into());
} else {
res.insert("Network access".into(), "blocked".into());
}
res.insert(
"Maximum number of build targets".into(),
self.targets.to_string(),
);
res
}
}

const TIME_SCALE: fn(usize) -> String = |v| scale(v, 60, &["seconds", "minutes", "hours"]);
const SIZE_SCALE: fn(usize) -> String = |v| scale(v, 1024, &["bytes", "KB", "MB", "GB"]);

fn scale(value: usize, interval: usize, labels: &[&str]) -> String {
let (mut value, interval) = (value as f64, interval as f64);
let mut chosen_label = &labels[0];
for label in &labels[1..] {
if value / interval >= 1.0 {
chosen_label = label;
value /= interval;
} else {
break;
}
}
// 2.x
let mut value = format!("{:.1}", value);
// 2.0 -> 2
if value.ends_with(".0") {
value.truncate(value.len() - 2);
}
format!("{} {}", value, chosen_label)
}

#[cfg(test)]
Expand Down Expand Up @@ -161,56 +115,4 @@ mod test {
Ok(())
});
}

#[test]
fn display_limits() {
let limits = Limits {
memory: 102_400,
timeout: Duration::from_secs(300),
targets: 1,
..Limits::default()
};
let display = limits.for_website();
assert_eq!(display.get("Network access"), Some(&"blocked".into()));
assert_eq!(
display.get("Maximum size of a build log"),
Some(&"100 KB".into())
);
assert_eq!(
display.get("Maximum number of build targets"),
Some(&limits.targets.to_string())
);
assert_eq!(
display.get("Maximum rustdoc execution time"),
Some(&"5 minutes".into())
);
assert_eq!(display.get("Available RAM"), Some(&"100 KB".into()));
}

#[test]
fn scale_limits() {
// time
assert_eq!(TIME_SCALE(300), "5 minutes");
assert_eq!(TIME_SCALE(1), "1 seconds");
assert_eq!(TIME_SCALE(7200), "2 hours");

// size
assert_eq!(SIZE_SCALE(1), "1 bytes");
assert_eq!(SIZE_SCALE(100), "100 bytes");
assert_eq!(SIZE_SCALE(1024), "1 KB");
assert_eq!(SIZE_SCALE(10240), "10 KB");
assert_eq!(SIZE_SCALE(1_048_576), "1 MB");
assert_eq!(SIZE_SCALE(10_485_760), "10 MB");
assert_eq!(SIZE_SCALE(1_073_741_824), "1 GB");
assert_eq!(SIZE_SCALE(10_737_418_240), "10 GB");
assert_eq!(SIZE_SCALE(std::u32::MAX as usize), "4 GB");

// fractional sizes
assert_eq!(TIME_SCALE(90), "1.5 minutes");
assert_eq!(TIME_SCALE(5400), "1.5 hours");

assert_eq!(SIZE_SCALE(1_288_490_189), "1.2 GB");
assert_eq!(SIZE_SCALE(3_758_096_384), "3.5 GB");
assert_eq!(SIZE_SCALE(1_048_051_712), "999.5 MB");
}
}
14 changes: 6 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ use web::page::GlobalAlert;

// Warning message shown in the navigation bar of every page. Set to `None` to hide it.
pub(crate) static GLOBAL_ALERT: Option<GlobalAlert> = None;
/*
pub(crate) static GLOBAL_ALERT: Option<GlobalAlert> = Some(GlobalAlert {
url: "https://blog.rust-lang.org/2019/09/18/upcoming-docsrs-changes.html",
text: "Upcoming docs.rs breaking changes!",
css_class: "error",
fa_icon: "warning",
});
*/
// pub(crate) static GLOBAL_ALERT: Option<GlobalAlert> = Some(GlobalAlert {
// url: "https://blog.rust-lang.org/2019/09/18/upcoming-docsrs-changes.html",
// text: "Upcoming docs.rs breaking changes!",
// css_class: "error",
// fa_icon: "warning",
// });

/// Version string generated at build time contains last git
/// commit hash and build date
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
Loading