From 131bdeec78c7f94bbd5f26eb022f5447c4a24582 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Wed, 22 Nov 2023 16:28:46 +0100 Subject: [PATCH] Add WIP controller websocket --- Cargo.lock | Bin 71298 -> 72670 bytes Cargo.toml | 2 ++ src/handlers/v1/mod.rs | 1 + src/handlers/v1/ws/controllers.rs | 53 ++++++++++++++++++++++++++++++ src/handlers/v1/ws/mod.rs | 1 + src/main.rs | 1 + 6 files changed, 58 insertions(+) create mode 100644 src/handlers/v1/ws/controllers.rs create mode 100644 src/handlers/v1/ws/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 769c3b526428d4648e384e854c0e7a7809215f45..5c6963ebc48178d87e251cbe6351d228409a95fc 100644 GIT binary patch delta 524 zcmY+AJ!_Lu7=}rjM3VHwrb(;{C8nqy+H$@>1~qOf2s((6rJQq~b0p9b$Q!iX+WZ3H z8M>*fLkG!VhfZz|g5c&*DcHZ@>|1qlyy1rXy06Jm>GWgi^#L|-`(E%)Dn*%P%v+*0 zwICd!AyFC(NH3&`8aQ}og#seDOpGLHKxr!@Fgz$;$4R|`N5y(|3*u<-GIrN-|G2&6 z_r@{nuGc!U*#LY5?VJ&eN#%s|K|2M(1)F#Zkz1A`1(KA>%akjB z!FjEigZ1lcGq0k@%QGujSzf>gS6g^;K8Gu%*?-g3>7%*%LOF~FyIY4h8iiIt;eP9B zPX`U2gLGC?BbA9FB;h32`Gh8#L={y^S{N!R0m8UWUNY-~kvRx(JbT+d{Lv`fnLeNY zMUX8w%Vbk-8a#XUX+X#Vxs*3 delta 38 ucmcb&ouz3l%Y??wkxZv}HunnfGH#wF^wnT"] [dependencies] +actix = "0.13" actix-web = "4.4" +actix-web-actors = "4.2" sqlx = { version = "0.7", features = ["sqlite", "runtime-async-std", "macros", "chrono"] } diff --git a/src/handlers/v1/mod.rs b/src/handlers/v1/mod.rs index 68f52e6..54b6498 100644 --- a/src/handlers/v1/mod.rs +++ b/src/handlers/v1/mod.rs @@ -1 +1,2 @@ pub mod schedules; +pub mod ws; diff --git a/src/handlers/v1/ws/controllers.rs b/src/handlers/v1/ws/controllers.rs new file mode 100644 index 0000000..d357195 --- /dev/null +++ b/src/handlers/v1/ws/controllers.rs @@ -0,0 +1,53 @@ +use crate::db::schedules::Schedule; +use crate::handlers::errors::ApiError; +use actix::{Actor, StreamHandler}; +use actix_web::{get, web, HttpRequest, HttpResponse}; +use actix_web_actors::ws; +use actix_web_actors::ws::ProtocolError; +use sqlx::{Pool, Sqlite}; +use ws::Message; + +struct ControllerWs { + pub pool: Pool, +} + +impl Actor for ControllerWs { + type Context = ws::WebsocketContext; +} + +async fn get_schedules(pool: &mut Pool) -> Result, ApiError> { + let mut pool_conn = pool.acquire().await?; + + Ok(Schedule::get_all(&mut pool_conn).await?) +} + +/// Handler for ws::Message message +impl StreamHandler> for ControllerWs { + fn handle(&mut self, msg: Result, ctx: &mut Self::Context) { + let schedules = futures::executor::block_on(get_schedules(&mut self.pool)).unwrap(); + let schedules_json = serde_json::to_string(&schedules).unwrap(); + match msg { + Ok(Message::Ping(msg)) => ctx.pong(&msg), + Ok(Message::Text(_text)) => ctx.text(schedules_json), + _ => {} + } + } +} + +#[get("/api/v1/ws/controllers")] +pub async fn index( + pool: web::Data>, + req: HttpRequest, + stream: web::Payload, +) -> Result { + let resp = ws::start( + ControllerWs { + pool: pool.get_ref().clone(), + }, + &req, + stream, + ) + .map_err(|_| ApiError::InternalError(String::from("error starting websocket"))); + println!("{:?}", resp); + resp +} diff --git a/src/handlers/v1/ws/mod.rs b/src/handlers/v1/ws/mod.rs new file mode 100644 index 0000000..f916674 --- /dev/null +++ b/src/handlers/v1/ws/mod.rs @@ -0,0 +1 @@ +pub mod controllers; diff --git a/src/main.rs b/src/main.rs index 6f8ef7b..067ab86 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,6 +47,7 @@ async fn main() -> std::io::Result<()> { .service(handlers::v1::schedules::add_list) .service(handlers::v1::schedules::update) .service(handlers::v1::schedules::delete) + .service(handlers::v1::ws::controllers::index) }) .bind(format!("{}:{}", settings.host, settings.port))? .run()