Add option for temporary redirection

This commit is contained in:
SinTan1729 2023-04-28 00:22:30 -05:00
parent ca01676916
commit f3984624d9
3 changed files with 17 additions and 2 deletions

View File

@ -87,7 +87,7 @@ export site_url=<url>
cd actix cd actix
cargo run cargo run
``` ```
You can optionally set the port the server listens on by appending `--port=[port]` You can optionally set the port the server listens on by appending `--port=[port]`.
### 4. Navigate to `http://localhost:4567` in your browser, add links as you wish. ### 4. Navigate to `http://localhost:4567` in your browser, add links as you wish.
## Running with docker ## Running with docker
@ -122,6 +122,9 @@ docker run -p 4567:4567 \
-d simply-shorten:latest -d simply-shorten:latest
``` ```
You can also set the redirect method to Permanent 308 (default) or Temporary 307 by setting
the `redirect_method` variable to `TEMPORARY` or `PERMANENT` (it's matched exactly).
## Disable authentication ## Disable authentication
If you do not define a password environment variable when starting the docker image, authentication If you do not define a password environment variable when starting the docker image, authentication
will be disabled. will be disabled.

View File

@ -71,8 +71,14 @@ async fn link_handler(shortlink: web::Path<String>, data: web::Data<AppState>) -
if longlink == *"" { if longlink == *"" {
Redirect::to("/err/404").using_status_code(StatusCode::NOT_FOUND) Redirect::to("/err/404").using_status_code(StatusCode::NOT_FOUND)
} else { } else {
let redirect_method = env::var("redirect_method").unwrap_or("PERMANENT".to_string());
database::add_hit(shortlink.as_str(), &data.db); database::add_hit(shortlink.as_str(), &data.db);
Redirect::to(longlink).permanent() if redirect_method == *"TEMPORARY" {
Redirect::to(longlink)
} else {
// Defaults to permanent redirection
Redirect::to(longlink).permanent()
}
} }
} }

View File

@ -10,11 +10,17 @@ services:
# In this case, you can get rid of the db volume below # In this case, you can get rid of the db volume below
# and instead do a mount manually by specifying the location # and instead do a mount manually by specifying the location
# - db_url=/urls.sqlite # - db_url=/urls.sqlite
# Change it in case you want to set the website name # Change it in case you want to set the website name
# displayed in front of the shorturls, defaults to # displayed in front of the shorturls, defaults to
# the hostname you're accessing it from # the hostname you're accessing it from
# - site_url=https://www.example.com # - site_url=https://www.example.com
- password=$3CuReP4S$W0rD - password=$3CuReP4S$W0rD
# Pass the redirect method, if needed TEMPORARY and PERMANENT
# are accepted values, defaults to PERMANENT
# - redirect_method=TEMPORARY
volumes: volumes:
- db:/urls.sqlite - db:/urls.sqlite
networks: networks: