crawlrs 0.2.0

crawlrs - Enterprise-grade web scraping and crawling platform built with Rust
Documentation
// Copyright (c) 2025-2026 Kirky.X🌠
// SPDX-License-Identifier: Apache-2.0

//! Management 路由组 — webhook / extract / teams / audit / admin 路由注册。
//!
//! 包含所有 feature-gated 路由(webhook、teams、auth)和通用管理路由(audit)。

use crate::presentation::handlers::{audit_handler, extract_handler, searxng_handler};
use axum::{routing::get, Router};

// webhook-off 时不导入
#[cfg(feature = "webhook")]
use crate::infrastructure::database::repositories::webhook_repo_impl::WebhookRepoImpl;
#[cfg(feature = "webhook")]
use crate::presentation::handlers::webhook_handler;

// teams-off 时不导入
#[cfg(feature = "teams")]
use crate::infrastructure::database::repositories::database_geo_restriction_repo::DatabaseGeoRestrictionRepository;
#[cfg(feature = "teams")]
use crate::presentation::handlers::team_handler;
#[cfg(feature = "teams")]
use axum::routing::put;

// auth-off 时不导入
#[cfg(feature = "auth")]
use crate::presentation::handlers::api_key_handler;

/// 注册 webhook 相关路由(feature-gated)。
#[cfg(feature = "webhook")]
pub fn register_webhook_routes() -> Router {
    Router::new()
        .route(
            "/v1/webhooks",
            axum::routing::post(webhook_handler::create_webhook::<WebhookRepoImpl>),
        )
        .route(
            "/v1/webhooks",
            get(webhook_handler::list_webhooks::<WebhookRepoImpl>),
        )
}

/// webhook-off 时的空路由占位。
#[cfg(not(feature = "webhook"))]
pub fn register_webhook_routes() -> Router {
    Router::new()
}

/// 注册 extract 路由(teams feature 分裂)。
#[cfg(feature = "teams")]
pub fn register_extract_routes() -> Router {
    Router::new().route(
        "/v1/extract",
        axum::routing::post(extract_handler::extract::<DatabaseGeoRestrictionRepository>),
    )
}

#[cfg(not(feature = "teams"))]
pub fn register_extract_routes() -> Router {
    Router::new().route("/v1/extract", axum::routing::post(extract_handler::extract))
}

/// 注册 teams 路由组(feature-gated)。
#[cfg(feature = "teams")]
pub fn register_teams_routes() -> Router {
    Router::new()
        .route("/v1/teams/me", get(team_handler::get_team_info))
        .route("/v1/teams/me/usage", get(team_handler::get_team_usage))
        .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>),
        )
}

#[cfg(not(feature = "teams"))]
pub fn register_teams_routes() -> Router {
    Router::new()
}

/// 注册 audit 路由。
pub fn register_audit_routes() -> Router {
    Router::new()
        .route("/v1/audit/logs", get(audit_handler::get_audit_logs))
        .route("/v1/audit/denied", get(audit_handler::get_denied_requests))
}

/// 注册 SearXNG 实例生命周期管理路由(auth 保护组)。
///
/// 服务为 None(`search.searxng.enabled = false`)时三端点统一返回 503。
pub fn register_searxng_routes() -> Router {
    Router::new()
        .route(
            "/v1/searxng/instances",
            get(searxng_handler::list_instances),
        )
        .route("/v1/searxng/pool", get(searxng_handler::get_pool))
        .route(
            "/v1/searxng/instances/collect",
            axum::routing::post(searxng_handler::trigger_collect),
        )
}

/// 注册 admin 路由(auth feature-gated)。
#[cfg(feature = "auth")]
pub fn register_admin_routes() -> Router {
    Router::new().route(
        "/v1/admin/api-keys",
        axum::routing::post(api_key_handler::create_api_key),
    )
}

#[cfg(not(feature = "auth"))]
pub fn register_admin_routes() -> Router {
    Router::new()
}