Compare commits

...

2 Commits

Author SHA1 Message Date
Martin Voldřich
6816a69706 Reorganising code 2024-08-27 15:59:30 +02:00
Martin Voldřich
5d0b03e813 Moving models into its own module 2024-08-27 15:34:43 +02:00
5 changed files with 84 additions and 60 deletions

3
src/lib.rs Normal file
View File

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

View File

@ -1,41 +1,6 @@
use bcup::{models::ServerConfiguration, services::BackupServerService};
use futures::StreamExt; use futures::StreamExt;
use serde::{Deserialize, Serialize}; use surrealdb::{engine::remote::ws::Ws, Notification, Result, Surreal};
use surrealdb::{
engine::remote::ws::{Client, Ws},
opt::PatchOp,
sql::Thing,
Notification, Result, Surreal,
};
#[derive(Debug, Serialize, Deserialize)]
struct State {
now: String,
required: String,
}
impl State {
fn is_pending(&self) -> bool {
self.now == "pending".as_ref()
}
}
#[derive(Debug, Serialize, Deserialize)]
struct ServerConfiguration {
id: Thing,
name: String,
domain: String,
directory: String,
volume: String,
repo_password: String,
rest_password: String,
state: State,
}
#[derive(Debug, Deserialize)]
struct Record {
#[allow(dead_code)]
id: Thing,
}
#[tokio::main] #[tokio::main]
async fn main() -> surrealdb::Result<()> { async fn main() -> surrealdb::Result<()> {
@ -43,47 +8,35 @@ async fn main() -> surrealdb::Result<()> {
db.use_ns("test").use_db("test").await?; db.use_ns("test").use_db("test").await?;
let service = BackupServerService::new(db.clone());
println!("Prepared to listen"); println!("Prepared to listen");
let mut stream = db.select("rest_server").live().await?; let mut stream = db.select("rest_server").live().await?;
while let Some(result) = stream.next().await { while let Some(result) = stream.next().await {
handle(result, &db).await; handle(result, &service).await;
} }
Ok(()) Ok(())
} }
async fn handle(result: Result<Notification<ServerConfiguration>>, db: &Surreal<Client>) { async fn handle(result: Result<Notification<ServerConfiguration>>, service: &BackupServerService) {
println!("Something to handle"); println!("Something to handle");
match result { match result {
Ok(notification) => { Ok(notification) => {
let conf = &notification.data; let conf = &notification.data;
if conf.state.is_pending() { if conf.state.is_pending() {
process_new_item(conf, db).await; process_new_item(conf, service).await;
} }
} }
Err(error) => eprintln!("{error}"), 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); 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>) { service.spawn_restic_container(conf.directory.clone()).await;
println!("Updating status on item id:{:?}", conf.id); service
let updated: Option<Record> = db .add_proxy_configuration(conf.directory.clone())
.update(&conf.id) .await;
.patch(PatchOp::replace("/state/now", "processed")) service.mark_data_as_processed(conf).await;
.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);
} }

32
src/models.rs Normal file
View File

@ -0,0 +1,32 @@
use serde::{Deserialize, Serialize};
use surrealdb::sql::Thing;
#[derive(Debug, Serialize, Deserialize)]
pub struct State {
pub now: String,
pub required: String,
}
impl State {
pub fn is_pending(&self) -> bool {
self.now == "pending".as_ref()
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ServerConfiguration {
pub id: Thing,
pub name: String,
pub domain: String,
pub directory: String,
pub volume: String,
pub repo_password: String,
pub rest_password: String,
pub state: State,
}
#[derive(Debug, Deserialize)]
pub struct Record {
#[allow(dead_code)]
pub id: Thing,
}

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>;