llm_test_bench_core/api/
server.rs1use axum::{
10 Router,
11 Extension,
12 routing::get,
13};
14use std::{net::SocketAddr, sync::Arc};
15use tokio::net::TcpListener;
16use tower::ServiceBuilder;
17use tower_http::{
18 trace::TraceLayer,
19 compression::CompressionLayer,
20};
21use tracing::info;
22use anyhow::Result;
23
24use crate::api::{
25 auth::AuthService,
26 middleware::CorsConfig,
27 rest::RestApi,
28 graphql::{GraphQLApi, Query, Mutation},
29 websocket::{ws_router, WsState},
30};
31
32#[derive(Debug, Clone)]
34pub struct ApiConfig {
35 pub bind_address: SocketAddr,
37
38 pub enable_rest: bool,
40
41 pub enable_graphql: bool,
43
44 pub enable_websocket: bool,
46
47 pub enable_swagger: bool,
49
50 pub cors: CorsConfig,
52
53 pub jwt_secret: String,
55
56 pub jwt_expiration: i64,
58
59 pub rate_limit_rps: Option<u64>,
61
62 pub rate_limit_burst: Option<u32>,
64
65 pub ws_channel_capacity: usize,
67}
68
69impl Default for ApiConfig {
70 fn default() -> Self {
71 Self {
72 bind_address: "0.0.0.0:3000".parse().unwrap(),
73 enable_rest: true,
74 enable_graphql: true,
75 enable_websocket: true,
76 enable_swagger: true,
77 cors: CorsConfig::default(),
78 jwt_secret: "change_this_in_production_use_env_var".to_string(),
79 jwt_expiration: 3600, rate_limit_rps: Some(100),
81 rate_limit_burst: Some(50),
82 ws_channel_capacity: 1000,
83 }
84 }
85}
86
87impl ApiConfig {
88 pub fn builder() -> ApiConfigBuilder {
90 ApiConfigBuilder::default()
91 }
92}
93
94#[derive(Default)]
96pub struct ApiConfigBuilder {
97 config: ApiConfig,
98}
99
100impl ApiConfigBuilder {
101 pub fn bind_address(mut self, addr: SocketAddr) -> Self {
102 self.config.bind_address = addr;
103 self
104 }
105
106 pub fn enable_rest(mut self, enable: bool) -> Self {
107 self.config.enable_rest = enable;
108 self
109 }
110
111 pub fn enable_graphql(mut self, enable: bool) -> Self {
112 self.config.enable_graphql = enable;
113 self
114 }
115
116 pub fn enable_websocket(mut self, enable: bool) -> Self {
117 self.config.enable_websocket = enable;
118 self
119 }
120
121 pub fn enable_swagger(mut self, enable: bool) -> Self {
122 self.config.enable_swagger = enable;
123 self
124 }
125
126 pub fn cors(mut self, cors: CorsConfig) -> Self {
127 self.config.cors = cors;
128 self
129 }
130
131 pub fn jwt_secret(mut self, secret: String) -> Self {
132 self.config.jwt_secret = secret;
133 self
134 }
135
136 pub fn jwt_expiration(mut self, seconds: i64) -> Self {
137 self.config.jwt_expiration = seconds;
138 self
139 }
140
141 pub fn rate_limit(mut self, rps: u64, burst: u32) -> Self {
142 self.config.rate_limit_rps = Some(rps);
143 self.config.rate_limit_burst = Some(burst);
144 self
145 }
146
147 pub fn build(self) -> ApiConfig {
148 self.config
149 }
150}
151
152#[derive(Clone)]
154pub struct AppState {
155 pub auth: Arc<AuthService>,
157
158 pub ws_state: Arc<WsState>,
160
161 pub graphql_schema: crate::api::graphql::GraphQLSchema,
163}
164
165pub struct ApiServer {
167 config: ApiConfig,
168 state: Arc<AppState>,
169}
170
171impl ApiServer {
172 pub fn new(config: ApiConfig) -> Self {
174 let auth = Arc::new(AuthService::new(
175 config.jwt_secret.clone(),
176 config.jwt_expiration,
177 ));
178
179 let ws_state = Arc::new(WsState::new(config.ws_channel_capacity));
180
181 let graphql_schema = GraphQLApi::schema();
182
183 let state = Arc::new(AppState {
184 auth,
185 ws_state,
186 graphql_schema,
187 });
188
189 Self { config, state }
190 }
191
192 fn build_router(&self) -> Router {
194 let mut app = Router::new();
195
196 app = app.route("/", get(|| async { "LLM Test Bench API" }));
198
199 if self.config.enable_rest {
201 info!("Enabling REST API at /v1/*");
202 let rest_router = RestApi::router::<AppState>();
203 app = app.merge(rest_router);
204 }
205
206 if self.config.enable_graphql {
208 info!("Enabling GraphQL API at /graphql");
209 let graphql_router = self.build_graphql_router();
210 app = app.nest("/graphql", graphql_router);
211 }
212
213 if self.config.enable_websocket {
215 info!("Enabling WebSocket API at /ws");
216 let ws_router = ws_router::<Arc<WsState>>()
217 .with_state(self.state.ws_state.clone());
218 app = app.merge(ws_router);
219 }
220
221 if self.config.enable_swagger {
223 info!("Enabling Swagger UI at /swagger-ui");
224 app = app.merge(self.build_swagger_router());
225 }
226
227 app = app.layer(
229 ServiceBuilder::new()
230 .layer(TraceLayer::new_for_http())
231 .layer(CompressionLayer::new())
232 .layer(self.config.cors.to_layer())
233 );
234
235 app.with_state(self.state.clone())
236 }
237
238 fn build_graphql_router(&self) -> Router<Arc<AppState>> {
240 use async_graphql::http::GraphiQLSource;
241
242 let schema = self.state.graphql_schema.clone();
243
244 async fn graphiql_handler() -> axum::response::Html<String> {
245 axum::response::Html(
246 GraphiQLSource::build()
247 .endpoint("/graphql")
248 .finish()
249 )
250 }
251
252 Router::new()
253 .route("/", axum::routing::post({
254 let schema = schema.clone();
255 move |axum::extract::State(_state): axum::extract::State<Arc<AppState>>, body: String| {
256 let schema = schema.clone();
257 async move {
258 let request = match serde_json::from_str::<async_graphql::Request>(&body) {
259 Ok(req) => req,
260 Err(e) => return axum::Json(async_graphql::Response::from_errors(vec![
261 async_graphql::ServerError::new(format!("Invalid GraphQL request: {}", e), None)
262 ])),
263 };
264 let response = schema.execute(request).await;
265 axum::Json(response)
266 }
267 }
268 }))
269 .route("/", axum::routing::get(graphiql_handler))
270 }
271
272 fn build_swagger_router(&self) -> Router<Arc<AppState>> {
274 use utoipa::OpenApi;
275 use utoipa_swagger_ui::SwaggerUi;
276
277 let openapi = crate::api::rest::ApiDoc::openapi();
278
279 SwaggerUi::new("/swagger-ui")
280 .url("/api-docs/openapi.json", openapi)
281 .into()
282 }
283
284 pub async fn start(self) -> Result<()> {
286 let addr = self.config.bind_address;
287 let app = self.build_router();
288
289 info!("Starting LLM Test Bench API server");
290 info!("Listening on http://{}", addr);
291
292 if self.config.enable_rest {
293 info!(" REST API: http://{}/v1", addr);
294 }
295 if self.config.enable_graphql {
296 info!(" GraphQL: http://{}/graphql", addr);
297 }
298 if self.config.enable_websocket {
299 info!(" WebSocket: ws://{}/ws", addr);
300 }
301 if self.config.enable_swagger {
302 info!(" Swagger UI: http://{}/swagger-ui", addr);
303 }
304
305 let listener = TcpListener::bind(addr).await?;
306 axum::serve(listener, app).await?;
307
308 Ok(())
309 }
310
311 pub fn state(&self) -> &Arc<AppState> {
313 &self.state
314 }
315
316 pub fn config(&self) -> &ApiConfig {
318 &self.config
319 }
320}
321
322pub struct ApiServerBuilder {
324 config: ApiConfig,
325}
326
327impl Default for ApiServerBuilder {
328 fn default() -> Self {
329 Self {
330 config: ApiConfig::default(),
331 }
332 }
333}
334
335impl ApiServerBuilder {
336 pub fn new() -> Self {
338 Self::default()
339 }
340
341 pub fn config(mut self, config: ApiConfig) -> Self {
343 self.config = config;
344 self
345 }
346
347 pub fn bind_address(mut self, addr: SocketAddr) -> Self {
349 self.config.bind_address = addr;
350 self
351 }
352
353 pub fn build(self) -> ApiServer {
355 ApiServer::new(self.config)
356 }
357}
358
359#[cfg(test)]
360mod tests {
361 use super::*;
362
363 #[test]
364 fn test_api_config_default() {
365 let config = ApiConfig::default();
366 assert!(config.enable_rest);
367 assert!(config.enable_graphql);
368 assert!(config.enable_websocket);
369 assert_eq!(config.jwt_expiration, 3600);
370 }
371
372 #[test]
373 fn test_api_config_builder() {
374 let config = ApiConfig::builder()
375 .enable_rest(true)
376 .enable_graphql(false)
377 .jwt_expiration(7200)
378 .build();
379
380 assert!(config.enable_rest);
381 assert!(!config.enable_graphql);
382 assert_eq!(config.jwt_expiration, 7200);
383 }
384
385 #[test]
386 fn test_api_server_creation() {
387 let config = ApiConfig::default();
388 let server = ApiServer::new(config);
389
390 assert!(server.config().enable_rest);
391 assert!(server.config().enable_graphql);
392 }
393
394 #[test]
395 fn test_api_server_builder() {
396 let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
397 let server = ApiServerBuilder::new()
398 .bind_address(addr)
399 .build();
400
401 assert_eq!(server.config().bind_address, addr);
402 }
403}