Skip to main content

crabtalk_proxy/
lib.rs

1use axum::{
2    Router, middleware,
3    routing::{get, post},
4};
5use crabtalk_core::Storage;
6
7pub use auth::KeyName;
8pub use state::AppState;
9
10pub mod auth;
11pub mod ext;
12mod handlers;
13mod state;
14pub mod storage;
15
16/// Build the Axum router with all API routes and admin routes.
17pub fn router<S: Storage + 'static>(state: AppState<S>, admin_routes: Vec<Router>) -> Router {
18    let mut app = Router::<AppState<S>>::new()
19        .route(
20            "/v1/chat/completions",
21            post(handlers::chat_completions::<S>),
22        )
23        .route("/v1/embeddings", post(handlers::embeddings::<S>))
24        .route(
25            "/v1/images/generations",
26            post(handlers::image_generations::<S>),
27        )
28        .route("/v1/audio/speech", post(handlers::audio_speech::<S>))
29        .route(
30            "/v1/audio/transcriptions",
31            post(handlers::audio_transcriptions::<S>),
32        )
33        .route("/v1/models", get(handlers::models::<S>))
34        .layer(middleware::from_fn_with_state(
35            state.clone(),
36            auth::auth::<S>,
37        ))
38        .with_state(state);
39
40    // Merge extension-provided admin routes (stateless — extensions
41    // capture their own state via closures in the Router<()>).
42    for admin_router in admin_routes {
43        app = app.merge(admin_router);
44    }
45
46    app
47}