Reorganising code

This commit is contained in:
Martin Voldřich 2024-08-27 15:59:30 +02:00
parent 5d0b03e813
commit 6816a69706
4 changed files with 51 additions and 29 deletions

View file

@ -1 +1,3 @@
pub mod models;
pub mod services;
pub mod types;

View file

@ -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<Notification<ServerConfiguration>>, db: &Surreal<Client>) {
async fn handle(result: Result<Notification<ServerConfiguration>>, service: &BackupServerService) {
println!("Something to handle");
match result {
Ok(notification) => {
let conf = &notification.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<Client>) {
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<Client>) {
println!("Updating status on item id:{:?}", conf.id);
let updated: Option<Record> = 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;
}

33
src/services.rs Normal file
View file

@ -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<Record> = 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);
}
}

3
src/types.rs Normal file
View file

@ -0,0 +1,3 @@
use surrealdb::{engine::remote::ws::Client, Surreal};
pub type DB = Surreal<Client>;