Skip to content
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

Caching #480

Merged
merged 3 commits into from
Mar 16, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ rev = "4a72ea2ec716cb0b26188fb00bccf2ef7d1e031c"
[build-dependencies]
ructe = "0.5.6"
rsass = "0.9"
plume-common = {path = "plume-common"}

[features]
default = ["postgres"]
Expand Down
37 changes: 36 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,38 @@ extern crate ructe;
extern crate rsass;
use ructe::*;
use std::{env, fs::*, io::Write, path::PathBuf};
use std::process::{Command, Stdio};

fn compute_static_hash() -> String {
//"find static/ -type f ! -path 'static/media/*' | sort | xargs stat --printf='%n %Y\n' | sha256sum"

let find = Command::new("find")
.args(&["static/", "-type", "f", "!", "-path", "static/media/*"])
.stdout(Stdio::piped())
.spawn()
.expect("failed find command");

let sort = Command::new("sort")
igalic marked this conversation as resolved.
Show resolved Hide resolved
.stdin(find.stdout.unwrap())
.stdout(Stdio::piped())
.spawn()
.expect("failed sort command");

let xargs = Command::new("xargs")
.args(&["stat", "--printf='%n %Y\n'"])
.stdin(sort.stdout.unwrap())
.stdout(Stdio::piped())
.spawn()
.expect("failed xargs command");

let sha = Command::new("sha256sum")
.stdin(xargs.stdout.unwrap())
.output()
.expect("failed sha256sum command");

String::from_utf8(sha.stdout).unwrap()
}


fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
Expand All @@ -16,8 +48,11 @@ fn main() {
.expect("Error during SCSS compilation")
).expect("Couldn't write CSS output");

let cache_id = &compute_static_hash()[..8];
println!("cargo:rerun-if-changed=target/deploy/plume-front.wasm");
copy("target/deploy/plume-front.wasm", "static/plume-front.wasm")
.and_then(|_| read_to_string("target/deploy/plume-front.js"))
.and_then(|js| write("static/plume-front.js", js.replace("\"plume-front.wasm\"", "\"/static/plume-front.wasm\""))).ok();
.and_then(|js| write("static/plume-front.js", js.replace("\"plume-front.wasm\"", &format!("\"/static/cached/{}/plume-front.wasm\"", cache_id)))).ok();

println!("cargo:rustc-env=CACHE_ID={}", cache_id)
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ Then try to restart Plume.
routes::session::password_reset_form,
routes::session::password_reset,

routes::plume_static_files,
routes::static_files,

routes::tags::tag,
Expand Down
26 changes: 23 additions & 3 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use atom_syndication::{ContentBuilder, Entry, EntryBuilder, LinkBuilder, Person, PersonBuilder};
use rocket::{
http::{RawStr, Status, uri::{FromUriParam, Query}},
http::{
RawStr, Status, uri::{FromUriParam, Query},
hyper::header::{CacheControl, CacheDirective}
},
Outcome,
request::{self, FromFormValue, FromRequest, Request},
response::NamedFile,
Expand Down Expand Up @@ -101,7 +104,24 @@ pub mod user;
pub mod search;
pub mod well_known;

#[get("/static/<file..>", rank = 2)]
pub fn static_files(file: PathBuf) -> Option<NamedFile> {
#[derive(Responder)]
#[response()]
pub struct CachedFile {
inner: NamedFile,
cache_control: CacheControl
}

#[get("/static/cached/<_build_id>/<file..>", rank = 2)]
pub fn plume_static_files(file: PathBuf, _build_id: &RawStr) -> Option<CachedFile> {
static_files(file)
}

#[get("/static/<file..>", rank = 3)]
pub fn static_files(file: PathBuf) -> Option<CachedFile> {
NamedFile::open(Path::new("static/").join(file)).ok()
.map(|f|
CachedFile {
inner: f,
cache_control: CacheControl(vec![CacheDirective::MaxAge(60*60*24*30)])
})
}
2 changes: 2 additions & 0 deletions src/template_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use templates::Html;

pub use askama_escape::escape;

pub static CACHE_NAME: &str = env!("CACHE_ID");

pub type BaseContext<'a> = &'a(&'a Connection, &'a Catalog, Option<User>);

pub type Ructe = Content<Vec<u8>>;
Expand Down
6 changes: 3 additions & 3 deletions static/css/main.scss
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* color palette: https://coolors.co/23f0c7-ef767a-7765e3-6457a6-ffe347 */

@import url('/static/fonts/Route159/Route159.css');
@import url('/static/fonts/Lora/Lora.css');
@import url('/static/fonts/Playfair_Display/PlayfairDisplay.css');
@import url('../fonts/Route159/Route159.css');
@import url('../fonts/Lora/Lora.css');
@import url('../fonts/Playfair_Display/PlayfairDisplay.css');

html {
box-sizing: border-box;
Expand Down
10 changes: 5 additions & 5 deletions templates/base.rs.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
<meta charset="utf-8" />
<title>@title ⋅ @i18n!(ctx.1, "Plume")</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="@uri!(static_files: file = "css/main.css")" />
<link rel="stylesheet" href="@uri!(static_files: file = "css/feather.css")" />
<link rel="stylesheet" href="@uri!(plume_static_files: file = "css/main.css", _build_id = CACHE_NAME)" />
<link rel="stylesheet" href="@uri!(plume_static_files: file = "css/feather.css", _build_id = CACHE_NAME)" />
<link rel="manifest" href="@uri!(instance::web_manifest)" />
<link rel="icon" type="image/png" href="@uri!(static_files: file = "icons/trwnh/feather-filled/plumeFeatherFilled64.png")">
<link rel="icon" type="image/png" href="@uri!(plume_static_files: file = "icons/trwnh/feather-filled/plumeFeatherFilled64.png", _build_id = CACHE_NAME)">
@:head()
</head>
<body>
Expand All @@ -22,7 +22,7 @@
<div id="content">
<nav>
<a href="@uri!(instance::index)" class="title">
<img src="@uri!(static_files: file = "icons/trwnh/feather/plumeFeather256.png")">
<img src="@uri!(plume_static_files: file = "icons/trwnh/feather/plumeFeather256.png", _build_id = CACHE_NAME)">
<p>@i18n!(ctx.1, "Plume")</p>
</a>
<hr/>
Expand Down Expand Up @@ -79,6 +79,6 @@
<a href="@uri!(instance::admin)">@i18n!(ctx.1, "Administration")</a>
}
</footer>
<script src="@uri!(static_files: file = "plume-front.js")"></script>
<script src="@uri!(plume_static_files: file = "plume-front.js", _build_id = CACHE_NAME)"></script>
</body>
</html>