From 8d15f1389dada926eb6f41718bdcac94a4a0566c Mon Sep 17 00:00:00 2001 From: akarras Date: Mon, 22 Jan 2024 21:39:00 -0700 Subject: [PATCH] add more cache-control headers to startup requests --- ultros/src/web.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/ultros/src/web.rs b/ultros/src/web.rs index 269f6e15..cde713da 100644 --- a/ultros/src/web.rs +++ b/ultros/src/web.rs @@ -10,7 +10,7 @@ pub(crate) mod sitemap; use anyhow::Error; use axum::body::{Empty, Full}; use axum::extract::{FromRef, Path, Query, State}; -use axum::headers::ContentType; +use axum::headers::{CacheControl, ContentType, HeaderMapExt}; use axum::http::{HeaderValue, Response, StatusCode}; use axum::response::{IntoResponse, Redirect}; use axum::routing::{delete, get, post}; @@ -350,10 +350,13 @@ pub(crate) async fn invite() -> Redirect { pub(crate) async fn world_data( State(world_cache): State>, -) -> Json<&'static WorldData> { +) -> (CacheControl, Json<&'static WorldData>) { static ONCE: OnceLock = OnceLock::new(); let world_data = ONCE.get_or_init(move || WorldData::from(world_cache.as_ref())); - Json(world_data) + ( + CacheControl::new().with_max_age(Duration::from_secs(60 * 60 * 24 * 7)), + Json(world_data), + ) } pub(crate) async fn current_user(user: AuthDiscordUser) -> Json { @@ -818,11 +821,17 @@ async fn get_bincode() -> &'static [u8] { } /// Returns a region- attempts to guess it from the CF Region header -async fn detect_region(region: Option) -> Region { +async fn detect_region(region: Option) -> impl IntoResponse { if region.is_none() { warn!("Unable to detect region"); } - region.unwrap_or(Region::NorthAmerica) + let mut response = region.unwrap_or(Region::NorthAmerica).into_response(); + response.headers_mut().typed_insert( + CacheControl::new() + .with_private() + .with_max_age(Duration::from_secs(604800)), + ); + response } async fn listings_redirect(Path((world, id)): Path<(String, i32)>) -> Redirect {