systemprompt_api/routes/content/
query.rs1use axum::extract::State;
7use axum::http::StatusCode;
8use axum::response::{IntoResponse, Response};
9use axum::{Extension, Json};
10use systemprompt_content::{SearchRequest, SearchService};
11use systemprompt_models::RequestContext;
12use systemprompt_runtime::AppContext;
13
14pub async fn query_handler(
15 Extension(_req_ctx): Extension<RequestContext>,
16 State(ctx): State<AppContext>,
17 Json(request): Json<SearchRequest>,
18) -> Response {
19 log_search_start(&request.query);
20
21 let repositories = ctx.content_repositories();
22 let search_service =
23 SearchService::new(repositories.search.clone(), repositories.content.clone());
24
25 execute_search(&search_service, &request).await
26}
27
28fn log_search_start(query: &str) {
29 tracing::info!(query = %query, "Searching");
30}
31
32async fn execute_search(service: &SearchService, request: &SearchRequest) -> Response {
33 match service.search(request).await {
34 Ok(response) => {
35 tracing::info!(total = response.total, "Search completed");
36 Json(response).into_response()
37 },
38 Err(e) => {
39 tracing::error!(error = %e, "Search error");
40 internal_error(&e.to_string())
41 },
42 }
43}
44
45fn internal_error(message: &str) -> Response {
46 (
47 StatusCode::INTERNAL_SERVER_ERROR,
48 Json(serde_json::json!({"error": message})),
49 )
50 .into_response()
51}