person_mention_view::PersonMentionQuery,
structs::{CommentReplyView, PersonMentionView},
};
-use lemmy_utils::{claims::Claims, error::LemmyError, utils::markdown::markdown_to_html};
+use lemmy_utils::{
+ cache_header::cache_1hour,
+ claims::Claims,
+ error::LemmyError,
+ utils::markdown::markdown_to_html,
+};
use once_cell::sync::Lazy;
use rss::{
extension::dublincore::DublinCoreExtensionBuilder,
}
pub fn config(cfg: &mut web::ServiceConfig) {
- cfg
- .route("/feeds/{type}/{name}.xml", web::get().to(get_feed))
- .route("/feeds/all.xml", web::get().to(get_all_feed))
- .route("/feeds/local.xml", web::get().to(get_local_feed));
+ cfg.service(
+ web::scope("/feeds")
+ .route("/{type}/{name}.xml", web::get().to(get_feed))
+ .route("/all.xml", web::get().to(get_all_feed).wrap(cache_1hour()))
+ .route(
+ "/local.xml",
+ web::get().to(get_local_feed).wrap(cache_1hour()),
+ ),
+ );
}
static RSS_NAMESPACE: Lazy<BTreeMap<String, String>> = Lazy::new(|| {
use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::RegistrationMode;
use lemmy_db_views::structs::SiteView;
-use lemmy_utils::{error::LemmyError, version};
+use lemmy_utils::{
+ cache_header::{cache_1hour, cache_3days},
+ error::LemmyError,
+ version,
+};
use serde::{Deserialize, Serialize};
use url::Url;
pub fn config(cfg: &mut web::ServiceConfig) {
cfg
- .route("/nodeinfo/2.0.json", web::get().to(node_info))
- .route("/.well-known/nodeinfo", web::get().to(node_info_well_known));
+ .route(
+ "/nodeinfo/2.0.json",
+ web::get().to(node_info).wrap(cache_1hour()),
+ )
+ .route(
+ "/.well-known/nodeinfo",
+ web::get().to(node_info_well_known).wrap(cache_3days()),
+ );
}
async fn node_info_well_known(
source::{community::Community, person::Person},
traits::ApubActor,
};
-use lemmy_utils::error::LemmyError;
+use lemmy_utils::{cache_header::cache_3days, error::LemmyError};
use serde::Deserialize;
use std::collections::HashMap;
use url::Url;
pub fn config(cfg: &mut web::ServiceConfig) {
cfg.route(
".well-known/webfinger",
- web::get().to(get_webfinger_response),
+ web::get().to(get_webfinger_response).wrap(cache_3days()),
);
}
--- /dev/null
+use actix_web::middleware::DefaultHeaders;
+
+/// Adds a cache header to requests
+///
+/// Common cache amounts are:
+/// * 1 hour = 60s * 60m = `3600` seconds
+/// * 3 days = 60s * 60m * 24h * 3d = `259200` seconds
+///
+/// Mastodon & other activitypub server defaults to 3d
+pub fn cache_header(seconds: usize) -> DefaultHeaders {
+ DefaultHeaders::new().add(("Cache-Control", format!("public, max-age={seconds}")))
+}
+
+/// Set a 1 hour cache time
+pub fn cache_1hour() -> DefaultHeaders {
+ cache_header(3600)
+}
+
+/// Set a 3 day cache time
+pub fn cache_3days() -> DefaultHeaders {
+ cache_header(259200)
+}
extern crate smart_default;
pub mod apub;
+pub mod cache_header;
pub mod email;
pub mod rate_limit;
pub mod settings;