maple_proxy/
lib.rs

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