forked from nutomic/lemmy
WIP: add cache-control headers
This commit is contained in:
parent
02bcbc42d6
commit
cb928b037e
7 changed files with 33 additions and 1 deletions
|
@ -1,10 +1,12 @@
|
||||||
use crate::apub::make_apub_endpoint;
|
use crate::apub::make_apub_endpoint;
|
||||||
|
use crate::constants::CACHE_INTERVAL_FEDERATION;
|
||||||
use crate::db::community::Community;
|
use crate::db::community::Community;
|
||||||
use crate::db::community_view::CommunityFollowerView;
|
use crate::db::community_view::CommunityFollowerView;
|
||||||
use crate::db::establish_connection;
|
use crate::db::establish_connection;
|
||||||
use crate::to_datetime_utc;
|
use crate::to_datetime_utc;
|
||||||
use activitypub::{actor::Group, collection::UnorderedCollection, context};
|
use activitypub::{actor::Group, collection::UnorderedCollection, context};
|
||||||
use actix_web::body::Body;
|
use actix_web::body::Body;
|
||||||
|
use actix_web::http::header::{CacheControl, CacheDirective};
|
||||||
use actix_web::web::Path;
|
use actix_web::web::Path;
|
||||||
use actix_web::HttpResponse;
|
use actix_web::HttpResponse;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
@ -90,6 +92,9 @@ pub async fn get_apub_community(info: Path<CommunityQuery>) -> HttpResponse<Body
|
||||||
if let Ok(community) = Community::read_from_name(&connection, info.community_name.to_owned()) {
|
if let Ok(community) = Community::read_from_name(&connection, info.community_name.to_owned()) {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.content_type("application/activity+json")
|
.content_type("application/activity+json")
|
||||||
|
.set(CacheControl(vec![CacheDirective::MaxAge(
|
||||||
|
CACHE_INTERVAL_FEDERATION.num_seconds() as u32,
|
||||||
|
)]))
|
||||||
.body(serde_json::to_string(&community.as_group()).unwrap())
|
.body(serde_json::to_string(&community.as_group()).unwrap())
|
||||||
} else {
|
} else {
|
||||||
HttpResponse::NotFound().finish()
|
HttpResponse::NotFound().finish()
|
||||||
|
@ -102,6 +107,9 @@ pub async fn get_apub_community_followers(info: Path<CommunityQuery>) -> HttpRes
|
||||||
if let Ok(community) = Community::read_from_name(&connection, info.community_name.to_owned()) {
|
if let Ok(community) = Community::read_from_name(&connection, info.community_name.to_owned()) {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.content_type("application/activity+json")
|
.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())
|
.body(serde_json::to_string(&community.followers_as_collection()).unwrap())
|
||||||
} else {
|
} else {
|
||||||
HttpResponse::NotFound().finish()
|
HttpResponse::NotFound().finish()
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
use crate::apub::make_apub_endpoint;
|
use crate::apub::make_apub_endpoint;
|
||||||
|
use crate::constants::CACHE_INTERVAL_FEDERATION;
|
||||||
use crate::db::establish_connection;
|
use crate::db::establish_connection;
|
||||||
use crate::db::user::User_;
|
use crate::db::user::User_;
|
||||||
use crate::to_datetime_utc;
|
use crate::to_datetime_utc;
|
||||||
use activitypub::{actor::Person, context};
|
use activitypub::{actor::Person, context};
|
||||||
use actix_web::body::Body;
|
use actix_web::body::Body;
|
||||||
|
use actix_web::http::header::{CacheControl, CacheDirective};
|
||||||
use actix_web::web::Path;
|
use actix_web::web::Path;
|
||||||
use actix_web::HttpResponse;
|
use actix_web::HttpResponse;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
@ -67,6 +69,9 @@ pub async fn get_apub_user(info: Path<UserQuery>) -> HttpResponse<Body> {
|
||||||
if let Ok(user) = User_::find_by_email_or_username(&connection, &info.user_name) {
|
if let Ok(user) = User_::find_by_email_or_username(&connection, &info.user_name) {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.content_type("application/activity+json")
|
.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())
|
.body(serde_json::to_string(&user.as_person()).unwrap())
|
||||||
} else {
|
} else {
|
||||||
HttpResponse::NotFound().finish()
|
HttpResponse::NotFound().finish()
|
||||||
|
|
6
server/src/constants.rs
Normal file
6
server/src/constants.rs
Normal file
|
@ -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);
|
|
@ -24,6 +24,7 @@ pub extern crate strum;
|
||||||
|
|
||||||
pub mod api;
|
pub mod api;
|
||||||
pub mod apub;
|
pub mod apub;
|
||||||
|
pub mod constants;
|
||||||
pub mod db;
|
pub mod db;
|
||||||
pub mod routes;
|
pub mod routes;
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::apub;
|
use crate::apub;
|
||||||
|
use actix_web::http::header::{CacheControl, CacheDirective};
|
||||||
use actix_web::web;
|
use actix_web::web;
|
||||||
|
|
||||||
pub fn config(cfg: &mut web::ServiceConfig) {
|
pub fn config(cfg: &mut web::ServiceConfig) {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
extern crate rss;
|
extern crate rss;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::constants::CACHE_INTERVAL_FEEDS;
|
||||||
use crate::db::comment_view::{ReplyQueryBuilder, ReplyView};
|
use crate::db::comment_view::{ReplyQueryBuilder, ReplyView};
|
||||||
use crate::db::community::Community;
|
use crate::db::community::Community;
|
||||||
use crate::db::post_view::{PostQueryBuilder, PostView};
|
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::db::{establish_connection, ListingType, SortType};
|
||||||
use crate::Settings;
|
use crate::Settings;
|
||||||
use actix_web::body::Body;
|
use actix_web::body::Body;
|
||||||
|
use actix_web::http::header::{CacheControl, CacheDirective};
|
||||||
use actix_web::{web, HttpResponse, Result};
|
use actix_web::{web, HttpResponse, Result};
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use failure::Error;
|
use failure::Error;
|
||||||
|
@ -81,6 +83,9 @@ async fn get_feed(
|
||||||
|
|
||||||
match feed_result {
|
match feed_result {
|
||||||
Ok(rss) => HttpResponse::Ok()
|
Ok(rss) => HttpResponse::Ok()
|
||||||
|
.set(CacheControl(vec![CacheDirective::MaxAge(
|
||||||
|
CACHE_INTERVAL_FEEDS.num_seconds() as u32,
|
||||||
|
)]))
|
||||||
.content_type("application/rss+xml")
|
.content_type("application/rss+xml")
|
||||||
.body(rss),
|
.body(rss),
|
||||||
Err(_) => HttpResponse::NotFound().finish(),
|
Err(_) => HttpResponse::NotFound().finish(),
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
|
use crate::constants::CACHE_INTERVAL_FRONTEND;
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
|
use actix::Response;
|
||||||
use actix_files::NamedFile;
|
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) {
|
pub fn config(cfg: &mut web::ServiceConfig) {
|
||||||
cfg
|
cfg
|
||||||
|
@ -39,6 +44,7 @@ pub fn config(cfg: &mut web::ServiceConfig) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn index() -> Result<NamedFile, actix_web::error::Error> {
|
async fn index() -> Result<NamedFile, actix_web::error::Error> {
|
||||||
|
// TODO: figure out how to set cache-control header here
|
||||||
Ok(NamedFile::open(
|
Ok(NamedFile::open(
|
||||||
Settings::get().front_end_dir.to_owned() + "/index.html",
|
Settings::get().front_end_dir.to_owned() + "/index.html",
|
||||||
)?)
|
)?)
|
||||||
|
|
Loading…
Reference in a new issue