Skip to content

Remove TEMPLATE_DATA static #841

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
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
6 changes: 4 additions & 2 deletions src/bin/cratesfyi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ impl CommandLine {
socket_addr,
reload_templates,
} => {
Server::start(Some(&socket_addr), reload_templates, config);
Server::start(Some(&socket_addr), reload_templates, config)?;
}
Self::Daemon { foreground } => {
cratesfyi::utils::start_daemon(!foreground, config)?;
}
Self::Daemon { foreground } => cratesfyi::utils::start_daemon(!foreground, config),
Self::Database { subcommand } => subcommand.handle_args(),
Self::Queue { subcommand } => subcommand.handle_args(),
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ pub(crate) struct TestFrontend {
impl TestFrontend {
fn new(db: &TestDatabase, config: Arc<Config>) -> Self {
Self {
server: Server::start_test(db.conn.clone(), config),
server: Server::start_test(db.conn.clone(), config)
.expect("failed to start the server"),
client: Client::new(),
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/utils/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
Config, DocBuilder, DocBuilderOptions,
};
use chrono::{Timelike, Utc};
use failure::Error;
use log::{debug, error, info, warn};
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::path::PathBuf;
Expand All @@ -18,7 +19,7 @@ use std::{env, thread};
#[cfg(not(target_os = "windows"))]
use ::{libc::fork, std::fs::File, std::io::Write, std::process::exit};

pub fn start_daemon(background: bool, config: Arc<Config>) {
pub fn start_daemon(background: bool, config: Arc<Config>) -> Result<(), Error> {
const CRATE_VARIABLES: [&str; 3] = [
"CRATESFYI_PREFIX",
"CRATESFYI_GITHUB_USERNAME",
Expand Down Expand Up @@ -250,7 +251,8 @@ pub fn start_daemon(background: bool, config: Arc<Config>) {
// at least start web server
info!("Starting web server");

crate::Server::start(None, false, config);
crate::Server::start(None, false, config)?;
Ok(())
}

fn opts() -> DocBuilderOptions {
Expand Down
53 changes: 42 additions & 11 deletions src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ mod rustdoc;
mod sitemap;
mod source;

use self::page::TemplateData;
use self::pool::Pool;
use crate::config::Config;
use chrono::{DateTime, Utc};
use failure::Error;
use handlebars_iron::{DirectorySource, HandlebarsEngine, SourceError};
use iron::headers::{CacheControl, CacheDirective, ContentType, Expires, HttpDate};
use iron::modifiers::Redirect;
Expand Down Expand Up @@ -112,8 +114,12 @@ impl CratesfyiHandler {
chain
}

fn new(pool: Pool, config: Arc<Config>) -> CratesfyiHandler {
let inject_extensions = InjectExtensions { pool, config };
fn new(pool: Pool, config: Arc<Config>, template_data: Arc<TemplateData>) -> CratesfyiHandler {
let inject_extensions = InjectExtensions {
pool,
config,
template_data,
};

let routes = routes::build_routes();
let blacklisted_prefixes = routes.page_prefixes();
Expand Down Expand Up @@ -215,12 +221,15 @@ impl Handler for CratesfyiHandler {
struct InjectExtensions {
pool: Pool,
config: Arc<Config>,
template_data: Arc<TemplateData>,
}

impl BeforeMiddleware for InjectExtensions {
fn before(&self, req: &mut Request) -> IronResult<()> {
req.extensions.insert::<Pool>(self.pool.clone());
req.extensions.insert::<Config>(self.config.clone());
req.extensions
.insert::<TemplateData>(self.template_data.clone());

Ok(())
}
Expand Down Expand Up @@ -383,24 +392,46 @@ pub struct Server {
}

impl Server {
pub fn start(addr: Option<&str>, reload_templates: bool, config: Arc<Config>) -> Self {
pub fn start(
addr: Option<&str>,
reload_templates: bool,
config: Arc<Config>,
) -> Result<Self, Error> {
// Initialize templates
let _: &page::TemplateData = &*page::TEMPLATE_DATA;
let template_data = Arc::new(TemplateData::new()?);
if reload_templates {
page::TemplateData::start_template_reloading();
TemplateData::start_template_reloading(template_data.clone());
}

let server = Self::start_inner(addr.unwrap_or(DEFAULT_BIND), Pool::new(), config);
let server = Self::start_inner(
addr.unwrap_or(DEFAULT_BIND),
Pool::new(),
config,
template_data,
);
info!("Running docs.rs web server on http://{}", server.addr());
server
Ok(server)
}

#[cfg(test)]
pub(crate) fn start_test(conn: Arc<Mutex<Connection>>, config: Arc<Config>) -> Self {
Self::start_inner("127.0.0.1:0", Pool::new_simple(conn.clone()), config)
pub(crate) fn start_test(
conn: Arc<Mutex<Connection>>,
config: Arc<Config>,
) -> Result<Self, Error> {
Ok(Self::start_inner(
"127.0.0.1:0",
Pool::new_simple(conn.clone()),
config,
Arc::new(TemplateData::new()?),
))
}

fn start_inner(addr: &str, pool: Pool, config: Arc<Config>) -> Self {
fn start_inner(
addr: &str,
pool: Pool,
config: Arc<Config>,
template_data: Arc<TemplateData>,
) -> Self {
// poke all the metrics counters to instantiate and register them
metrics::TOTAL_BUILDS.inc_by(0);
metrics::SUCCESSFUL_BUILDS.inc_by(0);
Expand All @@ -409,7 +440,7 @@ impl Server {
metrics::UPLOADED_FILES_TOTAL.inc_by(0);
metrics::FAILED_DB_CONNECTIONS.inc_by(0);

let cratesfyi = CratesfyiHandler::new(pool, config);
let cratesfyi = CratesfyiHandler::new(pool, config, template_data);
let inner = Iron::new(cratesfyi)
.http(addr)
.unwrap_or_else(|_| panic!("Failed to bind to socket on {}", addr));
Expand Down
5 changes: 0 additions & 5 deletions src/web/page/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ pub(crate) use web_page::WebPage;

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,
Expand Down
98 changes: 48 additions & 50 deletions src/web/page/templates.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,36 @@
use super::TEMPLATE_DATA;
use crate::error::Result;
use arc_swap::ArcSwap;
use chrono::{DateTime, Utc};
use notify::{watcher, RecursiveMode, Watcher};
use serde_json::Value;
use std::collections::HashMap;
use std::sync::{mpsc::channel, Arc};
use std::thread;
use std::time::Duration;
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
#[derive(Debug)]
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> {
pub(crate) 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,
};

pub(crate) fn start_template_reloading(template_data: Arc<TemplateData>) {
let (tx, rx) = channel();
// Set a 2 second event debounce for the watcher
let mut watcher = watcher(tx, Duration::from_secs(2)).unwrap();
Expand All @@ -67,7 +48,7 @@ impl TemplateData {
match load_templates() {
Ok(templates) => {
log::info!("Reloaded templates");
super::TEMPLATE_DATA.templates.swap(Arc::new(templates));
template_data.templates.swap(Arc::new(templates));
}

Err(err) => log::error!("Error reloading templates: {:?}", err),
Expand All @@ -77,6 +58,10 @@ impl TemplateData {
}
}

impl iron::typemap::Key for TemplateData {
type Value = std::sync::Arc<TemplateData>;
}

// 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> {
Expand All @@ -99,13 +84,31 @@ fn load_rustc_resource_suffix() -> Result<String> {
failure::bail!("failed to parse the rustc version");
}

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

// 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);
// This function will return any global alert, if present.
ReturnValue::add_function_to(
&mut tera,
"global_alert",
serde_json::to_value(crate::GLOBAL_ALERT)?,
);
// This function will return the current version of docs.rs.
ReturnValue::add_function_to(
&mut tera,
"docsrs_version",
Value::String(crate::BUILD_VERSION.into()),
);
// This function will return the resource suffix of the latest nightly used to build
// documentation on docs.rs, or ??? if no resource suffix was found.
ReturnValue::add_function_to(
&mut tera,
"rustc_resource_suffix",
Value::String(load_rustc_resource_suffix().unwrap_or_else(|err| {
log::error!("Failed to load rustc resource suffix: {:?}", err);
String::from("???")
})),
);

// Custom filters
tera.register_filter("timeformat", timeformat);
Expand All @@ -115,28 +118,23 @@ pub(super) fn load_templates() -> TeraResult<Tera> {
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())
/// Simple function that returns the pre-defined value.
struct ReturnValue {
name: &'static str,
value: Value,
Comment on lines +121 to +124
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea on this!

}

/// 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())
impl ReturnValue {
fn add_function_to(tera: &mut Tera, name: &'static str, value: Value) {
tera.register_function(name, Self { name, value })
}
}

/// 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())
impl tera::Function for ReturnValue {
fn call(&self, args: &HashMap<String, Value>) -> TeraResult<Value> {
debug_assert!(args.is_empty(), format!("{} takes no args", self.name));
Ok(self.value.clone())
}
}

/// Prettily format a timestamp
Expand Down
12 changes: 8 additions & 4 deletions src/web/page/web_page.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::TEMPLATE_DATA;
use iron::{headers::ContentType, response::Response, status::Status, IronResult};
use super::TemplateData;
use iron::{headers::ContentType, response::Response, status::Status, IronResult, Request};
use serde::Serialize;
use tera::Context;

Expand Down Expand Up @@ -28,9 +28,13 @@ macro_rules! impl_webpage {
pub trait WebPage: Serialize + Sized {
/// Turn the current instance into a `Response`, ready to be served
// TODO: We could cache similar pages using the `&Context`
fn into_response(self) -> IronResult<Response> {
fn into_response(self, req: &Request) -> IronResult<Response> {
let ctx = Context::from_serialize(&self).unwrap();
let rendered = TEMPLATE_DATA

let rendered = req
.extensions
.get::<TemplateData>()
.expect("missing TemplateData from the request extensions")
.templates
.load()
.render(Self::TEMPLATE, &ctx)
Expand Down
4 changes: 2 additions & 2 deletions src/web/sitemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn sitemap_handler(req: &mut Request) -> IronResult<Response> {
})
.collect::<Vec<(String, String)>>();

SitemapXml { releases }.into_response()
SitemapXml { releases }.into_response(req)
}

pub fn robots_txt_handler(_: &mut Request) -> IronResult<Response> {
Expand Down Expand Up @@ -83,5 +83,5 @@ pub fn about_handler(req: &mut Request) -> IronResult<Response> {
rustc_version,
limits: Limits::default(),
}
.into_response()
.into_response(req)
}