Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set audit index document cache max age #212

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions clowarden-server/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ use tower_http::{
};
use tracing::{error, instrument, trace};

/// Audit index HTML document cache duration.
const AUDIT_INDEX_CACHE_MAX_AGE: usize = 300;

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

Expand Down Expand Up @@ -73,8 +76,12 @@ pub(crate) fn setup_router(
let audit_path = Path::new(&static_path).join("audit");
let audit_index_path = audit_path.join("index.html");

// Setup webhook secret
let webhook_secret = cfg.get_string("server.githubApp.webhookSecret").unwrap();
// Setup audit index handler
let audit_index = SetResponseHeader::overriding(
ServeFile::new(audit_index_path),
CACHE_CONTROL,
HeaderValue::try_from(format!("max-age={AUDIT_INDEX_CACHE_MAX_AGE}"))?,
);

// Setup audit router
let mut audit_router = Router::new()
Expand All @@ -88,8 +95,8 @@ pub(crate) fn setup_router(
HeaderValue::try_from(format!("max-age={STATIC_CACHE_MAX_AGE}"))?,
)),
)
.route("/", get_service(ServeFile::new(&audit_index_path)))
.fallback_service(get_service(ServeFile::new(&audit_index_path)));
.route("/", get_service(audit_index.clone()))
.fallback_service(get_service(audit_index));

// Setup basic auth
if cfg.get_bool("server.basicAuth.enabled").unwrap_or(false) {
Expand All @@ -100,6 +107,7 @@ pub(crate) fn setup_router(

// Setup main router
let orgs = cfg.get("organizations")?;
let webhook_secret = cfg.get_string("server.githubApp.webhookSecret").unwrap();
let router = Router::new()
.route("/webhook/github", post(event))
.route("/health-check", get(health_check))
Expand Down