Compare commits
	
		
			No commits in common. "6816a6970676c7eb62a5f7153bccd6f5623c29a3" and "f92fe9bd65d1d17ccc731e61114fa1d6dd52734d" have entirely different histories.
		
	
	
		
			6816a69706
			...
			f92fe9bd65
		
	
		
					 5 changed files with 61 additions and 85 deletions
				
			
		|  | @ -1,3 +0,0 @@ | ||||||
| pub mod models; |  | ||||||
| pub mod services; |  | ||||||
| pub mod types; |  | ||||||
							
								
								
									
										75
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										75
									
								
								src/main.rs
									
									
									
									
									
								
							|  | @ -1,6 +1,41 @@ | ||||||
| use bcup::{models::ServerConfiguration, services::BackupServerService}; |  | ||||||
| use futures::StreamExt; | use futures::StreamExt; | ||||||
| use surrealdb::{engine::remote::ws::Ws, Notification, Result, Surreal}; | use serde::{Deserialize, Serialize}; | ||||||
|  | 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<()> { | ||||||
|  | @ -8,35 +43,47 @@ 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, &service).await; |         handle(result, &db).await; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     Ok(()) |     Ok(()) | ||||||
| } | } | ||||||
| async fn handle(result: Result<Notification<ServerConfiguration>>, service: &BackupServerService) { | async fn handle(result: Result<Notification<ServerConfiguration>>, db: &Surreal<Client>) { | ||||||
|     println!("Something to handle"); |     println!("Something to handle"); | ||||||
|     match result { |     match result { | ||||||
|         Ok(notification) => { |         Ok(notification) => { | ||||||
|             let conf = ¬ification.data; |             let conf = ¬ification.data; | ||||||
|             if conf.state.is_pending() { |             if conf.state.is_pending() { | ||||||
|                 process_new_item(conf, service).await; |                 process_new_item(conf, db).await; | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         Err(error) => eprintln!("{error}"), |         Err(error) => eprintln!("{error}"), | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async fn process_new_item(conf: &ServerConfiguration, service: &BackupServerService) { | async fn process_new_item(conf: &ServerConfiguration, db: &Surreal<Client>) { | ||||||
|     println!("The following item has to be processed {:?}", conf); |     println!("The following item has to be processed {:?}", conf); | ||||||
| 
 |     spawn_restic_container(conf.directory.clone()); | ||||||
|     service.spawn_restic_container(conf.directory.clone()).await; |     add_proxy_configuration(conf.directory.clone()); | ||||||
|     service |     mark_data_as_processed(conf, db).await; | ||||||
|         .add_proxy_configuration(conf.directory.clone()) | } | ||||||
|         .await; | 
 | ||||||
|     service.mark_data_as_processed(conf).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); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,32 +0,0 @@ | ||||||
| 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, |  | ||||||
| } |  | ||||||
|  | @ -1,33 +0,0 @@ | ||||||
| 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); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
|  | @ -1,3 +0,0 @@ | ||||||
| use surrealdb::{engine::remote::ws::Client, Surreal}; |  | ||||||
| 
 |  | ||||||
| pub type DB = Surreal<Client>; |  | ||||||
		Loading…
	
		Reference in a new issue