#[cfg(feature = "teams")]
use crate::infrastructure::database::repositories::database_geo_restriction_repo::DatabaseGeoRestrictionRepository;
use crate::infrastructure::database::repositories::task_repo_impl::TaskRepositoryImpl;
#[cfg(feature = "webhook")]
use crate::infrastructure::database::repositories::webhook_repo_impl::WebhookRepoImpl;
use crate::presentation::handlers::{
audit_handler, crawl_handler, extract_handler, metrics_handler, scrape_handler, search_handler,
task_handler,
};
#[cfg(feature = "webhook")]
use crate::presentation::handlers::webhook_handler;
#[cfg(feature = "teams")]
use crate::presentation::handlers::team_handler;
use axum::{
routing::{get, post},
Extension, Json, Router,
};
use dbnexus::sea_orm::ConnectionTrait;
use dbnexus::DbPool;
use std::sync::Arc;
use std::time::Duration;
#[cfg(feature = "teams")]
use axum::routing::put;
use serde_json::json;
pub fn routes() -> Router {
let app = Router::new()
.route("/health", get(health_check))
.route("/metrics", get(metrics_handler::metrics))
.route("/v1/version", get(version))
.route("/v1/scrape", post(scrape_handler::create_scrape))
.route("/v1/scrape/{id}", get(scrape_handler::get_scrape_status))
.route(
"/v1/scrape/{id}/_cancel",
post(scrape_handler::cancel_scrape),
)
.route("/v1/crawl", post(crawl_handler::create_crawl))
.route("/v1/crawl/{id}", get(crawl_handler::get_crawl))
.route(
"/v1/crawl/{id}/results",
get(crawl_handler::get_crawl_results),
)
.route("/v1/crawl/{id}/_cancel", post(crawl_handler::cancel_crawl))
.route("/v1/search", post(search_handler::search));
#[cfg(feature = "teams")]
let app = app.route(
"/v1/extract",
post(extract_handler::extract::<DatabaseGeoRestrictionRepository>),
);
#[cfg(not(feature = "teams"))]
let app = app.route("/v1/extract", post(extract_handler::extract));
#[cfg(feature = "webhook")]
let app = app.route(
"/v1/webhooks",
post(webhook_handler::create_webhook::<WebhookRepoImpl>),
);
#[cfg(feature = "teams")]
let app = app
.route(
"/v1/teams/geo-restrictions",
get(team_handler::get_team_geo_restrictions::<DatabaseGeoRestrictionRepository>),
)
.route(
"/v1/teams/geo-restrictions",
put(team_handler::update_team_geo_restrictions::<DatabaseGeoRestrictionRepository>),
);
app.route("/v1/audit/logs", get(audit_handler::get_audit_logs))
.route("/v1/audit/denied", get(audit_handler::get_denied_requests))
.route(
"/v1/tasks/_query",
post(task_handler::query_tasks::<TaskRepositoryImpl>),
)
.route(
"/v1/tasks/_cancel",
post(task_handler::cancel_tasks::<TaskRepositoryImpl>),
)
}
pub async fn health_check() -> Json<serde_json::Value> {
Json(json!({
"status": "healthy",
"version": env!("CARGO_PKG_VERSION"),
}))
}
pub async fn version() -> &'static str {
env!("CARGO_PKG_VERSION")
}
pub async fn readiness_check(
Extension(db_pool): Extension<Arc<DbPool>>,
Extension(cache_service): Extension<Arc<dyn crate::infrastructure::oxcache::CacheService>>,
) -> (axum::http::StatusCode, Json<serde_json::Value>) {
use axum::http::StatusCode;
let mut details = serde_json::Map::new();
let (db_ready, db_status) = check_database(&db_pool).await;
details.insert("database".to_string(), db_status);
let (cache_ready, cache_status) = check_cache(&cache_service).await;
details.insert("cache".to_string(), cache_status);
details.insert(
"database_pool".to_string(),
database_pool_snapshot(&db_pool).await,
);
let all_ready = db_ready && cache_ready;
let status = if all_ready { "ready" } else { "not_ready" };
let status_code = if all_ready {
StatusCode::OK
} else {
StatusCode::SERVICE_UNAVAILABLE
};
(
status_code,
Json(json!({
"status": status,
"checks": details,
"version": env!("CARGO_PKG_VERSION"),
})),
)
}
async fn check_database(db_pool: &Arc<DbPool>) -> (bool, serde_json::Value) {
let db_check = async {
let session = db_pool
.get_session("system")
.await
.map_err(|e| e.to_string())?;
let conn = session.connection().map_err(|e| e.to_string())?;
conn.execute_unprepared("SELECT 1")
.await
.map_err(|e| e.to_string())?;
Ok::<_, String>(())
};
match tokio::time::timeout(Duration::from_secs(3), db_check).await {
Ok(Ok(())) => (true, json!({"status": "up"})),
Ok(Err(e)) => {
log::warn!("Readiness check: PostgreSQL down - {e}");
(false, json!({"status": "down", "error": e}))
}
Err(_) => {
log::warn!("Readiness check: PostgreSQL timeout (3s)");
(false, json!({"status": "down", "error": "timeout"}))
}
}
}
async fn check_cache(
cache_service: &Arc<dyn crate::infrastructure::oxcache::CacheService>,
) -> (bool, serde_json::Value) {
let cache_check = async {
cache_service
.get("__readiness_probe__")
.await
.map_err(|e| e.to_string())?;
Ok::<_, String>(())
};
match tokio::time::timeout(Duration::from_secs(1), cache_check).await {
Ok(Ok(())) => (true, json!({"status": "up"})),
Ok(Err(e)) => {
log::warn!("Readiness check: cache down - {e}");
(false, json!({"status": "down", "error": e}))
}
Err(_) => {
log::warn!("Readiness check: cache timeout (1s)");
(false, json!({"status": "down", "error": "timeout"}))
}
}
}
async fn database_pool_snapshot(db_pool: &Arc<DbPool>) -> serde_json::Value {
let mut pool_snapshot =
match tokio::time::timeout(Duration::from_secs(1), db_pool.health_snapshot()).await {
Ok(snapshot) => snapshot,
Err(_) => {
log::warn!("Readiness check: pool health snapshot timeout (1s)");
json!({"status": "timeout"})
}
};
if let Some(stats) = db_pool.prepare_cache_stats() {
pool_snapshot["prepare_cache"] = json!({
"size": stats.size,
"hits": stats.hits,
"misses": stats.misses,
"evictions": stats.evictions,
});
}
pool_snapshot
}
#[cfg(test)]
mod tests {
use super::*;
use crate::common::test_support::testcontainers_fixtures as tcf;
use crate::infrastructure::database::dbnexus_connection::create_pool_with_retry;
use crate::infrastructure::oxcache::CacheService;
use axum::body::Body;
use axum::http::{Method, Request, StatusCode};
use axum::routing::get;
use axum::Router;
use std::sync::atomic::{AtomicBool, Ordering};
use tower::ServiceExt;
struct MockCacheService {
should_fail: AtomicBool,
}
impl MockCacheService {
fn new(should_fail: bool) -> Self {
Self {
should_fail: AtomicBool::new(should_fail),
}
}
}
impl CacheService for MockCacheService {
fn get(
&self,
_key: &str,
) -> std::pin::Pin<
Box<dyn std::future::Future<Output = anyhow::Result<Option<String>>> + Send + '_>,
> {
let should_fail = self.should_fail.load(Ordering::Relaxed);
Box::pin(async move {
if should_fail {
Err(anyhow::anyhow!("mock cache error"))
} else {
Ok(None)
}
})
}
fn set(
&self,
_key: &str,
_value: &str,
_ttl_seconds: u64,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = anyhow::Result<()>> + Send + '_>>
{
Box::pin(async { Ok(()) })
}
fn delete(
&self,
_key: &str,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = anyhow::Result<()>> + Send + '_>>
{
Box::pin(async { Ok(()) })
}
fn exists(
&self,
_key: &str,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = anyhow::Result<bool>> + Send + '_>>
{
Box::pin(async { Ok(false) })
}
}
async fn require_docker() -> bool {
tcf::docker_available().await
}
fn build_ready_router(db_pool: Arc<DbPool>, cache_service: Arc<dyn CacheService>) -> Router {
Router::new()
.route("/ready", get(readiness_check))
.layer(Extension(db_pool))
.layer(Extension(cache_service))
}
async fn start_db_with_pool() -> Option<(tcf::DbHandle, Arc<DbPool>)> {
for attempt in 1..=2 {
let handle = match tcf::DbHandle::start().await {
Ok(h) => h,
Err(e) => {
eprintln!("[skip] failed to start DB container (attempt {attempt}): {e}");
return None;
}
};
let settings = tcf::database_settings(&handle.pg.url);
match create_pool_with_retry(&settings, 3, 1).await {
Ok(pool) => return Some((handle, Arc::new(pool))),
Err(e) => {
eprintln!(
"[warn] testcontainers DB pool creation failed (attempt {attempt}), \
restarting container: {e}"
);
}
}
}
eprintln!("[skip] testcontainers DB pool creation failed after container restart");
None
}
#[tokio::test]
async fn tc_readiness_all_up_returns_200() {
if !require_docker().await {
eprintln!("[skip] Docker unavailable — tc_readiness_all_up_returns_200");
return;
}
let Some((_handle, pool)) = start_db_with_pool().await else {
eprintln!("[skip] DB unavailable — tc_readiness_all_up_returns_200");
return;
};
let cache = Arc::new(MockCacheService::new(false));
let app = build_ready_router(pool, cache);
let response = app
.oneshot(
Request::builder()
.method(Method::GET)
.uri("/ready")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let body = axum::body::to_bytes(response.into_body(), 4096)
.await
.unwrap();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["status"], "ready");
assert_eq!(json["checks"]["database"]["status"], "up");
assert_eq!(json["checks"]["cache"]["status"], "up");
assert!(json["version"].is_string());
}
#[tokio::test]
async fn tc_readiness_cache_down_returns_503() {
if !require_docker().await {
eprintln!("[skip] Docker unavailable — tc_readiness_cache_down_returns_503");
return;
}
let Some((_handle, pool)) = start_db_with_pool().await else {
eprintln!("[skip] DB unavailable — tc_readiness_cache_down_returns_503");
return;
};
let cache = Arc::new(MockCacheService::new(true)); let app = build_ready_router(pool, cache);
let response = app
.oneshot(
Request::builder()
.method(Method::GET)
.uri("/ready")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
let body = axum::body::to_bytes(response.into_body(), 4096)
.await
.unwrap();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["status"], "not_ready");
assert_eq!(json["checks"]["database"]["status"], "up");
assert_eq!(json["checks"]["cache"]["status"], "down");
}
}