Skip to main content

maple_proxy/
lib.rs

1pub mod config;
2pub mod proxy;
3
4pub use config::{Config, OpenAIError, OpenAIErrorDetails};
5use proxy::{create_chat_completion, create_embeddings, health_check, list_models, ProxyState};
6
7use axum::{
8    extract::DefaultBodyLimit,
9    http::Method,
10    routing::{get, post},
11    Router,
12};
13use std::sync::Arc;
14use tower::ServiceBuilder;
15use tower_http::{
16    cors::{Any, CorsLayer},
17    trace::{DefaultMakeSpan, DefaultOnResponse, TraceLayer},
18};
19use tracing::Level;
20
21pub const MAX_PROXY_REQUEST_BODY_BYTES: usize = 50 * 1024 * 1024;
22
23/// Create the Axum application with the given configuration
24pub fn create_app(config: Config) -> Router {
25    let state = Arc::new(ProxyState::new(config.clone()));
26
27    let mut app = Router::new()
28        // Health check endpoints
29        .route("/health", get(health_check))
30        .route("/", get(health_check))
31        // OpenAI-compatible endpoints
32        .route("/v1/models", get(list_models))
33        .route("/v1/chat/completions", post(create_chat_completion))
34        .route("/v1/embeddings", post(create_embeddings))
35        .with_state(state)
36        .layer(
37            ServiceBuilder::new()
38                .layer(DefaultBodyLimit::max(MAX_PROXY_REQUEST_BODY_BYTES))
39                .layer(
40                    TraceLayer::new_for_http()
41                        .make_span_with(DefaultMakeSpan::new().level(Level::INFO))
42                        .on_response(DefaultOnResponse::new().level(Level::INFO)),
43                ),
44        );
45
46    // Add CORS if enabled
47    if config.enable_cors {
48        app = app.layer(
49            CorsLayer::new()
50                .allow_origin(Any)
51                .allow_methods([Method::GET, Method::POST, Method::OPTIONS])
52                .allow_headers(Any),
53        );
54    }
55
56    app
57}