Skip to content

Commit

Permalink
Add write API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
DervexDev committed Apr 11, 2024
1 parent 54119d0 commit bf0dbaf
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/core/changes.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use rbx_dom_weak::types::Ref;
use serde::Serialize;
use serde::{Deserialize, Serialize};

use super::snapshot::{AddedSnapshot, Snapshot, UpdatedSnapshot};

#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Changes {
pub additions: Vec<AddedSnapshot>,
pub updates: Vec<UpdatedSnapshot>,
Expand Down
14 changes: 13 additions & 1 deletion src/core/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ impl Source {
}
}

impl Default for Source {
fn default() -> Self {
Self::new()
}
}

#[derive(Debug, Clone)]
pub struct ResolvedSyncRule {
pub file_type: FileType,
Expand Down Expand Up @@ -278,7 +284,13 @@ impl Context {
}
}

#[derive(Debug, Clone, PartialEq, Serialize)]
impl Default for Context {
fn default() -> Self {
Self::new()
}
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Meta {
/// Instance source that is guaranteed to exist
Expand Down
8 changes: 6 additions & 2 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct Core {
project: Arc<Mutex<Project>>,
tree: Arc<Mutex<Tree>>,
queue: Arc<Queue>,
_processor: Arc<Processor>,
processor: Arc<Processor>,
_vfs: Arc<Vfs>,
}

Expand Down Expand Up @@ -68,7 +68,7 @@ impl Core {
project,
tree,
queue,
_processor: processor,
processor,
_vfs: vfs,
})
}
Expand Down Expand Up @@ -97,6 +97,10 @@ impl Core {
self.queue.clone()
}

pub fn processor(&self) -> Arc<Processor> {
self.processor.clone()
}

/// Create snapshot of the tree
pub fn snapshot(&self) -> Snapshot {
let tree = lock!(self.tree);
Expand Down
17 changes: 14 additions & 3 deletions src/core/processor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crossbeam_channel::select;
use crossbeam_channel::{select, Sender};
use log::{debug, error, info, trace, warn};
use rbx_dom_weak::types::Ref;
use std::{
Expand All @@ -22,7 +22,9 @@ use crate::{
BLACKLISTED_PATHS,
};

pub struct Processor {}
pub struct Processor {
writer: Sender<Changes>,
}

impl Processor {
pub fn new(queue: Arc<Queue>, tree: Arc<Mutex<Tree>>, vfs: Arc<Vfs>, project: Arc<Mutex<Project>>) -> Self {
Expand All @@ -34,23 +36,32 @@ impl Processor {
});

let handler = handler.clone();
let (sender, receiver) = crossbeam_channel::unbounded();

Builder::new()
.name("processor".to_owned())
.spawn(move || {
let vfs_receiver = vfs.receiver();
let client_receiver = receiver;

loop {
select! {
recv(vfs_receiver) -> event => {
handler.on_vfs_event(event.unwrap());
}
recv(client_receiver) -> changes => {
println!("{:#?}", changes);
}
}
}
})
.unwrap();

Self {}
Self { writer: sender }
}

pub fn write(&self, changes: Changes) {
self.writer.send(changes).unwrap();
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/core/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rbx_dom_weak::{
types::{Ref, Variant},
Instance, WeakDom,
};
use serde::Serialize;
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
fmt::{self, Debug, Formatter},
Expand All @@ -12,7 +12,7 @@ use crate::middleware::data::DataSnapshot;

use super::meta::Meta;

#[derive(Clone, Serialize)]
#[derive(Clone, Serialize, Deserialize)]
pub struct Snapshot {
pub id: Ref,
pub meta: Meta,
Expand Down Expand Up @@ -191,7 +191,7 @@ impl Debug for Snapshot {
}
}

#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddedSnapshot {
pub id: Ref,
pub meta: Meta,
Expand All @@ -202,7 +202,7 @@ pub struct AddedSnapshot {
pub children: Vec<Snapshot>,
}

#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdatedSnapshot {
pub id: Ref,
pub meta: Option<Meta>,
Expand Down
2 changes: 2 additions & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod snapshot;
mod stop;
mod subscribe;
mod unsubscribe;
mod write;

async fn default_redirect() -> impl Responder {
web::Redirect::to("/")
Expand Down Expand Up @@ -48,6 +49,7 @@ impl Server {
.service(home::main)
.service(stop::main)
.service(read::main)
.service(write::main)
.service(snapshot::main)
.service(exec::main)
.service(open::main)
Expand Down
28 changes: 28 additions & 0 deletions src/server/write.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use actix_msgpack::MsgPack;
use actix_web::{post, web::Data, HttpResponse, Responder};
use serde::Deserialize;
use std::sync::Arc;

use crate::core::{changes::Changes, Core};

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct Request {
client_id: u32,
changes: Changes,
}

#[post("/write")]
async fn main(request: MsgPack<Request>, core: Data<Arc<Core>>) -> impl Responder {
println!("{:#?}", 1);
let id = request.client_id;
let queue = core.queue();

if !queue.is_subscribed(id) {
return HttpResponse::Unauthorized().body("Not subscribed");
}

core.processor().write(request.changes.clone());

HttpResponse::Ok().body("Written changes successfully")
}

0 comments on commit bf0dbaf

Please sign in to comment.