From 2054b6835f76cb7d4a3502506d2d7ae83bb2d8cc Mon Sep 17 00:00:00 2001
From: Philippe Loctaux
Date: Tue, 7 Mar 2023 19:22:44 +0100
Subject: [PATCH] ezidam: cache for one hour css and js files
---
crates/ezidam/src/cache.rs | 46 +++++++++++++++++++++++++++++
crates/ezidam/src/lib.rs | 5 ++++
crates/ezidam/src/response_timer.rs | 6 ++--
3 files changed, 54 insertions(+), 3 deletions(-)
create mode 100644 crates/ezidam/src/cache.rs
diff --git a/crates/ezidam/src/cache.rs b/crates/ezidam/src/cache.rs
new file mode 100644
index 0000000..1af6fba
--- /dev/null
+++ b/crates/ezidam/src/cache.rs
@@ -0,0 +1,46 @@
+use rocket::fairing::{self, Fairing};
+use rocket::http::{ContentType, Header};
+use rocket::{Build, Request, Response, Rocket};
+
+#[derive(Debug)]
+pub struct CacheControl {
+ max_age: u32,
+ cache_types: Vec,
+}
+
+impl Default for CacheControl {
+ fn default() -> Self {
+ CacheControl {
+ // 60 secs * 60 minutes
+ max_age: 60 * 60,
+ cache_types: vec![ContentType::CSS, ContentType::JavaScript],
+ }
+ }
+}
+
+impl CacheControl {
+ pub fn rocket(rocket_builder: Rocket) -> Rocket {
+ rocket_builder.attach(Self::default())
+ }
+}
+
+#[rocket::async_trait]
+impl Fairing for CacheControl {
+ fn info(&self) -> fairing::Info {
+ fairing::Info {
+ name: "Cache Control",
+ kind: fairing::Kind::Response,
+ }
+ }
+
+ async fn on_response<'r>(&self, _request: &'r Request<'_>, response: &mut Response<'r>) {
+ if let Some(content_type) = response.content_type() {
+ if self.cache_types.contains(&content_type) {
+ response.set_header(Header::new(
+ "Cache-Control",
+ format!("public, max-age={}", self.max_age),
+ ));
+ }
+ }
+ }
+}
diff --git a/crates/ezidam/src/lib.rs b/crates/ezidam/src/lib.rs
index 9abf03f..41b30cd 100644
--- a/crates/ezidam/src/lib.rs
+++ b/crates/ezidam/src/lib.rs
@@ -1,5 +1,6 @@
use rocket::{Build, Rocket};
+mod cache;
mod database;
mod error;
mod file_from_bytes;
@@ -13,6 +14,7 @@ mod shutdown;
mod tests;
pub fn rocket_setup(rocket_builder: Rocket) -> Rocket {
+ use cache::CacheControl;
use database::Database;
use error::catchers;
use response_timer::ResponseTimer;
@@ -22,6 +24,9 @@ pub fn rocket_setup(rocket_builder: Rocket) -> Rocket {
// Response timer
let rocket_builder = ResponseTimer::rocket(rocket_builder);
+ // Cache control
+ let rocket_builder = CacheControl::rocket(rocket_builder);
+
// Database
let rocket_builder = Database::rocket(rocket_builder);
diff --git a/crates/ezidam/src/response_timer.rs b/crates/ezidam/src/response_timer.rs
index 3d00d1f..2133e71 100644
--- a/crates/ezidam/src/response_timer.rs
+++ b/crates/ezidam/src/response_timer.rs
@@ -1,7 +1,7 @@
// Code stolen from https://crates.io/crates/rocket-response-time
// Copyright Thomas Parsley , MIT License
-use rocket::fairing::{Fairing, Kind};
+use rocket::fairing::{self, Fairing, Kind};
use rocket::{Build, Request, Rocket};
pub struct ResponseTimer {}
@@ -14,8 +14,8 @@ impl ResponseTimer {
#[rocket::async_trait]
impl Fairing for ResponseTimer {
- fn info(&self) -> rocket::fairing::Info {
- rocket::fairing::Info {
+ fn info(&self) -> fairing::Info {
+ fairing::Info {
name: "ResponseTimer",
kind: Kind::Request | Kind::Response,
}