//! CORS preflight check storage. use super::{CorsCheckResult, Result, SqlitePool}; use tracing::instrument; #[instrument(skip_all)] pub async fn insert_cors_check(pool: &SqlitePool, result: &CorsCheckResult) -> Result { let row = sqlx::query( "INSERT INTO cors_checks (target, url, origin, method, passes, checked_at, error) VALUES (?, ?, ?, ?, ?, ?, ?)", ) .bind(&result.target) .bind(&result.url) .bind(&result.origin) .bind(&result.method) .bind(result.passes) .bind(&result.checked_at) .bind(&result.error) .execute(pool) .await?; Ok(row.last_insert_rowid()) } /// Get the latest CORS check per URL for a target. #[instrument(skip_all)] pub async fn get_latest_cors_checks( pool: &SqlitePool, target: &str, ) -> Result> { let rows = sqlx::query_as::<_, (String, String, String, String, bool, String, Option)>( "SELECT c.target, c.url, c.origin, c.method, c.passes, c.checked_at, c.error FROM cors_checks c INNER JOIN (SELECT url, MAX(id) as max_id FROM cors_checks WHERE target = ? GROUP BY url) latest ON c.id = latest.max_id ORDER BY c.url", ) .bind(target) .fetch_all(pool) .await?; Ok(rows .into_iter() .map( |(target, url, origin, method, passes, checked_at, error)| CorsCheckResult { target, url, origin, method, passes, checked_at, error, }, ) .collect()) }