summaryrefslogtreecommitdiffstats
path: root/server/src
diff options
context:
space:
mode:
authorDessalines <tyhou13@gmx.com>2020-06-02 14:03:38 -0400
committerDessalines <tyhou13@gmx.com>2020-06-02 14:03:38 -0400
commita8160288aea093067f0e930b571a718d3bfbb370 (patch)
tree0508243752a28bbb5f5ed4b72586ace7a5ea4b67 /server/src
parent0c2905fd2dfae48087b2f70d0d7c056ff2ffb737 (diff)
parent21e913192a8355fe64220ef168cef901f1af4258 (diff)
Merge remote-tracking branch 'yerba/cache-control'
Diffstat (limited to 'server/src')
-rw-r--r--server/src/main.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/server/src/main.rs b/server/src/main.rs
index 4e773ee5..f894dc54 100644
--- a/server/src/main.rs
+++ b/server/src/main.rs
@@ -1,8 +1,15 @@
extern crate lemmy_server;
#[macro_use]
extern crate diesel_migrations;
+#[macro_use]
+pub extern crate lazy_static;
+use crate::lemmy_server::actix_web::dev::Service;
use actix::prelude::*;
+use actix_web::body::Body;
+use actix_web::dev::{ServiceRequest, ServiceResponse};
+use actix_web::http::header::CONTENT_TYPE;
+use actix_web::http::{header::CACHE_CONTROL, HeaderValue};
use actix_web::*;
use diesel::r2d2::{ConnectionManager, Pool};
use diesel::PgConnection;
@@ -12,9 +19,16 @@ use lemmy_server::{
settings::Settings,
websocket::server::*,
};
+use regex::Regex;
use std::{io, sync::Arc};
use tokio::sync::Mutex;
+lazy_static! {
+ static ref CACHE_CONTROL_REGEX: Regex =
+ Regex::new("^((text|image)/.+|application/javascript)$").unwrap();
+ static ref CACHE_CONTROL_VALUE: String = format!("public, max-age={}", 365 * 24 * 60 * 60);
+}
+
embed_migrations!();
#[actix_rt::main]
@@ -51,6 +65,7 @@ async fn main() -> io::Result<()> {
let settings = Settings::get();
let rate_limiter = rate_limiter.clone();
App::new()
+ .wrap_fn(add_cache_headers)
.wrap(middleware::Logger::default())
.data(pool.clone())
.data(server.clone())
@@ -75,3 +90,23 @@ async fn main() -> io::Result<()> {
.run()
.await
}
+
+fn add_cache_headers<S>(
+ req: ServiceRequest,
+ srv: &mut S,
+) -> impl Future<Output = Result<ServiceResponse, Error>>
+where
+ S: Service<Request = ServiceRequest, Response = ServiceResponse<Body>, Error = Error>,
+{
+ let fut = srv.call(req);
+ async move {
+ let mut res = fut.await?;
+ if let Some(content_type) = res.headers().get(CONTENT_TYPE) {
+ if CACHE_CONTROL_REGEX.is_match(content_type.to_str().unwrap()) {
+ let header_val = HeaderValue::from_static(&CACHE_CONTROL_VALUE);
+ res.headers_mut().insert(CACHE_CONTROL, header_val);
+ }
+ }
+ Ok(res)
+ }
+}