Add cache-busting and server headers with a wrapper.
In order to support custom headers for various response types, this commit adds a wrapper type, ResponseWrapper, which can service all types of response in `bin`. For paste objects, the preferred `Last-Modified` is used, so that caches can compare their exact timings with the HEAD response when revalidating. For static objects, an `ETag` is used instead, based on the Cargo version and git hash of the codebase at compilation time; a `build.rs` is used for this.
This commit is contained in:
parent
55ed495b83
commit
2ab7ddb9c8
12 changed files with 291 additions and 152 deletions
15
src/main.rs
15
src/main.rs
|
|
@ -3,6 +3,7 @@ extern crate rocket;
|
|||
use std::{fs, net::IpAddr, path::PathBuf};
|
||||
|
||||
use clap::Parser;
|
||||
use once_cell::sync::Lazy;
|
||||
use rocket::{
|
||||
figment::{providers::Env, Figment},
|
||||
shield::{NoSniff, Shield},
|
||||
|
|
@ -13,6 +14,18 @@ use rust_embed::RustEmbed;
|
|||
mod models;
|
||||
mod routes;
|
||||
|
||||
const BINARY_VERSION: &str =
|
||||
concat!(env!("CARGO_PKG_VERSION"), env!("GIT_HASH"));
|
||||
const SERVER_VERSION: &str = concat!(
|
||||
"bin v.",
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
" (",
|
||||
env!("GIT_HASH"),
|
||||
") (Rocket)"
|
||||
);
|
||||
static BINARY_ETAG: Lazy<String> =
|
||||
Lazy::new(|| sha256::digest(BINARY_VERSION));
|
||||
|
||||
#[derive(RustEmbed)]
|
||||
#[folder = "templates/"]
|
||||
struct EmbeddedTemplates;
|
||||
|
|
@ -91,7 +104,7 @@ fn rocket() -> _ {
|
|||
routes::retrieve::retrieve,
|
||||
routes::retrieve::retrieve_ext,
|
||||
routes::pretty_retrieve::pretty_retrieve,
|
||||
routes::pretty_retrieve_ext::pretty_retrieve_ext
|
||||
routes::pretty_retrieve::pretty_retrieve_ext
|
||||
],
|
||||
)
|
||||
.attach(shield)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue