oxirs_embed/api/
mod.rs

1//! RESTful and GraphQL API endpoints for embedding services
2//!
3//! This module provides production-ready HTTP APIs for embedding generation,
4//! model management, and batch processing capabilities.
5//!
6//! ## Module Structure
7//!
8//! - **config**: API configuration and state management
9//! - **types**: Request/response types and data structures
10//! - **graphql**: GraphQL schema and resolvers
11//! - **handlers**: HTTP endpoint handlers
12//! - **routes**: API route definitions
13//! - **helpers**: Utility functions
14
15pub mod config;
16pub mod graphql;
17pub mod handlers;
18pub mod helpers;
19pub mod routes;
20pub mod types;
21
22// Re-export main types and functions
23pub use config::*;
24#[cfg(feature = "graphql")]
25pub use graphql::create_schema;
26pub use routes::create_router;
27pub use types::*;
28
29/// Start the API server
30#[cfg(feature = "api-server")]
31pub async fn start_server(state: ApiState) -> anyhow::Result<()> {
32    use axum::http::StatusCode;
33    use std::net::SocketAddr;
34    use tower::ServiceBuilder;
35    use tower_http::{cors::CorsLayer, timeout::TimeoutLayer, trace::TraceLayer};
36    use tracing::info;
37
38    let app = create_router(state.clone()).layer(
39        ServiceBuilder::new()
40            .layer(TraceLayer::new_for_http())
41            .layer(TimeoutLayer::with_status_code(
42                StatusCode::REQUEST_TIMEOUT,
43                std::time::Duration::from_secs(state.config.request_timeout_secs),
44            ))
45            .layer(if state.config.enable_cors {
46                CorsLayer::permissive()
47            } else {
48                CorsLayer::new()
49            }),
50    );
51
52    let addr = SocketAddr::from(([127, 0, 0, 1], state.config.port));
53    info!("API server starting on {}", addr);
54
55    let listener = tokio::net::TcpListener::bind(addr).await?;
56    axum::serve(listener, app).await?;
57
58    Ok(())
59}