Skip to content

Commit

Permalink
Set index document cache max age
Browse files Browse the repository at this point in the history
Signed-off-by: Sergio Castaño Arteaga <[email protected]>
  • Loading branch information
tegioz committed May 29, 2024
1 parent b7054c5 commit 406dff9
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions clotributor-apiserver/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ use tower_http::{
};
use tracing::error;

/// Static files cache duration.
const STATIC_CACHE_MAX_AGE: usize = 365 * 24 * 60 * 60;
/// Index HTML document cache duration.
const INDEX_CACHE_MAX_AGE: usize = 300;

/// Default cache duration for some API endpoints.
const DEFAULT_API_MAX_AGE: usize = 300;

/// Static files cache duration.
const STATIC_CACHE_MAX_AGE: usize = 365 * 24 * 60 * 60;

/// Header that indicates the number of items available for pagination purposes.
const PAGINATION_TOTAL_COUNT: &str = "pagination-total-count";

Expand All @@ -43,11 +46,18 @@ pub(crate) fn setup_router(cfg: &Arc<Config>, db: DynDB) -> Result<Router> {
let static_path = cfg.get_string("apiserver.staticPath")?;
let index_path = Path::new(&static_path).join("index.html");

// Setup index handler
let index = SetResponseHeader::overriding(
ServeFile::new(index_path),
CACHE_CONTROL,
HeaderValue::try_from(format!("max-age={INDEX_CACHE_MAX_AGE}"))?,
);

// Setup router
let router = Router::new()
.route("/api/filters/issues", get(issues_filters))
.route("/api/issues/search", get(search_issues))
.route("/", get_service(ServeFile::new(&index_path)))
.route("/", get_service(index.clone()))
.nest_service(
"/static",
get_service(SetResponseHeader::overriding(
Expand All @@ -56,7 +66,7 @@ pub(crate) fn setup_router(cfg: &Arc<Config>, db: DynDB) -> Result<Router> {
HeaderValue::try_from(format!("max-age={STATIC_CACHE_MAX_AGE}"))?,
)),
)
.fallback_service(get_service(ServeFile::new(&index_path)))
.fallback_service(get_service(index))
.layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()))
.with_state(RouterState { db });

Expand Down

0 comments on commit 406dff9

Please sign in to comment.