new: Made the Cache-Control headers fully configurable

This commit is contained in:
SinTan1729 2024-05-31 20:30:09 -05:00
parent 69fc25a264
commit 96495b037d
No known key found for this signature in database
GPG Key ID: EB3E68BFBA25C85F
3 changed files with 14 additions and 12 deletions

View File

@ -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 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. 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, By default, the server sends no Cache-Control headers. You can set custom `cache_control_header`
set `cache_control_header` to `Disable`. It might help boost performance if served through a to send your desired headers. It must be a comma separated list of valid
proxy. [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 ## Instructions for CLI usage
The application can be used from the terminal using something like `curl`. In all the examples 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. that those links aren't created by you.
## Notes ## 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 - 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). version of [this list used by docker](https://github.com/moby/moby/blob/master/pkg/namesgenerator/names-generator.go).

View File

@ -36,8 +36,7 @@ async fn main() -> Result<()> {
let cache_control_header = env::var("cache_control_header") let cache_control_header = env::var("cache_control_header")
.ok() .ok()
.filter(|s| !s.trim().is_empty()) .filter(|s| !s.trim().is_empty());
.unwrap_or(String::from("Enable"));
// Actually start the server // Actually start the server
HttpServer::new(move || { HttpServer::new(move || {
@ -54,10 +53,10 @@ async fn main() -> Result<()> {
.app_data(web::Data::new(AppState { .app_data(web::Data::new(AppState {
db: database::open_db(db_location.clone()), db: database::open_db(db_location.clone()),
})) }))
.wrap(if cache_control_header == "Disable" { .wrap(if let Some(header) = &cache_control_header {
middleware::DefaultHeaders::new() middleware::DefaultHeaders::new().add(("Cache-Control", header.to_owned()))
} else { } else {
middleware::DefaultHeaders::new().add(("Cache-Control", "no-cache, private")) middleware::DefaultHeaders::new()
}) })
.service(services::link_handler) .service(services::link_handler)
.service(services::getall) .service(services::getall)

View File

@ -36,9 +36,10 @@ services:
# delete, or listing), change the following option to Enable. # delete, or listing), change the following option to Enable.
# - public_mode=Disable # - public_mode=Disable
# By default, the server sends `no-cache` and `private` Cache-Control # By default, the server sends no Cache-Control headers. You can supply a
# headers. To disable those, change the following option to Disable. # comma separated list of valid header as per RFC 7234 §5.2 to send those
# - cache_control_header=Enable # headers instead.
# - cache_control_header=no-cache, private
volumes: volumes:
- db:/urls.sqlite - db:/urls.sqlite
networks: networks: