From 96495b037da903213b8899f84094b2910a0882d5 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Fri, 31 May 2024 20:30:09 -0500 Subject: [PATCH] new: Made the Cache-Control headers fully configurable --- README.md | 10 ++++++---- actix/src/main.rs | 9 ++++----- compose.yaml | 7 ++++--- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index a1046ca..ebc7dfa 100644 --- a/README.md +++ b/README.md @@ -133,9 +133,11 @@ the `slug_length` variable. It defaults to 8, and a minimum of 4 is supported. To enable public mode, set `public_mode` to `Enable`. With this, anyone will be able to add links. Listing existing links or deleting links will need admin access using the password. -By default, the server sends `no-cache` and `private` Cache-Control headers. To disable those, -set `cache_control_header` to `Disable`. It might help boost performance if served through a -proxy. +By default, the server sends no Cache-Control headers. You can set custom `cache_control_header` +to send your desired headers. It must be a comma separated list of valid +[RFC 7234 §5.2](https://datatracker.ietf.org/doc/html/rfc7234#section-5.2) headers. For example, +you can set it to `no-cache, private` to disable caching. It might help during testing if +served through a proxy. ## Instructions for CLI usage The application can be used from the terminal using something like `curl`. In all the examples @@ -179,6 +181,6 @@ pointing to illegal content. Since there are no logs, it's impossible to prove that those links aren't created by you. ## Notes -- It started as a fork of [this project](https://gitlab.com/draganczukp/simply-shorten). +- It started as a fork of [`simply-shorten`](https://gitlab.com/draganczukp/simply-shorten). - The list of adjectives and names used for random short url generation is a modified version of [this list used by docker](https://github.com/moby/moby/blob/master/pkg/namesgenerator/names-generator.go). diff --git a/actix/src/main.rs b/actix/src/main.rs index 09a08b9..0f914b7 100644 --- a/actix/src/main.rs +++ b/actix/src/main.rs @@ -36,8 +36,7 @@ async fn main() -> Result<()> { let cache_control_header = env::var("cache_control_header") .ok() - .filter(|s| !s.trim().is_empty()) - .unwrap_or(String::from("Enable")); + .filter(|s| !s.trim().is_empty()); // Actually start the server HttpServer::new(move || { @@ -54,10 +53,10 @@ async fn main() -> Result<()> { .app_data(web::Data::new(AppState { db: database::open_db(db_location.clone()), })) - .wrap(if cache_control_header == "Disable" { - middleware::DefaultHeaders::new() + .wrap(if let Some(header) = &cache_control_header { + middleware::DefaultHeaders::new().add(("Cache-Control", header.to_owned())) } else { - middleware::DefaultHeaders::new().add(("Cache-Control", "no-cache, private")) + middleware::DefaultHeaders::new() }) .service(services::link_handler) .service(services::getall) diff --git a/compose.yaml b/compose.yaml index 3eeec8e..deb968a 100644 --- a/compose.yaml +++ b/compose.yaml @@ -36,9 +36,10 @@ services: # delete, or listing), change the following option to Enable. # - public_mode=Disable - # By default, the server sends `no-cache` and `private` Cache-Control - # headers. To disable those, change the following option to Disable. - # - cache_control_header=Enable + # By default, the server sends no Cache-Control headers. You can supply a + # comma separated list of valid header as per RFC 7234 §5.2 to send those + # headers instead. + # - cache_control_header=no-cache, private volumes: - db:/urls.sqlite networks: