disable caching

This commit is contained in:
Dietrich 2021-02-05 19:10:41 +01:00
parent 5d354800b5
commit 051d55a936
Signed by: dietrich
GPG Key ID: 9F3C20C0F85DF67C
2 changed files with 36 additions and 46 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
/target /target
.env
links.db

View File

@ -1,6 +1,11 @@
use std::time::SystemTime;
use actix_identity::Identity; use actix_identity::Identity;
use actix_web::{web, HttpResponse}; use actix_web::{
http::header::{CacheControl, CacheDirective, Expires},
web, HttpResponse,
};
use qrcodegen::{QrCode, QrCodeEcc}; use qrcodegen::{QrCode, QrCodeEcc};
use crate::ServerError; use crate::ServerError;
@ -29,6 +34,18 @@ fn establish_connection() -> Result<SqliteConnection, ServerError> {
} }
} }
fn redirect_builder(target: &str) -> HttpResponse {
HttpResponse::TemporaryRedirect()
.set(CacheControl(vec![
CacheDirective::NoCache,
CacheDirective::NoStore,
CacheDirective::MustRevalidate,
]))
.set(Expires(SystemTime::now().into()))
.set_header(actix_web::http::header::LOCATION, target.clone())
.body(format!("Redirect to {}", target.clone()))
}
/// Show the list of all available links if a user is authenticated /// Show the list of all available links if a user is authenticated
pub(crate) async fn index( pub(crate) async fn index(
tera: web::Data<Tera>, tera: web::Data<Tera>,
@ -48,9 +65,7 @@ pub(crate) async fn index(
let rendered = tera.render("index.html", &data)?; let rendered = tera.render("index.html", &data)?;
Ok(HttpResponse::Ok().body(rendered)) Ok(HttpResponse::Ok().body(rendered))
} else { } else {
Ok(HttpResponse::TemporaryRedirect() Ok(redirect_builder("/admin/login/"))
.set_header(actix_web::http::header::LOCATION, "/admin/login/")
.body("Redirect to /admin/login/"))
} }
} }
@ -83,9 +98,7 @@ pub(crate) async fn view_link(
let rendered = tera.render("view_link.html", &data)?; let rendered = tera.render("view_link.html", &data)?;
Ok(HttpResponse::Ok().body(rendered)) Ok(HttpResponse::Ok().body(rendered))
} else { } else {
Ok(HttpResponse::TemporaryRedirect() Ok(redirect_builder("/admin/login/"))
.set_header(actix_web::http::header::LOCATION, "/admin/login/")
.body("Redirect to /admin/login/"))
} }
} }
@ -101,9 +114,7 @@ pub(crate) async fn signup(
let rendered = tera.render("signup.html", &data)?; let rendered = tera.render("signup.html", &data)?;
Ok(HttpResponse::Ok().body(rendered)) Ok(HttpResponse::Ok().body(rendered))
} else { } else {
Ok(HttpResponse::TemporaryRedirect() Ok(redirect_builder("/admin/login/"))
.set_header(actix_web::http::header::LOCATION, "/admin/login/")
.body("Redirect to /admin/login/"))
} }
} }
@ -128,9 +139,7 @@ pub(crate) async fn process_signup(
println!("{:?}", data); println!("{:?}", data);
Ok(HttpResponse::Ok().body(format!("Successfully saved user: {}", data.username))) Ok(HttpResponse::Ok().body(format!("Successfully saved user: {}", data.username)))
} else { } else {
Ok(HttpResponse::TemporaryRedirect() Ok(redirect_builder("/admin/login/"))
.set_header(actix_web::http::header::LOCATION, "/admin/login/")
.body("Redirect to /admin/login/"))
} }
} }
@ -142,9 +151,7 @@ pub(crate) async fn login(
data.insert("title", "Login"); data.insert("title", "Login");
if let Some(_id) = id.identity() { if let Some(_id) = id.identity() {
return Ok(HttpResponse::TemporaryRedirect() return Ok(redirect_builder("/admin/index/"));
.set_header(actix_web::http::header::LOCATION, "/admin/index/")
.body("Redirect to /admin/index/"));
} }
let rendered = tera.render("login.html", &data)?; let rendered = tera.render("login.html", &data)?;
@ -177,26 +184,18 @@ pub(crate) async fn process_login(
let session_token = u.username; let session_token = u.username;
id.remember(session_token); id.remember(session_token);
Ok(HttpResponse::TemporaryRedirect() Ok(redirect_builder("/admin/index/"))
.set_header(actix_web::http::header::LOCATION, "/admin/index/")
.body("Redirect to /admin/index/"))
} else { } else {
Ok(HttpResponse::TemporaryRedirect() Ok(redirect_builder("/admin/login/"))
.set_header(actix_web::http::header::LOCATION, "/admin/login/")
.body("Redirect to /admin/login/"))
} }
} }
Err(_e) => Ok(HttpResponse::TemporaryRedirect() Err(_e) => Ok(redirect_builder("/admin/login/")),
.set_header(actix_web::http::header::LOCATION, "/admin/login/")
.body("Redirect to /admin/login/")),
} }
} }
pub(crate) async fn logout(id: Identity) -> Result<HttpResponse, ServerError> { pub(crate) async fn logout(id: Identity) -> Result<HttpResponse, ServerError> {
id.forget(); id.forget();
Ok(HttpResponse::TemporaryRedirect() Ok(redirect_builder("/admin/login/"))
.set_header(actix_web::http::header::LOCATION, "/admin/login/")
.body("Redirect to /admin/login/"))
} }
pub(crate) async fn redirect( pub(crate) async fn redirect(
@ -208,9 +207,7 @@ pub(crate) async fn redirect(
let link = links.filter(code.eq(&data.0)).first::<Link>(&connection); let link = links.filter(code.eq(&data.0)).first::<Link>(&connection);
match link { match link {
Ok(link) => Ok(HttpResponse::TemporaryRedirect() Ok(link) => Ok(redirect_builder(&link.target)),
.set_header(actix_web::http::header::LOCATION, link.target.clone())
.body(format!("Redirect to {}", link.target))),
Err(NotFound) => { Err(NotFound) => {
let mut data = Context::new(); let mut data = Context::new();
data.insert("title", "Wurde gelöscht"); data.insert("title", "Wurde gelöscht");
@ -222,10 +219,9 @@ pub(crate) async fn redirect(
} }
pub(crate) async fn redirect_fhs() -> Result<HttpResponse, ServerError> { pub(crate) async fn redirect_fhs() -> Result<HttpResponse, ServerError> {
Ok(HttpResponse::TemporaryRedirect().set_header( Ok(redirect_builder(
actix_web::http::header::LOCATION,
"https://www.freie-hochschule-stuttgart.de", "https://www.freie-hochschule-stuttgart.de",
).body("If you are not redirected automatically go to https://www.freie-hochschule-stuttgart.de")) ))
} }
pub(crate) async fn submission( pub(crate) async fn submission(
@ -240,9 +236,7 @@ pub(crate) async fn submission(
let rendered = tera.render("submission.html", &data)?; let rendered = tera.render("submission.html", &data)?;
return Ok(HttpResponse::Ok().body(rendered)); return Ok(HttpResponse::Ok().body(rendered));
} }
Ok(HttpResponse::TemporaryRedirect() Ok(redirect_builder("/admin/login/"))
.set_header(actix_web::http::header::LOCATION, "/admin/login/")
.body("Redirect to /admin/login/"))
} }
pub(crate) async fn process_submission( pub(crate) async fn process_submission(
@ -265,17 +259,11 @@ pub(crate) async fn process_submission(
.values(&new_post) .values(&new_post)
.execute(&connection)?; .execute(&connection)?;
return Ok(HttpResponse::TemporaryRedirect() return Ok(redirect_builder("/admin/index/"));
.set_header(actix_web::http::header::LOCATION, "/admin/index/")
.body("Redirect to /admin/index/"));
} }
Err(_e) => Ok(HttpResponse::TemporaryRedirect() Err(_e) => Ok(redirect_builder("/admin/login/")),
.set_header(actix_web::http::header::LOCATION, "/admin/login/")
.body("Redirect to /admin/login/")),
} }
} else { } else {
Ok(HttpResponse::TemporaryRedirect() Ok(redirect_builder("/admin/login/"))
.set_header(actix_web::http::header::LOCATION, "/admin/login/")
.body("Redirect to /admin/login/"))
} }
} }