Skip to content

Commit

Permalink
new: Made the Cache-Control headers fully configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
SinTan1729 committed Jun 1, 2024
1 parent 69fc25a commit 96495b0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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).
9 changes: 4 additions & 5 deletions actix/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 96495b0

Please sign in to comment.