litellm-rs 0.1.1

A high-performance AI Gateway written in Rust, providing OpenAI-compatible APIs with intelligent routing, load balancing, and enterprise features
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! HTTP server implementation
//!
//! This module provides the HTTP server and routing functionality.

pub mod middleware;
pub mod routes;

use crate::config::{Config, ServerConfig};
use crate::utils::error::{GatewayError, Result};
use actix_cors::Cors;
use actix_web::{
    App, HttpResponse, HttpServer as ActixHttpServer,
    middleware::{DefaultHeaders, Logger},
    web,
};
use chrono;
use serde_json::json;
use std::sync::Arc;

use tracing::info;

/// HTTP server state shared across handlers
///
/// This struct contains shared resources that need to be accessed across
/// multiple request handlers. All fields are wrapped in Arc for efficient
/// sharing across threads.
#[derive(Clone)]
#[allow(dead_code)]
pub struct AppState {
    /// Gateway configuration (shared read-only)
    pub config: Arc<Config>,
    /// Authentication system
    pub auth: Arc<crate::auth::AuthSystem>,
    /// Request router
    pub router: Arc<crate::core::router::Router>,
    /// Storage layer
    pub storage: Arc<crate::storage::StorageLayer>,
}

impl AppState {
    /// Create a new AppState with shared resources
    pub fn new(
        config: Config,
        auth: crate::auth::AuthSystem,
        router: crate::core::router::Router,
        storage: crate::storage::StorageLayer,
    ) -> Self {
        Self {
            config: Arc::new(config),
            auth: Arc::new(auth),
            router: Arc::new(router),
            storage: Arc::new(storage),
        }
    }
}

/// HTTP server
#[allow(dead_code)]
pub struct HttpServer {
    /// Server configuration
    config: ServerConfig,
    /// Application state
    state: AppState,
}

#[allow(dead_code)]
impl HttpServer {
    /// Create a new HTTP server
    pub async fn new(config: &Config) -> Result<Self> {
        info!("Creating HTTP server");

        // Create storage layer
        let storage = crate::storage::StorageLayer::new(&config.gateway.storage).await?;

        // Create auth system
        let auth =
            crate::auth::AuthSystem::new(&config.gateway.auth, Arc::new(storage.clone())).await?;

        // Create router
        let router = crate::core::router::Router::new(
            config.gateway.providers.clone(),
            Arc::new(storage.clone()),
            crate::core::router::RoutingStrategy::RoundRobin,
        )
        .await?;

        // Create shared state using the builder method
        let state = AppState::new(config.clone(), auth, router, storage);

        Ok(Self {
            config: config.gateway.server.clone(),
            state,
        })
    }

    /// Create the Actix-web application
    fn create_app(
        state: web::Data<AppState>,
    ) -> App<
        impl actix_web::dev::ServiceFactory<
            actix_web::dev::ServiceRequest,
            Config = (),
            Response = actix_web::dev::ServiceResponse<impl actix_web::body::MessageBody>,
            Error = actix_web::Error,
            InitError = (),
        >,
    > {
        info!("Setting up routes and middleware");

        let cors_config = &state.config.gateway.server.cors;
        let mut cors = Cors::default();

        // Configure CORS based on settings
        if cors_config.enabled {
            if cors_config.allows_all_origins() {
                cors = cors.allow_any_origin();
                cors_config.validate().unwrap_or_else(|e| {
                    eprintln!("⚠️  CORS Configuration Warning: {}", e);
                });
            } else {
                for origin in &cors_config.allowed_origins {
                    cors = cors.allowed_origin(origin);
                }
            }

            // Convert method strings to actix methods
            let methods: Vec<actix_web::http::Method> = cors_config
                .allowed_methods
                .iter()
                .filter_map(|m| m.parse().ok())
                .collect();
            if !methods.is_empty() {
                cors = cors.allowed_methods(methods);
            }

            // Convert header strings
            let headers: Vec<actix_web::http::header::HeaderName> = cors_config
                .allowed_headers
                .iter()
                .filter_map(|h| h.parse().ok())
                .collect();
            if !headers.is_empty() {
                cors = cors.allowed_headers(headers);
            }

            cors = cors.max_age(cors_config.max_age as usize);

            if cors_config.allow_credentials {
                cors = cors.supports_credentials();
            }
        }

        App::new()
            .app_data(state)
            // Add CORS middleware with secure configuration
            .wrap(cors)
            // Add logging middleware
            .wrap(Logger::default())
            // Add default headers
            .wrap(DefaultHeaders::new().add(("Server", "LiteLLM-RS")))
            // Health check route
            .route("/health", web::get().to(health_check))
            // Configure AI API routes using the proper implementation
            .configure(routes::ai::configure_routes)
    }

    /// Start the HTTP server
    pub async fn start(self) -> Result<()> {
        let bind_addr = format!("{}:{}", self.config.host, self.config.port);

        info!("Starting HTTP server on {}", bind_addr);

        let state = web::Data::new(self.state);

        // Create and start the Actix-web server
        let server = ActixHttpServer::new(move || Self::create_app(state.clone()))
            .bind(&bind_addr)
            .map_err(|e| GatewayError::server(format!("Failed to bind to {}: {}", bind_addr, e)))?
            .run();

        info!("HTTP server listening on {}", bind_addr);

        // Start the server
        server
            .await
            .map_err(|e| GatewayError::server(format!("Server error: {}", e)))?;

        info!("HTTP server stopped");
        Ok(())
    }

