From 6816a6970676c7eb62a5f7153bccd6f5623c29a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Vold=C5=99ich?= Date: Tue, 27 Aug 2024 15:59:30 +0200 Subject: [PATCH] Reorganising code --- src/lib.rs | 2 ++ src/main.rs | 42 +++++++++++++----------------------------- src/services.rs | 33 +++++++++++++++++++++++++++++++++ src/types.rs | 3 +++ 4 files changed, 51 insertions(+), 29 deletions(-) create mode 100644 src/services.rs create mode 100644 src/types.rs diff --git a/src/lib.rs b/src/lib.rs index c446ac8..2abb63f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,3 @@ pub mod models; +pub mod services; +pub mod types; diff --git a/src/main.rs b/src/main.rs index dbfb0ed..db62886 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,6 @@ -use bcup::models::{Record, ServerConfiguration}; +use bcup::{models::ServerConfiguration, services::BackupServerService}; use futures::StreamExt; -use surrealdb::{ - engine::remote::ws::{Client, Ws}, - opt::PatchOp, - Notification, Result, Surreal, -}; +use surrealdb::{engine::remote::ws::Ws, Notification, Result, Surreal}; #[tokio::main] async fn main() -> surrealdb::Result<()> { @@ -12,47 +8,35 @@ async fn main() -> surrealdb::Result<()> { db.use_ns("test").use_db("test").await?; + let service = BackupServerService::new(db.clone()); + println!("Prepared to listen"); let mut stream = db.select("rest_server").live().await?; while let Some(result) = stream.next().await { - handle(result, &db).await; + handle(result, &service).await; } Ok(()) } -async fn handle(result: Result>, db: &Surreal) { +async fn handle(result: Result>, service: &BackupServerService) { println!("Something to handle"); match result { Ok(notification) => { let conf = ¬ification.data; if conf.state.is_pending() { - process_new_item(conf, db).await; + process_new_item(conf, service).await; } } Err(error) => eprintln!("{error}"), } } -async fn process_new_item(conf: &ServerConfiguration, db: &Surreal) { +async fn process_new_item(conf: &ServerConfiguration, service: &BackupServerService) { println!("The following item has to be processed {:?}", conf); - spawn_restic_container(conf.directory.clone()); - add_proxy_configuration(conf.directory.clone()); - mark_data_as_processed(conf, db).await; -} -async fn mark_data_as_processed(conf: &ServerConfiguration, db: &Surreal) { - println!("Updating status on item id:{:?}", conf.id); - let updated: Option = db - .update(&conf.id) - .patch(PatchOp::replace("/state/now", "processed")) - .await - .unwrap(); - dbg!(updated); -} - -fn spawn_restic_container(path: String) { - println!("[fake] Spawning container for path {:?}", path); -} -fn add_proxy_configuration(path: String) { - println!("[fake] Creating caddy configuration {:?}", path); + service.spawn_restic_container(conf.directory.clone()).await; + service + .add_proxy_configuration(conf.directory.clone()) + .await; + service.mark_data_as_processed(conf).await; } diff --git a/src/services.rs b/src/services.rs new file mode 100644 index 0000000..cc34fad --- /dev/null +++ b/src/services.rs @@ -0,0 +1,33 @@ +use surrealdb::opt::PatchOp; + +use crate::{ + models::{Record, ServerConfiguration}, + types::DB, +}; + +pub struct BackupServerService { + db: DB, +} + +impl BackupServerService { + pub fn new(db: DB) -> Self { + Self { db } + } + pub async fn mark_data_as_processed(&self, conf: &ServerConfiguration) { + println!("Updating status on item id:{:?}", conf.id); + let updated: Option = self + .db + .update(&conf.id) + .patch(PatchOp::replace("/state/now", "processed")) + .await + .unwrap(); + dbg!(updated); + } + + pub async fn spawn_restic_container(&self, path: String) { + println!("[fake] Spawning container for path {:?}", path); + } + pub async fn add_proxy_configuration(&self, path: String) { + println!("[fake] Creating caddy configuration {:?}", path); + } +} diff --git a/src/types.rs b/src/types.rs new file mode 100644 index 0000000..8d82e53 --- /dev/null +++ b/src/types.rs @@ -0,0 +1,3 @@ +use surrealdb::{engine::remote::ws::Client, Surreal}; + +pub type DB = Surreal;