oxirs_embed/api/
config.rs

1//! API configuration and state management
2//!
3//! This module contains configuration structures and server state management
4//! for the embedding API service.
5
6#[cfg(feature = "api-server")]
7use crate::{CacheManager, EmbeddingModel, ModelRegistry};
8use std::collections::HashMap;
9use std::sync::Arc;
10use tokio::sync::RwLock;
11use uuid::Uuid;
12
13/// API server state
14#[derive(Clone)]
15pub struct ApiState {
16    /// Model registry for managing deployed models
17    pub registry: Arc<ModelRegistry>,
18    /// Cache manager for performance optimization
19    pub cache_manager: Arc<CacheManager>,
20    /// Currently loaded models
21    pub models: Arc<RwLock<HashMap<Uuid, Arc<dyn EmbeddingModel + Send + Sync>>>>,
22    /// API configuration
23    pub config: ApiConfig,
24}
25
26/// API configuration
27#[derive(Debug, Clone)]
28pub struct ApiConfig {
29    /// Server host
30    pub host: String,
31    /// Server port
32    pub port: u16,
33    /// Request timeout in seconds
34    pub timeout_seconds: u64,
35    /// Request timeout in seconds (alias for axum)
36    pub request_timeout_secs: u64,
37    /// Maximum batch size for bulk operations
38    pub max_batch_size: usize,
39    /// Rate limiting configuration
40    pub rate_limit: RateLimitConfig,
41    /// Authentication configuration
42    pub auth: AuthConfig,
43    /// Enable request logging
44    pub enable_logging: bool,
45    /// Enable CORS
46    pub enable_cors: bool,
47}
48
49impl Default for ApiConfig {
50    fn default() -> Self {
51        Self {
52            host: "0.0.0.0".to_string(),
53            port: 8080,
54            timeout_seconds: 30,
55            request_timeout_secs: 30,
56            max_batch_size: 1000,
57            rate_limit: RateLimitConfig::default(),
58            auth: AuthConfig::default(),
59            enable_logging: true,
60            enable_cors: true,
61        }
62    }
63}
64
65/// Rate limiting configuration
66#[derive(Debug, Clone)]
67pub struct RateLimitConfig {
68    /// Requests per minute per IP
69    pub requests_per_minute: u32,
70    /// Enable rate limiting
71    pub enabled: bool,
72}
73
74impl Default for RateLimitConfig {
75    fn default() -> Self {
76        Self {
77            requests_per_minute: 1000,
78            enabled: true,
79        }
80    }
81}
82
83/// Authentication configuration
84#[derive(Debug, Clone, Default)]
85pub struct AuthConfig {
86    /// Enable API key authentication
87    pub require_api_key: bool,
88    /// Valid API keys
89    pub api_keys: Vec<String>,
90    /// Enable JWT authentication
91    pub enable_jwt: bool,
92    /// JWT secret
93    pub jwt_secret: Option<String>,
94}