    /// Graceful shutdown signal handler
    async fn shutdown_signal() {
        let ctrl_c = async {
            tokio::signal::ctrl_c()
                .await
                .expect("Failed to install Ctrl+C handler");
        };

        #[cfg(unix)]
        let terminate = async {
            tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
                .expect("Failed to install signal handler")
                .recv()
                .await;
        };

        #[cfg(not(unix))]
        let terminate = std::future::pending::<()>();

        tokio::select! {
            _ = ctrl_c => {
                info!("Received Ctrl+C signal, shutting down gracefully");
            },
            _ = terminate => {
                info!("Received terminate signal, shutting down gracefully");
            },
        }
    }

    /// Get server configuration
    pub fn config(&self) -> &ServerConfig {
        &self.config
    }

    /// Get application state
    pub fn state(&self) -> &AppState {
        &self.state
    }
}

impl AppState {
    /// Get gateway configuration
    #[allow(dead_code)] // May be used by handlers
    pub fn config(&self) -> &Config {
        &self.config
    }
}

/// Server builder for easier configuration
#[allow(dead_code)]
pub struct ServerBuilder {
    config: Option<Config>,
}

#[allow(dead_code)]
impl ServerBuilder {
    /// Create a new server builder
    pub fn new() -> Self {
        Self { config: None }
    }

    /// Set configuration
    pub fn with_config(mut self, config: Config) -> Self {
        self.config = Some(config);
        self
    }

    /// Build the HTTP server
    pub async fn build(self) -> Result<HttpServer> {
        let config = self
            .config
            .ok_or_else(|| GatewayError::Config("Configuration is required".to_string()))?;

        HttpServer::new(&config).await
    }
}

/// Run the server with automatic configuration loading
#[allow(dead_code)]
pub async fn run_server() -> Result<()> {
    info!("🚀 启动 Rust LiteLLM Gateway");

    // 自动加载配置文件
    let config_path = "config/gateway.yaml";
    info!("📄 加载配置文件: {}", config_path);

    let config = match Config::from_file(config_path).await {
        Ok(config) => {
            info!("✅ 配置文件加载成功");
            config
        }
        Err(e) => {
            info!("⚠️  配置文件加载失败,使用默认配置: {}", e);
            info!("💡 请确保 config/gateway.yaml 文件存在并填入正确的 API 密钥");
            Config::default()
        }
    };

    // 创建并启动服务器
    let server = HttpServer::new(&config).await?;
    info!(
        "🌐 服务器启动地址: http://{}:{}",
        config.server().host,
        config.server().port
    );
    info!("📋 API 端点:");
    info!("   GET  /health - 健康检查");
    info!("   GET  /v1/models - 模型列表");
    info!("   POST /v1/chat/completions - 聊天完成");
    info!("   POST /v1/completions - 文本完成");
    info!("   POST /v1/embeddings - 文本嵌入");

    server.start().await
}

impl Default for ServerBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Server health status
#[derive(Debug, Clone, serde::Serialize)]
pub struct ServerHealth {
    /// Server status
    pub status: String,
    /// Server uptime in seconds
    pub uptime: u64,
    /// Number of active connections
    pub active_connections: u32,
    /// Memory usage in bytes
    pub memory_usage: u64,
    /// CPU usage percentage
    pub cpu_usage: f64,
    /// Storage health
    pub storage_health: crate::storage::StorageHealthStatus,
}

/// Request metrics for monitoring
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct RequestMetrics {
    /// Request ID
    pub request_id: String,
    /// HTTP method
    pub method: String,
    /// Request path
    pub path: String,
    /// Response status code
    pub status_code: u16,
    /// Response time in milliseconds
    pub response_time_ms: u64,
    /// Request size in bytes
    pub request_size: u64,
    /// Response size in bytes
    pub response_size: u64,
    /// User agent
    pub user_agent: Option<String>,
    /// Client IP address
    pub client_ip: Option<String>,
    /// User ID (if authenticated)
    pub user_id: Option<uuid::Uuid>,
    /// API key ID (if used)
    pub api_key_id: Option<uuid::Uuid>,
}

// Route handlers
async fn health_check() -> HttpResponse {
    HttpResponse::Ok().json(json!({
        "status": "healthy",
        "timestamp": chrono::Utc::now().to_rfc3339(),
        "version": env!("CARGO_PKG_VERSION")
    }))
}

// Removed unused list_models function - now using proper AI routes

// Placeholder functions removed - now using proper AI routes from routes::ai module

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_server_builder() {
        let builder = ServerBuilder::new();
        assert!(builder.config.is_none());
    }

    #[test]
    fn test_app_state_creation() {
        // Basic test to ensure module compiles
        // HttpServer requires config, so we just test that the type exists
        assert_eq!(
            std::mem::size_of::<HttpServer>(),
            std::mem::size_of::<HttpServer>()
        );
    }

    #[test]
    fn test_request_metrics_creation() {
        let metrics = RequestMetrics {
            request_id: "req-123".to_string(),
            method: "GET".to_string(),
            path: "/health".to_string(),
            status_code: 200,
            response_time_ms: 50,
            request_size: 0,
            response_size: 100,
            user_agent: Some("test-agent".to_string()),
            client_ip: Some("127.0.0.1".to_string()),
            user_id: None,
            api_key_id: None,
        };

        assert_eq!(metrics.request_id, "req-123");
        assert_eq!(metrics.method, "GET");
        assert_eq!(metrics.status_code, 200);
    }
}