Skip to main content

cloakpipe_proxy/
server.rs

1//! HTTP server setup and router configuration.
2
3use crate::{handlers, state::AppState};
4use axum::{routing::{get, post}, Router};
5use std::sync::Arc;
6use tower_http::cors::CorsLayer;
7use tower_http::trace::TraceLayer;
8
9/// Build the axum router with all routes and middleware.
10pub fn build_router(state: Arc<AppState>) -> Router {
11    Router::new()
12        .route("/health", get(handlers::health))
13        .route("/v1/chat/completions", post(handlers::proxy_chat_completions))
14        .route("/v1/embeddings", post(handlers::proxy_embeddings))
15        .layer(CorsLayer::permissive())
16        .layer(TraceLayer::new_for_http())
17        .with_state(state)
18}
19
20/// Start the proxy server.
21pub async fn start(state: AppState) -> anyhow::Result<()> {
22    let listen_addr = state.config.proxy.listen.clone();
23    let state = Arc::new(state);
24
25    let app = build_router(state);
26
27    tracing::info!("CloakPipe proxy listening on {}", listen_addr);
28
29    let listener = tokio::net::TcpListener::bind(&listen_addr).await?;
30    axum::serve(listener, app).await?;
31
32    Ok(())
33}