mirror of
https://github.com/perstarkse/minne.git
synced 2026-03-29 22:01:59 +02:00
refactoring: new structure and mailer
This commit is contained in:
38
src/server/routes/api/file.rs
Normal file
38
src/server/routes/api/file.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use crate::{error::ApiError, server::AppState, storage::types::file_info::FileInfo};
|
||||
use axum::{extract::State, response::IntoResponse, Json};
|
||||
use axum_typed_multipart::{FieldData, TryFromMultipart, TypedMultipart};
|
||||
use serde_json::json;
|
||||
use tempfile::NamedTempFile;
|
||||
use tracing::info;
|
||||
|
||||
#[derive(Debug, TryFromMultipart)]
|
||||
pub struct FileUploadRequest {
|
||||
#[form_data(limit = "1000000")] // Example limit: ~1000 KB
|
||||
pub file: FieldData<NamedTempFile>,
|
||||
}
|
||||
|
||||
/// Handler to upload a new file.
|
||||
///
|
||||
/// Route: POST /file
|
||||
pub async fn upload_handler(
|
||||
State(state): State<AppState>,
|
||||
TypedMultipart(input): TypedMultipart<FileUploadRequest>,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
info!("Received an upload request");
|
||||
|
||||
// Process the file upload
|
||||
let file_info = FileInfo::new(input.file, &state.surreal_db_client).await?;
|
||||
|
||||
// Prepare the response JSON
|
||||
let response = json!({
|
||||
"id": file_info.id,
|
||||
"sha256": file_info.sha256,
|
||||
"path": file_info.path,
|
||||
"mime_type": file_info.mime_type,
|
||||
});
|
||||
|
||||
info!("File uploaded successfully: {:?}", file_info);
|
||||
|
||||
// Return the response with HTTP 200
|
||||
Ok((axum::http::StatusCode::OK, Json(response)))
|
||||
}
|
||||
28
src/server/routes/api/ingress.rs
Normal file
28
src/server/routes/api/ingress.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use crate::{
|
||||
error::ApiError,
|
||||
ingress::types::ingress_input::{create_ingress_objects, IngressInput},
|
||||
server::AppState,
|
||||
storage::types::user::User,
|
||||
};
|
||||
use axum::{extract::State, http::StatusCode, response::IntoResponse, Extension, Json};
|
||||
use futures::future::try_join_all;
|
||||
use tracing::info;
|
||||
|
||||
pub async fn ingress_handler(
|
||||
State(state): State<AppState>,
|
||||
Extension(user): Extension<User>,
|
||||
Json(input): Json<IngressInput>,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
info!("Received input: {:?}", input);
|
||||
|
||||
let ingress_objects = create_ingress_objects(input, &state.surreal_db_client, &user.id).await?;
|
||||
|
||||
let futures: Vec<_> = ingress_objects
|
||||
.into_iter()
|
||||
.map(|object| state.rabbitmq_producer.publish(object))
|
||||
.collect();
|
||||
|
||||
try_join_all(futures).await?;
|
||||
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
4
src/server/routes/api/mod.rs
Normal file
4
src/server/routes/api/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
pub mod file;
|
||||
pub mod ingress;
|
||||
pub mod query;
|
||||
pub mod queue_length;
|
||||
34
src/server/routes/api/query.rs
Normal file
34
src/server/routes/api/query.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use crate::{
|
||||
error::ApiError, retrieval::query_helper::get_answer_with_references, server::AppState,
|
||||
storage::types::user::User,
|
||||
};
|
||||
use axum::{extract::State, response::IntoResponse, Extension, Json};
|
||||
use serde::Deserialize;
|
||||
use tracing::info;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct QueryInput {
|
||||
query: String,
|
||||
}
|
||||
|
||||
pub async fn query_handler(
|
||||
State(state): State<AppState>,
|
||||
Extension(user): Extension<User>,
|
||||
Json(query): Json<QueryInput>,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
info!("Received input: {:?}", query);
|
||||
info!("{:?}", user);
|
||||
|
||||
let answer = get_answer_with_references(
|
||||
&state.surreal_db_client,
|
||||
&state.openai_client,
|
||||
&query.query,
|
||||
&user.id,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(
|
||||
Json(serde_json::json!({"answer": answer.content, "references": answer.references}))
|
||||
.into_response(),
|
||||
)
|
||||
}
|
||||
17
src/server/routes/api/queue_length.rs
Normal file
17
src/server/routes/api/queue_length.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use axum::{extract::State, http::StatusCode, response::IntoResponse};
|
||||
use tracing::info;
|
||||
|
||||
use crate::{error::ApiError, server::AppState};
|
||||
|
||||
pub async fn queue_length_handler(
|
||||
State(state): State<AppState>,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
info!("Getting queue length");
|
||||
|
||||
let queue_length = state.rabbitmq_consumer.get_queue_length().await?;
|
||||
|
||||
info!("Queue length: {}", queue_length);
|
||||
|
||||
// Return the queue length with a 200 OK status
|
||||
Ok((StatusCode::OK, queue_length.to_string()))
|
||||
}
|
||||
Reference in New Issue
Block a user