Skip to content

Embed static resources and templates into the site binary in release mode #1554

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 1 commit into from
Mar 31, 2023
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
36 changes: 36 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,5 @@ RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \

COPY --from=build /target/release/postgres-to-sqlite /usr/local/bin/rustc-perf-postgres-to-sqlite
COPY --from=build /target/release/site /usr/local/bin/rustc-perf-site
COPY --from=build site/static /site/static
COPY --from=build site/templates /site/templates

CMD rustc-perf-site
1 change: 1 addition & 0 deletions site/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ mime = "0.3"
prometheus = "0.13"
uuid = { version = "1.3.0", features = ["v4"] }
tera = "1.18"
rust-embed = { version = "6.6.0", features = ["include-exclude"] }

[target.'cfg(unix)'.dependencies]
jemallocator = "0.5"
Expand Down
2 changes: 1 addition & 1 deletion site/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ mod average;
mod comparison;
mod interpolate;
mod request_handlers;
mod resources;
mod selector;
mod self_profile;
mod templates;
61 changes: 61 additions & 0 deletions site/src/resources.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use tera::Tera;
use tokio::sync::RwLock;

use rust_embed::RustEmbed;

/// Static files and templates are embedded into the binary (in release mode) or hot-reloaded
/// from the `site` directory (in debug mode).
#[derive(RustEmbed)]
#[folder = "static/"]
#[include = "*.js"]
#[include = "*.css"]
#[include = "*.svg"]
#[include = "*.png"]
struct StaticAssets;

#[derive(RustEmbed)]
#[folder = "templates/"]
#[include = "*.html"]
struct TemplateAssets;

pub struct ResourceResolver {
tera: RwLock<Tera>,
}

impl ResourceResolver {
pub fn new() -> anyhow::Result<Self> {
let tera = load_templates()?;

Ok(Self {
tera: RwLock::new(tera),
})
}

pub fn get_static_asset(&self, path: &str) -> Option<Vec<u8>> {
StaticAssets::get(path).map(|file| file.data.to_vec())
}

pub async fn get_template(&self, path: &str) -> anyhow::Result<Vec<u8>> {
// Live-reload the template if we're in debug mode
#[cfg(debug_assertions)]
{
*self.tera.write().await = load_templates()?;
}

let context = tera::Context::new();
let rendered = self.tera.read().await.render(path, &context)?;
Ok(rendered.into_bytes())
}
}

fn load_templates() -> anyhow::Result<Tera> {
let templates = TemplateAssets::iter().map(|path| {
(
path.to_string(),
String::from_utf8(TemplateAssets::get(&path).unwrap().data.to_vec()).unwrap(),
)
});
let mut tera = Tera::default();
tera.add_raw_templates(templates)?;
Ok(tera)
}
40 changes: 12 additions & 28 deletions site/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use brotli::enc::BrotliEncoderParams;
use brotli::BrotliCompress;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::path::Path;
use std::str::FromStr;
use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering};
use std::sync::Arc;
use std::time::Instant;
use std::{fmt, fs, str};
use std::{fmt, str};

use futures::{future::FutureExt, stream::StreamExt};
use headers::{Authorization, CacheControl, ContentType, ETag, Header, HeaderMapExt, IfNoneMatch};
Expand All @@ -28,7 +28,7 @@ pub use crate::api::{
use crate::db::{self, ArtifactId};
use crate::load::{Config, SiteCtxt};
use crate::request_handlers;
use crate::templates::TemplateRenderer;
use crate::resources::ResourceResolver;

pub type Request = http::Request<hyper::Body>;
pub type Response = http::Response<hyper::Body>;
Expand Down Expand Up @@ -566,23 +566,12 @@ where

lazy_static::lazy_static! {
static ref VERSION_UUID: Uuid = Uuid::new_v4(); // random UUID used as ETag for cache revalidation
static ref TEMPLATES: TemplateRenderer = {
let livereload = std::env::var("LIVERELOAD").is_ok();
TemplateRenderer::new(PathBuf::from("site/templates"), livereload).unwrap()
};
static ref TEMPLATES: ResourceResolver = ResourceResolver::new().expect("Cannot load resources");
}

/// Handle the case where the path is to a static file
async fn handle_fs_path(req: &Request, path: &str) -> Option<http::Response<hyper::Body>> {
let fs_path = format!(
"site/static{}",
match path {
"" | "/" => "/index.html",
_ => path,
}
);

if fs_path.contains("./") | fs_path.contains("../") {
if path.contains("./") | path.contains("../") {
return Some(not_found());
}

Expand All @@ -598,31 +587,26 @@ async fn handle_fs_path(req: &Request, path: &str) -> Option<http::Response<hype
}
}

async fn render_page(path: &str) -> Vec<u8> {
async fn resolve_template(path: &str) -> Vec<u8> {
TEMPLATES
.render(&format!("pages/{}", path))
.get_template(&format!("pages/{}", path))
.await
.unwrap()
.into_bytes()
}

let relative_path = path.trim_start_matches("/");
let source = match path {
"" | "/" | "/index.html" => render_page("graphs.html").await,
"" | "/" | "/index.html" => resolve_template("graphs.html").await,
"/bootstrap.html"
| "/compare.html"
| "/dashboard.html"
| "/detailed-query.html"
| "/help.html"
| "/status.html" => render_page(path.trim_start_matches("/")).await,
_ => {
if !Path::new(&fs_path).is_file() {
return None;
}
fs::read(&fs_path).unwrap()
}
| "/status.html" => resolve_template(relative_path).await,
_ => TEMPLATES.get_static_asset(relative_path)?,
};

let p = Path::new(&fs_path);
let p = Path::new(&path);
match p.extension().and_then(|x| x.to_str()) {
Some("html") => response = response.header_typed(ContentType::html()),
Some("png") => response = response.header_typed(ContentType::png()),
Expand Down
37 changes: 0 additions & 37 deletions site/src/templates.rs

This file was deleted.