diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index fac6088e..eb2b4c2a 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -1,10 +1,12 @@ use crate::apub::make_apub_endpoint; +use crate::constants::CACHE_INTERVAL_FEDERATION; use crate::db::community::Community; use crate::db::community_view::CommunityFollowerView; use crate::db::establish_connection; use crate::to_datetime_utc; use activitypub::{actor::Group, collection::UnorderedCollection, context}; use actix_web::body::Body; +use actix_web::http::header::{CacheControl, CacheDirective}; use actix_web::web::Path; use actix_web::HttpResponse; use serde::Deserialize; @@ -90,6 +92,9 @@ pub async fn get_apub_community(info: Path) -> HttpResponse) -> HttpRes if let Ok(community) = Community::read_from_name(&connection, info.community_name.to_owned()) { HttpResponse::Ok() .content_type("application/activity+json") + .set(CacheControl(vec![CacheDirective::MaxAge( + CACHE_INTERVAL_FEDERATION.num_seconds() as u32, + )])) .body(serde_json::to_string(&community.followers_as_collection()).unwrap()) } else { HttpResponse::NotFound().finish() diff --git a/server/src/apub/user.rs b/server/src/apub/user.rs index cf9a9797..d42c00eb 100644 --- a/server/src/apub/user.rs +++ b/server/src/apub/user.rs @@ -1,9 +1,11 @@ use crate::apub::make_apub_endpoint; +use crate::constants::CACHE_INTERVAL_FEDERATION; use crate::db::establish_connection; use crate::db::user::User_; use crate::to_datetime_utc; use activitypub::{actor::Person, context}; use actix_web::body::Body; +use actix_web::http::header::{CacheControl, CacheDirective}; use actix_web::web::Path; use actix_web::HttpResponse; use serde::Deserialize; @@ -67,6 +69,9 @@ pub async fn get_apub_user(info: Path) -> HttpResponse { if let Ok(user) = User_::find_by_email_or_username(&connection, &info.user_name) { HttpResponse::Ok() .content_type("application/activity+json") + .set(CacheControl(vec![CacheDirective::MaxAge( + CACHE_INTERVAL_FEDERATION.num_seconds() as u32, + )])) .body(serde_json::to_string(&user.as_person()).unwrap()) } else { HttpResponse::NotFound().finish() diff --git a/server/src/constants.rs b/server/src/constants.rs new file mode 100644 index 00000000..4b5ace66 --- /dev/null +++ b/server/src/constants.rs @@ -0,0 +1,6 @@ +use chrono::Duration; + +// TODO: should all be 0 during debug +pub static CACHE_INTERVAL_FEEDS: Duration = Duration::minutes(15); +pub static CACHE_INTERVAL_FEDERATION: Duration = Duration::minutes(1); +pub static CACHE_INTERVAL_FRONTEND: Duration = Duration::hours(2); diff --git a/server/src/lib.rs b/server/src/lib.rs index e23ec4ba..1c647708 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -24,6 +24,7 @@ pub extern crate strum; pub mod api; pub mod apub; +pub mod constants; pub mod db; pub mod routes; pub mod schema; diff --git a/server/src/routes/federation.rs b/server/src/routes/federation.rs index ea6039d6..af3cb915 100644 --- a/server/src/routes/federation.rs +++ b/server/src/routes/federation.rs @@ -1,4 +1,5 @@ use crate::apub; +use actix_web::http::header::{CacheControl, CacheDirective}; use actix_web::web; pub fn config(cfg: &mut web::ServiceConfig) { diff --git a/server/src/routes/feeds.rs b/server/src/routes/feeds.rs index ae1631e2..13764d45 100644 --- a/server/src/routes/feeds.rs +++ b/server/src/routes/feeds.rs @@ -1,6 +1,7 @@ extern crate rss; use super::*; +use crate::constants::CACHE_INTERVAL_FEEDS; use crate::db::comment_view::{ReplyQueryBuilder, ReplyView}; use crate::db::community::Community; use crate::db::post_view::{PostQueryBuilder, PostView}; @@ -10,6 +11,7 @@ use crate::db::user_mention_view::{UserMentionQueryBuilder, UserMentionView}; use crate::db::{establish_connection, ListingType, SortType}; use crate::Settings; use actix_web::body::Body; +use actix_web::http::header::{CacheControl, CacheDirective}; use actix_web::{web, HttpResponse, Result}; use chrono::{DateTime, Utc}; use failure::Error; @@ -81,6 +83,9 @@ async fn get_feed( match feed_result { Ok(rss) => HttpResponse::Ok() + .set(CacheControl(vec![CacheDirective::MaxAge( + CACHE_INTERVAL_FEEDS.num_seconds() as u32, + )])) .content_type("application/rss+xml") .body(rss), Err(_) => HttpResponse::NotFound().finish(), diff --git a/server/src/routes/index.rs b/server/src/routes/index.rs index 2453a1b2..b4c463a4 100644 --- a/server/src/routes/index.rs +++ b/server/src/routes/index.rs @@ -1,6 +1,11 @@ +use crate::constants::CACHE_INTERVAL_FRONTEND; use crate::settings::Settings; +use actix::Response; use actix_files::NamedFile; -use actix_web::web; +use actix_web::body::Body; +use actix_web::http::header::{CacheControl, CacheDirective}; +use actix_web::middleware; +use actix_web::{web, HttpRequest, HttpResponse}; pub fn config(cfg: &mut web::ServiceConfig) { cfg @@ -39,6 +44,7 @@ pub fn config(cfg: &mut web::ServiceConfig) { } async fn index() -> Result { + // TODO: figure out how to set cache-control header here Ok(NamedFile::open( Settings::get().front_end_dir.to_owned() + "/index.html", )?)