revised approach

split file operation and ingress functionality
This commit is contained in:
Per Stark
2024-09-24 16:21:27 +02:00
parent eed07c31f9
commit f18bce3c45
18 changed files with 264 additions and 36464 deletions

22
src/routes/file.rs Normal file
View File

@@ -0,0 +1,22 @@
use axum::response::{IntoResponse, Response};
use axum_typed_multipart::TypedMultipart;
use crate::models::files::FileUploadRequest;
// async fn upload_asset(
// TypedMultipart(UploadAssetRequest { image, author }): TypedMultipart<UploadAssetRequest>,
// ) -> StatusCode {
// let file_name = image.metadata.file_name.unwrap_or(String::from("data.bin"));
// let path = Path::new("/tmp").join(author).join(file_name);
// match image.contents.persist(path) {
// Ok(_) => StatusCode::CREATED,
// Err(_) => StatusCode::INTERNAL_SERVER_ERROR,
// }
// }
pub async fn upload_handler(TypedMultipart(input): TypedMultipart<FileUploadRequest>) -> Response {
// let file_name = input.file.metadata.file_name.unwrap_or("newstring".to_string());
//
"Successfully processed".to_string().into_response()
}

33
src/routes/ingress.rs Normal file
View File

@@ -0,0 +1,33 @@
use std::sync::Arc;
use axum::{http::StatusCode, response::IntoResponse, Extension, Json};
use tracing::{error, info};
use crate::{models::ingress::{IngressContent, IngressInput}, rabbitmq::publisher::RabbitMQProducer};
pub async fn ingress_handler(
Extension(producer): Extension<Arc<RabbitMQProducer>>,
Json(input): Json<IngressInput>,
) -> impl IntoResponse {
info!("Recieved input: {:?}", input);
if let Ok(content) = IngressContent::new(input).await {
// Publish content to RabbitMQ (or other system)
match producer.publish(&content).await {
Ok(_) => {
info!("Message published successfully");
"Successfully processed".to_string().into_response()
}
Err(e) => {
error!("Failed to publish message: {:?}", e);
(StatusCode::INTERNAL_SERVER_ERROR, "Failed to publish message").into_response()
}
}
}
else {
error!("Failed to create IngressContent object" );
(StatusCode::INTERNAL_SERVER_ERROR, "Failed to create object").into_response()
}
}

3
src/routes/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
pub mod file;
pub mod ingress;
pub mod queue_length;

View File

@@ -0,0 +1,36 @@
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()
}
}
}