1pub mod admin;
2pub mod admin_api;
3pub mod admin_resources;
4pub mod columnar;
5pub mod hnsw;
6pub mod kv;
7pub mod session;
8pub mod sql;
9pub mod vector;
10
11use std::sync::atomic::Ordering;
12use std::sync::Arc;
13
14use axum::http::{HeaderValue, StatusCode};
15use axum::middleware;
16use axum::response::{IntoResponse, Response};
17use axum::{Json, Router};
18use serde::Serialize;
19use tower::ServiceBuilder;
20use tower_http::limit::RequestBodyLimitLayer;
21use tower_http::trace::TraceLayer;
22use tracing::Span;
23use uuid::Uuid;
24
25use crate::auth::AuthError;
26use crate::error::ServerError;
27use crate::server::ServerState;
28
29#[derive(Clone, Debug)]
30pub struct RequestContext {
31 pub correlation_id: String,
32 pub actor: Option<String>,
33}
34
35#[derive(Serialize)]
36struct ErrorBody {
37 code: String,
38 message: String,
39 correlation_id: String,
40}
41
42#[derive(Serialize)]
43struct ErrorResponse {
44 error: ErrorBody,
45}
46
47struct QueueWaitGuard<'a> {
48 counter: &'a std::sync::atomic::AtomicUsize,
49}
50
51impl<'a> QueueWaitGuard<'a> {
52 fn new(counter: &'a std::sync::atomic::AtomicUsize) -> Self {
53 Self { counter }
54 }
55}
56
57impl Drop for QueueWaitGuard<'_> {
58 fn drop(&mut self) {
59 self.counter.fetch_sub(1, Ordering::AcqRel);
60 }
61}
62
63pub fn router(state: Arc<ServerState>) -> Router {
64 let api = Router::new()
65 .route("/kv/get", axum::routing::post(kv::get))
66 .route("/kv/put", axum::routing::post(kv::put))
67 .route("/kv/delete", axum::routing::post(kv::delete))
68 .route("/kv/list", axum::routing::post(kv::list))
69 .route("/kv/txn/begin", axum::routing::post(kv::txn_begin))
70 .route("/kv/txn/get", axum::routing::post(kv::txn_get))
71 .route("/kv/txn/put", axum::routing::post(kv::txn_put))
72 .route("/kv/txn/delete", axum::routing::post(kv::txn_delete))
73 .route("/kv/txn/commit", axum::routing::post(kv::txn_commit))
74 .route("/kv/txn/rollback", axum::routing::post(kv::txn_rollback))
75 .route("/columnar/scan", axum::routing::post(columnar::scan))
76 .route("/columnar/stats", axum::routing::post(columnar::stats))
77 .route("/columnar/list", axum::routing::post(columnar::list))
78 .route("/columnar/ingest", axum::routing::post(columnar::ingest))
79 .route(
80 "/columnar/index/create",
81 axum::routing::post(columnar::index_create),
82 )
83 .route(
84 "/columnar/index/list",
85 axum::routing::post(columnar::index_list),
86 )
87 .route(
88 "/columnar/index/drop",
89 axum::routing::post(columnar::index_drop),
90 )
91 .route("/hnsw/search", axum::routing::post(hnsw::search))
92 .route("/hnsw/upsert", axum::routing::post(hnsw::upsert))
93 .route("/hnsw/delete", axum::routing::post(hnsw::delete))
94 .route("/hnsw/create", axum::routing::post(hnsw::create))
95 .route("/hnsw/drop", axum::routing::post(hnsw::drop))
96 .route("/hnsw/stats", axum::routing::post(hnsw::stats))
97 .route("/sql", axum::routing::post(sql::handle))
98 .route("/api/sql/query", axum::routing::post(sql::handle))
99 .route("/vector/search", axum::routing::post(vector::search))
100 .route("/vector/upsert", axum::routing::post(vector::upsert))
101 .route("/vector/delete", axum::routing::post(vector::delete))
102 .route(
103 "/vector/index/create",
104 axum::routing::post(vector::index_create),
105 )
106 .route(
107 "/vector/index/update",
108 axum::routing::post(vector::index_update),
109 )
110 .route(
111 "/vector/index/delete",
112 axum::routing::post(vector::index_delete),
113 )
114 .route(
115 "/vector/index/compact",
116 axum::routing::post(vector::index_compact),
117 )
118 .route(
119 "/api/admin/capabilities",
120 axum::routing::get(admin_api::capabilities),
121 )
122 .route(
123 "/api/admin/resources",
124 axum::routing::get(admin_resources::list),
125 )
126 .route("/api/admin/status", axum::routing::get(admin_api::status))
127 .route("/api/admin/metrics", axum::routing::get(admin_api::metrics))
128 .route("/api/admin/health", axum::routing::get(admin_api::health))
129 .route(
130 "/api/admin/cluster/join",
131 axum::routing::post(admin_api::cluster_join),
132 )
133 .route(
134 "/api/admin/cluster/leave",
135 axum::routing::post(admin_api::cluster_leave),
136 )
137 .route(
138 "/api/admin/backup",
139 axum::routing::post(admin_api::start_backup),
140 )
141 .route("/api/admin/export", axum::routing::post(admin_api::export))
142 .route(
143 "/api/admin/backup/{id}",
144 axum::routing::get(admin_api::backup_status),
145 )
146 .route(
147 "/api/admin/restore",
148 axum::routing::post(admin_api::start_restore),
149 )
150 .route(
151 "/api/admin/restore/{id}",
152 axum::routing::get(admin_api::restore_status),
153 )
154 .route(
155 "/api/admin/lifecycle",
156 axum::routing::post(admin_api::lifecycle),
157 )
158 .route(
159 "/api/admin/compaction",
160 axum::routing::post(admin_api::compaction),
161 )
162 .route("/session/begin", axum::routing::post(session::begin))
163 .route("/session/{id}/commit", axum::routing::post(session::commit))
164 .route(
165 "/session/{id}/rollback",
166 axum::routing::post(session::rollback),
167 );
168
169 let api = if state.config.api_prefix.is_empty() {
170 api
171 } else {
172 Router::new().nest(&state.config.api_prefix, api)
173 };
174
175 let middleware = middleware::from_fn(context_middleware);
176 let connection_middleware = middleware::from_fn(connection_middleware);
177 let admission_middleware = middleware::from_fn(admission_middleware);
178 api.layer(
185 ServiceBuilder::new()
186 .layer(TraceLayer::new_for_http().make_span_with(make_trace_span))
187 .layer(admission_middleware)
188 .layer(middleware)
189 .layer(connection_middleware)
190 .layer(RequestBodyLimitLayer::new(state.config.max_request_size)),
191 )
192 .layer(axum::Extension(state))
193}
194
195pub fn admin_router(state: Arc<ServerState>) -> Router {
196 admin::router(state)
197}
198
199pub async fn context_middleware(
200 axum::extract::Extension(state): axum::extract::Extension<Arc<ServerState>>,
201 mut req: axum::extract::Request,
202 next: middleware::Next,
203) -> Response {
204 let correlation_id =
205 extract_correlation_id(req.headers()).unwrap_or_else(|| Uuid::new_v4().to_string());
206
207 let actor = match state.auth.validate_http(req.headers()) {
208 Ok(actor) => actor,
209 Err(err) => {
210 if state.config.audit_log_enabled {
211 state.audit.log(crate::audit::AuditLogEntry {
212 event_type: crate::audit::AuditEventType::AuthFailure,
213 actor: None,
214 target: "auth".into(),
215 correlation_id: correlation_id.clone(),
216 timestamp: chrono::Utc::now(),
217 details: serde_json::json!({ "error": err.to_string() }),
218 });
219 }
220 return auth_error_response(err, &correlation_id);
221 }
222 };
223
224 req.extensions_mut().insert(RequestContext {
225 correlation_id: correlation_id.clone(),
226 actor,
227 });
228
229 let mut res = next.run(req).await;
230 let _ = res.headers_mut().insert(
231 "x-correlation-id",
232 HeaderValue::from_str(&correlation_id).unwrap_or_else(|_| HeaderValue::from_static("")),
233 );
234 res
235}
236
237pub async fn connection_middleware(
238 axum::extract::Extension(state): axum::extract::Extension<Arc<ServerState>>,
239 req: axum::extract::Request,
240 next: middleware::Next,
241) -> Response {
242 state.metrics.record_connection(1);
243 let res = next.run(req).await;
244 state.metrics.record_connection(-1);
245 res
246}
247
248pub async fn admission_middleware(
249 axum::extract::Extension(state): axum::extract::Extension<Arc<ServerState>>,
250 req: axum::extract::Request,
251 next: middleware::Next,
252) -> Response {
253 if let Ok(permit) = state.admission_permits.clone().try_acquire_owned() {
254 let res = next.run(req).await;
255 drop(permit);
256 return res;
257 }
258
259 let queued_now = state.admission_waiters.fetch_add(1, Ordering::AcqRel) + 1;
260 if queued_now > state.config.max_queue_len {
261 state.admission_waiters.fetch_sub(1, Ordering::AcqRel);
262 let correlation_id =
263 extract_correlation_id(req.headers()).unwrap_or_else(|| Uuid::new_v4().to_string());
264 return queue_overflow_response(&correlation_id);
265 }
266 let queue_wait_guard = QueueWaitGuard::new(&state.admission_waiters);
267
268 let permit = match state.admission_permits.clone().acquire_owned().await {
269 Ok(permit) => permit,
270 Err(_) => {
271 let correlation_id =
272 extract_correlation_id(req.headers()).unwrap_or_else(|| Uuid::new_v4().to_string());
273 return queue_overflow_response(&correlation_id);
274 }
275 };
276 drop(queue_wait_guard);
277
278 let res = next.run(req).await;
279 drop(permit);
280 res
281}
282
283fn auth_error_response(err: AuthError, correlation_id: &str) -> Response {
284 let message = err.to_string();
285 let body = Json(ErrorResponse {
286 error: ErrorBody {
287 code: "UNAUTHORIZED".to_string(),
288 message,
289 correlation_id: correlation_id.to_string(),
290 },
291 });
292 (StatusCode::UNAUTHORIZED, body).into_response()
293}
294
295fn queue_overflow_response(correlation_id: &str) -> Response {
296 let body = Json(ErrorResponse {
297 error: ErrorBody {
298 code: "SERVER_BACKPRESSURE".to_string(),
299 message: "server request queue is full".to_string(),
300 correlation_id: correlation_id.to_string(),
301 },
302 });
303 (StatusCode::SERVICE_UNAVAILABLE, body).into_response()
304}
305
306pub fn error_response(err: ServerError, ctx: &RequestContext) -> Response {
307 let body = Json(ErrorResponse {
308 error: ErrorBody {
309 code: err.error_code(),
310 message: err.to_string(),
311 correlation_id: ctx.correlation_id.clone(),
312 },
313 });
314 (err.status_code(), body).into_response()
315}
316
317fn make_trace_span<B>(request: &axum::http::Request<B>) -> Span {
318 let correlation_id = request
319 .extensions()
320 .get::<RequestContext>()
321 .map(|ctx| ctx.correlation_id.clone())
322 .or_else(|| extract_correlation_id(request.headers()))
323 .unwrap_or_else(|| Uuid::new_v4().to_string());
324 let traceparent = request
325 .headers()
326 .get("traceparent")
327 .and_then(|v| v.to_str().ok())
328 .unwrap_or("");
329 tracing::info_span!(
330 "http_request",
331 correlation_id = %correlation_id,
332 traceparent = %traceparent,
333 method = %request.method(),
334 path = %request.uri().path()
335 )
336}
337
338pub fn json_response<T: Serialize>(value: T, max_size: usize, ctx: &RequestContext) -> Response {
339 match serde_json::to_vec(&value) {
340 Ok(bytes) if bytes.len() <= max_size => (StatusCode::OK, Json(value)).into_response(),
341 Ok(_) => error_response(
342 ServerError::PayloadTooLarge("response size exceeds limit".into()),
343 ctx,
344 ),
345 Err(err) => error_response(ServerError::Internal(err.to_string()), ctx),
346 }
347}
348
349fn extract_correlation_id(headers: &axum::http::HeaderMap) -> Option<String> {
350 headers
351 .get("x-correlation-id")
352 .and_then(|v| v.to_str().ok())
353 .map(|v| v.to_string())
354 .or_else(|| {
355 headers
356 .get("x-request-id")
357 .and_then(|v| v.to_str().ok())
358 .map(|v| v.to_string())
359 })
360}