mod auth;
#[cfg(feature = "teams")]
mod geolocation;
mod runtime_services;
mod search;
#[cfg(feature = "teams")]
mod teams;
mod webhook;
mod workers;
#[cfg(feature = "auth")]
pub use auth::init_garrison_auth;
use log::info;
use std::sync::Arc;
use crate::application::use_cases::create_scrape::CreateScrapeUseCaseTrait;
use crate::bootstrap::infrastructure::InfrastructureComponents;
use crate::config::settings::Settings;
use crate::domain::services::audit_service::AuditServiceTrait;
use crate::domain::services::content_extractor::ContentExtractionFacade;
use crate::domain::services::extraction_service::ExtractionServiceTrait;
#[cfg(feature = "teams")]
use crate::domain::services::geo_location::GeoLocationService;
use crate::domain::services::llm::LLMServiceTrait;
use crate::domain::services::rag_strategy::EmbeddingProvider;
use crate::domain::services::rate_limiting_service::RateLimitingService;
use crate::domain::services::rerank::RerankProvider;
use crate::domain::services::search_service::SearchServiceTrait;
use crate::domain::services::searxng::SearxngInstanceService;
use crate::domain::services::team_semaphore::TeamSemaphore;
#[cfg(feature = "teams")]
use crate::domain::services::team_service::TeamService;
use crate::domain::services::webhook_service::WebhookService;
use crate::engines::engine_client::EngineClient;
use crate::presentation::middleware::rate_limit_middleware::RateLimitMiddleware;
use crate::queue::task_queue::TaskQueue;
use crate::search::engine_trait::SearchEngine;
use crate::utils::robots::RobotsChecker;
use crate::workers::coalesce_coordinator::RequestCoalescer;
#[derive(Clone)]
pub struct ServicesComponents {
pub rate_limit_middleware: RateLimitMiddleware,
pub team_semaphore: Arc<TeamSemaphore>,
pub request_coalescer: Arc<RequestCoalescer>,
pub rate_limiting_service: Arc<dyn RateLimitingService>,
pub create_scrape_use_case: Arc<dyn CreateScrapeUseCaseTrait>,
pub webhook_service: Arc<dyn WebhookService>,
#[cfg(feature = "teams")]
pub team_service: Arc<TeamService>,
#[cfg(feature = "teams")]
pub geo_location_service: Arc<dyn GeoLocationService>,
pub robots_checker: Arc<RobotsChecker>,
pub search_engine_service: Arc<dyn SearchEngine>,
pub search_service: Arc<dyn SearchServiceTrait>,
pub searxng_instances: Option<Arc<dyn SearxngInstanceService>>,
pub queue: Arc<dyn TaskQueue>,
pub audit_service: Arc<dyn AuditServiceTrait>,
pub http_client: Arc<reqwest::Client>,
pub llm_service: Arc<dyn LLMServiceTrait>,
pub extraction_service: Arc<dyn ExtractionServiceTrait>,
pub embedding_provider: Option<Arc<dyn EmbeddingProvider>>,
pub rerank_provider: Option<Arc<dyn RerankProvider>>,
pub fluxweir_gateway: crate::bootstrap::fluxweir_gateway::FluxweirGatewayHandle,
pub content_extractor: Arc<ContentExtractionFacade>,
#[cfg(feature = "webhook")]
pub webhook_worker: Arc<crate::workers::webhook_worker::WebhookWorker>,
pub backlog_worker: Arc<crate::workers::backlog_worker::BacklogWorker>,
pub expiration_worker: Arc<crate::workers::expiration_worker::ExpirationWorker>,
#[cfg(feature = "sitemap")]
pub map_use_case: Arc<crate::application::use_cases::map_use_case::MapUseCase>,
}
pub(crate) async fn init_services(
infrastructure: &InfrastructureComponents,
engine_client: Arc<EngineClient>,
http_client: Arc<reqwest::Client>,
settings: &Settings,
) -> anyhow::Result<ServicesComponents> {
let runtime =
runtime_services::init_runtime_services(infrastructure, &engine_client, settings).await?;
let webhook_service =
webhook::init_webhook_service(&infrastructure.repositories, settings, &http_client);
#[cfg(feature = "teams")]
let (team_service, geo_location_service) =
teams::init_team_services(&infrastructure.repositories, &http_client);
let search =
search::init_search_stack(infrastructure, settings, &http_client, &engine_client).await;
let (queue, audit_service) = auth::init_auth_and_audit(settings, infrastructure).await;
let llm = search::init_llm_stack(
settings,
&http_client,
&search.fluxweir_gateway,
search.embedding_provider.clone(),
);
let workers = workers::init_workers(
&infrastructure.repositories,
settings,
&webhook_service,
&runtime.rate_limiting_service,
);
info!("Services initialized");
Ok(ServicesComponents {
rate_limit_middleware: runtime.rate_limit_middleware,
team_semaphore: runtime.team_semaphore,
request_coalescer: runtime.request_coalescer,
rate_limiting_service: runtime.rate_limiting_service,
create_scrape_use_case: runtime.create_scrape_use_case,
webhook_service,
#[cfg(feature = "teams")]
team_service,
#[cfg(feature = "teams")]
geo_location_service,
robots_checker: search.robots_checker,
search_engine_service: search.search_engine_service,
search_service: search.search_service,
searxng_instances: search.searxng_instances,
queue,
audit_service,
http_client,
llm_service: llm.llm_service,
extraction_service: llm.extraction_service,
embedding_provider: search.embedding_provider,
rerank_provider: search.rerank_provider,
fluxweir_gateway: search.fluxweir_gateway,
content_extractor: llm.content_extractor,
#[cfg(feature = "webhook")]
webhook_worker: workers.webhook_worker,
backlog_worker: workers.backlog_worker,
expiration_worker: workers.expiration_worker,
#[cfg(feature = "sitemap")]
map_use_case: runtime.map_use_case,
})
}
#[cfg(test)]
mod tests {
use super::runtime_services::{
init_rate_limit_middleware, init_rate_limiting_service, init_team_semaphore,
};
use super::*;
use crate::domain::models::CreditsTransactionType;
use crate::domain::services::rate_limiting_service::{
BacklogService, ConcurrencyConfig, ConcurrencyControlService, ConcurrencyResult,
QuotaService, RateLimitConfig, RateLimitResult, RateLimitService, RateLimitingError,
};
use crate::engines::router::EngineRouter;
use crate::utils::host_discipline::HostDiscipline;
struct MockRateLimitingService;
#[async_trait::async_trait]
impl RateLimitService for MockRateLimitingService {
async fn check_rate_limit(
&self,
_api_key: &str,
_endpoint: &str,
) -> Result<RateLimitResult, RateLimitingError> {
Ok(RateLimitResult::Allowed)
}
async fn get_team_rate_limit_config(
&self,
_team_id: crate::domain::id::Id,
) -> Result<RateLimitConfig, RateLimitingError> {
Ok(RateLimitConfig::default())
}
async fn update_team_rate_limit_config(
&self,
_team_id: crate::domain::id::Id,
_config: RateLimitConfig,
) -> Result<(), RateLimitingError> {
Ok(())
}
async fn cleanup_expired_rate_limits(&self) -> Result<u64, RateLimitingError> {
Ok(0)
}
}
#[async_trait::async_trait]
impl ConcurrencyControlService for MockRateLimitingService {
async fn check_team_concurrency(
&self,
_team_id: crate::domain::id::Id,
_task_id: crate::domain::id::Id,
) -> Result<ConcurrencyResult, RateLimitingError> {
Ok(ConcurrencyResult::Allowed)
}
async fn release_team_concurrency_slot(
&self,
_team_id: crate::domain::id::Id,
_task_id: crate::domain::id::Id,
) -> Result<(), RateLimitingError> {
Ok(())
}
async fn get_team_current_concurrency(
&self,
_team_id: crate::domain::id::Id,
) -> Result<u32, RateLimitingError> {
Ok(0)
}
async fn get_team_concurrency_config(
&self,
_team_id: crate::domain::id::Id,
) -> Result<ConcurrencyConfig, RateLimitingError> {
Ok(ConcurrencyConfig::default())
}
async fn update_team_concurrency_config(
&self,
_team_id: crate::domain::id::Id,
_config: ConcurrencyConfig,
) -> Result<(), RateLimitingError> {
Ok(())
}
}
#[async_trait::async_trait]
impl BacklogService for MockRateLimitingService {
async fn process_backlog_tasks(
&self,
_team_id: crate::domain::id::Id,
) -> Result<u32, RateLimitingError> {
Ok(0)
}
}
#[async_trait::async_trait]
impl QuotaService for MockRateLimitingService {
async fn check_and_deduct_quota(
&self,
_team_id: crate::domain::id::Id,
_amount: i64,
_transaction_type: CreditsTransactionType,
_description: String,
_reference_id: Option<crate::domain::id::Id>,
) -> Result<(), RateLimitingError> {
Ok(())
}
async fn get_quota_balance(
&self,
_team_id: crate::domain::id::Id,
) -> Result<i64, RateLimitingError> {
Ok(0)
}
async fn refund_quota(
&self,
_team_id: crate::domain::id::Id,
_amount: i64,
_description: String,
_reference_id: Option<crate::domain::id::Id>,
) -> Result<(), RateLimitingError> {
Ok(())
}
}
impl RateLimitingService for MockRateLimitingService {}
fn make_test_settings(default_team_limit: u64, adaptive_enabled: bool) -> Settings {
let mut settings = Settings::default();
settings.concurrency.default_team_limit = default_team_limit;
settings.concurrency.adaptive_enabled = adaptive_enabled;
settings
}
#[test]
fn test_init_team_semaphore_creates_instance_fixed() {
let settings = make_test_settings(10, false);
let semaphore = init_team_semaphore(&settings);
assert!(
Arc::strong_count(&semaphore) >= 1,
"init_team_semaphore should return a valid Arc<TeamSemaphore>"
);
assert_eq!(semaphore.mode(), "Fixed");
}
#[test]
fn test_init_team_semaphore_with_different_limits_fixed() {
let s1 = init_team_semaphore(&make_test_settings(1, false));
let s2 = init_team_semaphore(&make_test_settings(100, false));
let s3 = init_team_semaphore(&make_test_settings(1000, false));
assert!(Arc::strong_count(&s1) >= 1);
assert!(Arc::strong_count(&s2) >= 1);
assert!(Arc::strong_count(&s3) >= 1);
assert_eq!(s1.mode(), "Fixed");
assert_eq!(s2.mode(), "Fixed");
assert_eq!(s3.mode(), "Fixed");
}
#[test]
fn test_init_team_semaphore_zero_limit_fixed() {
let settings = make_test_settings(0, false);
let semaphore = init_team_semaphore(&settings);
assert!(Arc::strong_count(&semaphore) >= 1);
assert_eq!(semaphore.mode(), "Fixed");
}
#[test]
fn test_init_team_semaphore_adaptive_enabled_creates_adaptive() {
let settings = make_test_settings(10, true);
let semaphore = init_team_semaphore(&settings);
assert_eq!(semaphore.mode(), "Adaptive");
assert_eq!(semaphore.default_permits(), 10);
}
#[test]
fn test_init_team_semaphore_adaptive_disabled_creates_fixed() {
let settings = make_test_settings(20, false);
let semaphore = init_team_semaphore(&settings);
assert_eq!(semaphore.mode(), "Fixed");
assert_eq!(semaphore.default_permits(), 20);
}
#[test]
fn test_init_team_semaphore_adaptive_uses_default_team_limit_as_initial() {
let settings = make_test_settings(15, true);
let semaphore = init_team_semaphore(&settings);
let team_id = crate::domain::id::Id::generate();
assert_eq!(semaphore.current_target(team_id), 15);
}
#[test]
fn test_init_rate_limit_middleware_creates_instance() {
let mock: Arc<dyn RateLimitingService> = Arc::new(MockRateLimitingService);
let middleware = init_rate_limit_middleware(mock);
let _cloned = middleware.clone();
}
#[test]
fn test_init_rate_limit_middleware_with_cloned_service() {
let mock: Arc<dyn RateLimitingService> = Arc::new(MockRateLimitingService);
let middleware = init_rate_limit_middleware(mock.clone());
let _middleware2 = init_rate_limit_middleware(mock);
let _ = &middleware;
}
use crate::bootstrap::infrastructure::{init_database, init_infrastructure, init_repositories};
use crate::common::test_support::testcontainers_fixtures as tcf;
async fn require_docker() -> bool {
tcf::docker_available().await
}
#[tokio::test]
async fn tc_init_rate_limiting_service() {
if !require_docker().await {
eprintln!("[skip] Docker unavailable — tc_init_rate_limiting_service");
return;
}
let pg = match tcf::PgHandle::start().await {
Ok(p) => p,
Err(e) => {
eprintln!("[skip] failed to start postgres container: {e}");
return;
}
};
let mut settings = tcf::settings_with_urls(&pg.url).unwrap();
let db = match init_database(&settings).await {
Ok(d) => d,
Err(e) => {
eprintln!("[skip] failed to init database pool: {e}");
return;
}
};
let repos = init_repositories(db.clone(), &settings);
settings.rate_limiting.storage_backend = "database".to_string();
let service = init_rate_limiting_service(&repos, &settings, Some(db.clone_inner()))
.await
.expect("init_rate_limiting_service");
assert!(Arc::strong_count(&service) >= 1);
settings.rate_limiting.storage_backend = "memory".to_string();
let service = init_rate_limiting_service(&repos, &settings, Some(db.clone_inner()))
.await
.expect("init_rate_limiting_service");
assert!(Arc::strong_count(&service) >= 1);
}
#[tokio::test]
async fn tc_init_services_full_stack() {
if !require_docker().await {
eprintln!("[skip] Docker unavailable — tc_init_services_full_stack");
return;
}
let _garrison_guard = crate::common::test_helpers::acquire_garrison_global_state().await;
let handle = match tcf::DbHandle::start().await {
Ok(h) => h,
Err(e) => {
eprintln!("[skip] failed to start db container: {e}");
return;
}
};
let settings = tcf::settings_with_urls(&handle.pg.url).unwrap();
let infra = match init_infrastructure(&settings).await {
Ok(i) => i,
Err(e) => {
eprintln!("[skip] failed to init infrastructure: {e}");
return;
}
};
let engines = crate::bootstrap::engines::init_engines(
infra.http_client.clone(),
None,
crate::config::settings::ProxyStrategy::RoundRobin,
None,
&settings.engines,
&settings.timeouts.engines,
settings.engines.max_response_body_bytes as usize,
None,
);
let engine_router = Arc::new(EngineRouter::with_host_discipline(
engines.clone(),
HostDiscipline::from_politeness(&settings.politeness),
));
let engine_client = Arc::new(EngineClient::with_router(engine_router.clone()));
let services = init_services(&infra, engine_client, infra.http_client.clone(), &settings)
.await
.expect("init_services");
assert!(Arc::strong_count(&services.rate_limiting_service) >= 1);
assert!(Arc::strong_count(&services.team_semaphore) >= 1);
assert!(Arc::strong_count(&services.create_scrape_use_case) >= 1);
assert!(Arc::strong_count(&services.webhook_service) >= 1);
#[cfg(feature = "teams")]
{
assert!(Arc::strong_count(&services.team_service) >= 1);
assert!(Arc::strong_count(&services.geo_location_service) >= 1);
}
assert!(Arc::strong_count(&services.robots_checker) >= 1);
assert!(Arc::strong_count(&services.search_engine_service) >= 1);
assert!(Arc::strong_count(&services.search_service) >= 1);
assert!(Arc::strong_count(&services.queue) >= 1);
assert!(Arc::strong_count(&services.audit_service) >= 1);
assert!(Arc::strong_count(&services.llm_service) >= 1);
assert!(Arc::strong_count(&services.extraction_service) >= 1);
#[cfg(feature = "webhook")]
{
assert!(Arc::strong_count(&services.webhook_worker) >= 1);
}
assert!(Arc::strong_count(&services.backlog_worker) >= 1);
assert!(Arc::strong_count(&services.expiration_worker) >= 1);
}
}