refactoring: wip

This commit is contained in:
Per Stark
2024-11-20 16:09:35 +01:00
parent 2f20d5986d
commit 838aa4c1ec
5 changed files with 100 additions and 14 deletions

View File

@@ -1,5 +1,6 @@
pub mod models;
pub mod rabbitmq;
pub mod routes;
pub mod storage;
pub mod surrealdb;
pub mod utils;

View File

@@ -1,9 +1,19 @@
use axum::{
extract::DefaultBodyLimit, routing::{delete, get, post, put}, Extension, Router
extract::DefaultBodyLimit,
routing::{delete, get, post, put},
Extension, Router,
};
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
use zettle_db::{rabbitmq::{publisher::RabbitMQProducer, RabbitMQConfig}, routes::{file::{delete_file_handler, get_file_handler, update_file_handler, upload_handler}, ingress::ingress_handler, queue_length::queue_length_handler}, surrealdb::SurrealDbClient};
use std::sync::Arc;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
use zettle_db::{
rabbitmq::{publisher::RabbitMQProducer, RabbitMQConfig},
routes::{
file::{delete_file_handler, get_file_handler, update_file_handler, upload_handler},
ingress::ingress_handler,
queue_length::queue_length_handler,
},
surrealdb::SurrealDbClient,
};
#[tokio::main(flavor = "multi_thread", worker_threads = 2)]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -28,7 +38,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Set up database client
let db_client = Arc::new(SurrealDbClient::new().await?);
// Create Axum router
let app = Router::new()
.route("/ingress", post(ingress_handler))
@@ -47,5 +56,3 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

1
src/storage/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod types;

42
src/storage/types/mod.rs Normal file
View File

@@ -0,0 +1,42 @@
pub mod text_content;
#[macro_export]
macro_rules! stored_entity {
($name:ident, $table:expr, {$($field:ident: $ty:ty),*}) => {
use axum::async_trait;
use serde::{Deserialize, Deserializer, Serialize};
use surrealdb::sql::Thing;
fn thing_to_string<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: Deserializer<'de>,
{
let thing = Thing::deserialize(deserializer)?;
Ok(thing.id.to_raw())
}
#[async_trait]
pub trait StoredEntity: Serialize + for<'de> Deserialize<'de> {
fn table_name() -> &'static str;
fn get_id(&self) -> &str;
}
#[derive(Debug, Serialize, Deserialize)]
pub struct $name {
#[serde(deserialize_with = "thing_to_string")]
pub id: String,
$(pub $field: $ty),*
}
#[async_trait]
impl StoredEntity for $name {
fn table_name() -> &'static str {
$table
}
fn get_id(&self) -> &str {
&self.id
}
}
};
}

View File

@@ -0,0 +1,35 @@
use uuid::Uuid;
use crate::models::file_info::FileInfo;
use crate::stored_entity;
stored_entity!(TextContent, "text_content", {
text: String,
file_info: Option<FileInfo>,
instructions: String,
category: String
});
impl TextContent {
pub fn new(text: String, instructions: String, category: String) -> Self {
Self {
id: Uuid::new_v4().to_string(),
text,
file_info: None,
instructions,
category,
}
}
// Other methods...
}
fn test() {
let content = TextContent::new(
"hiho".to_string(),
"instructions".to_string(),
"cat".to_string(),
);
content.get_id();
}