mirror of
https://github.com/perstarkse/minne.git
synced 2026-03-26 03:11:34 +01:00
refactor: moved routes
This commit is contained in:
1
src/server/mod.rs
Normal file
1
src/server/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod routes;
|
||||
122
src/server/routes/file.rs
Normal file
122
src/server/routes/file.rs
Normal file
@@ -0,0 +1,122 @@
|
||||
use crate::storage::{
|
||||
db::SurrealDbClient,
|
||||
types::file_info::{FileError, FileInfo},
|
||||
};
|
||||
use axum::{extract::Path, response::IntoResponse, Extension, Json};
|
||||
use axum_typed_multipart::{FieldData, TryFromMultipart, TypedMultipart};
|
||||
use serde_json::json;
|
||||
use std::sync::Arc;
|
||||
use tempfile::NamedTempFile;
|
||||
use tracing::info;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, TryFromMultipart)]
|
||||
pub struct FileUploadRequest {
|
||||
#[form_data(limit = "100000")] // Example limit: ~100 KB
|
||||
pub file: FieldData<NamedTempFile>,
|
||||
}
|
||||
|
||||
/// Handler to upload a new file.
|
||||
///
|
||||
/// Route: POST /file
|
||||
pub async fn upload_handler(
|
||||
Extension(db_client): Extension<Arc<SurrealDbClient>>,
|
||||
TypedMultipart(input): TypedMultipart<FileUploadRequest>,
|
||||
) -> Result<impl IntoResponse, FileError> {
|
||||
info!("Received an upload request");
|
||||
|
||||
// Process the file upload
|
||||
let file_info = FileInfo::new(input.file, &db_client).await?;
|
||||
|
||||
// Prepare the response JSON
|
||||
let response = json!({
|
||||
"uuid": file_info.uuid,
|
||||
"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)))
|
||||
}
|
||||
|
||||
/// Handler to retrieve file information by UUID.
|
||||
///
|
||||
/// Route: GET /file/:uuid
|
||||
pub async fn get_file_handler(
|
||||
Extension(db_client): Extension<Arc<SurrealDbClient>>,
|
||||
Path(uuid_str): Path<String>,
|
||||
) -> Result<impl IntoResponse, FileError> {
|
||||
// Parse UUID
|
||||
let uuid = Uuid::parse_str(&uuid_str).map_err(|_| FileError::InvalidUuid(uuid_str.clone()))?;
|
||||
|
||||
// Retrieve FileInfo
|
||||
let file_info = FileInfo::get_by_uuid(uuid, &db_client).await?;
|
||||
|
||||
// Prepare the response JSON
|
||||
let response = json!({
|
||||
"uuid": file_info.uuid,
|
||||
"sha256": file_info.sha256,
|
||||
"path": file_info.path,
|
||||
"mime_type": file_info.mime_type,
|
||||
});
|
||||
|
||||
info!("Retrieved FileInfo: {:?}", file_info);
|
||||
|
||||
// Return the response with HTTP 200
|
||||
Ok((axum::http::StatusCode::OK, Json(response)))
|
||||
}
|
||||
|
||||
/// Handler to update an existing file by UUID.
|
||||
///
|
||||
/// Route: PUT /file/:uuid
|
||||
pub async fn update_file_handler(
|
||||
Extension(db_client): Extension<Arc<SurrealDbClient>>,
|
||||
Path(uuid_str): Path<String>,
|
||||
TypedMultipart(input): TypedMultipart<FileUploadRequest>,
|
||||
) -> Result<impl IntoResponse, FileError> {
|
||||
// Parse UUID
|
||||
let uuid = Uuid::parse_str(&uuid_str).map_err(|_| FileError::InvalidUuid(uuid_str.clone()))?;
|
||||
|
||||
// Update the file
|
||||
let updated_file_info = FileInfo::update(uuid, input.file, &db_client).await?;
|
||||
|
||||
// Prepare the response JSON
|
||||
let response = json!({
|
||||
"uuid": updated_file_info.uuid,
|
||||
"sha256": updated_file_info.sha256,
|
||||
"path": updated_file_info.path,
|
||||
"mime_type": updated_file_info.mime_type,
|
||||
});
|
||||
|
||||
info!("File updated successfully: {:?}", updated_file_info);
|
||||
|
||||
// Return the response with HTTP 200
|
||||
Ok((axum::http::StatusCode::OK, Json(response)))
|
||||
}
|
||||
|
||||
/// Handler to delete a file by UUID.
|
||||
///
|
||||
/// Route: DELETE /file/:uuid
|
||||
pub async fn delete_file_handler(
|
||||
Extension(db_client): Extension<Arc<SurrealDbClient>>,
|
||||
Path(uuid_str): Path<String>,
|
||||
) -> Result<impl IntoResponse, FileError> {
|
||||
// Parse UUID
|
||||
let uuid = Uuid::parse_str(&uuid_str).map_err(|_| FileError::InvalidUuid(uuid_str.clone()))?;
|
||||
|
||||
// Delete the file
|
||||
FileInfo::delete(uuid, &db_client).await?;
|
||||
|
||||
info!("Deleted file with UUID: {}", uuid);
|
||||
|
||||
// Prepare the response JSON
|
||||
let response = json!({
|
||||
"message": "File deleted successfully",
|
||||
});
|
||||
|
||||
// Return the response with HTTP 204 No Content
|
||||
Ok((axum::http::StatusCode::NO_CONTENT, Json(response)))
|
||||
}
|
||||
41
src/server/routes/ingress.rs
Normal file
41
src/server/routes/ingress.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
use crate::{
|
||||
ingress::types::ingress_input::{create_ingress_objects, IngressInput},
|
||||
rabbitmq::publisher::RabbitMQProducer,
|
||||
storage::db::SurrealDbClient,
|
||||
};
|
||||
use axum::{http::StatusCode, response::IntoResponse, Extension, Json};
|
||||
use std::sync::Arc;
|
||||
use tracing::{error, info};
|
||||
|
||||
pub async fn ingress_handler(
|
||||
Extension(producer): Extension<Arc<RabbitMQProducer>>,
|
||||
Extension(db_client): Extension<Arc<SurrealDbClient>>,
|
||||
Json(input): Json<IngressInput>,
|
||||
) -> impl IntoResponse {
|
||||
info!("Received input: {:?}", input);
|
||||
|
||||
match create_ingress_objects(input, &db_client).await {
|
||||
Ok(objects) => {
|
||||
for object in objects {
|
||||
match producer.publish(&object).await {
|
||||
Ok(_) => {
|
||||
info!("Message published successfully");
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to publish message: {:?}", e);
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"Failed to publish message",
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
}
|
||||
}
|
||||
StatusCode::OK.into_response()
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to process input: {:?}", e);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, "Failed to process input").into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
4
src/server/routes/mod.rs
Normal file
4
src/server/routes/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
pub mod file;
|
||||
pub mod ingress;
|
||||
pub mod query;
|
||||
pub mod queue_length;
|
||||
17
src/server/routes/query.rs
Normal file
17
src/server/routes/query.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use crate::storage::db::SurrealDbClient;
|
||||
use axum::{http::StatusCode, response::IntoResponse, Extension, Json};
|
||||
use serde::Deserialize;
|
||||
use std::sync::Arc;
|
||||
use tracing::{error, info};
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct QueryInput {
|
||||
query: String,
|
||||
}
|
||||
|
||||
pub async fn query_handler(
|
||||
Extension(db_client): Extension<Arc<SurrealDbClient>>,
|
||||
Json(query): Json<QueryInput>,
|
||||
) -> impl IntoResponse {
|
||||
info!("Received input: {:?}", query);
|
||||
}
|
||||
42
src/server/routes/queue_length.rs
Normal file
42
src/server/routes/queue_length.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use axum::{
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use tracing::{error, info};
|
||||
|
||||
use crate::rabbitmq::{consumer::RabbitMQConsumer, RabbitMQConfig};
|
||||
|
||||
pub async fn queue_length_handler() -> Response {
|
||||
info!("Getting queue length");
|
||||
|
||||
// Set up RabbitMQ config
|
||||
let config = RabbitMQConfig {
|
||||
amqp_addr: "amqp://localhost".to_string(),
|
||||
exchange: "my_exchange".to_string(),
|
||||
queue: "my_queue".to_string(),
|
||||
routing_key: "my_key".to_string(),
|
||||
};
|
||||
|
||||
// Create a new consumer
|
||||
match RabbitMQConsumer::new(&config).await {
|
||||
Ok(consumer) => {
|
||||
info!("Consumer connected to RabbitMQ");
|
||||
|
||||
// Get the queue length
|
||||
let queue_length = consumer.queue.message_count();
|
||||
|
||||
info!("Queue length: {}", queue_length);
|
||||
|
||||
// Return the queue length with a 200 OK status
|
||||
(StatusCode::OK, queue_length.to_string()).into_response()
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to create consumer: {:?}", e);
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"Failed to connect to RabbitMQ".to_string(),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user