Add WIP controller websocket
This commit is contained in:
parent
ee5d4e2126
commit
131bdeec78
6 changed files with 58 additions and 0 deletions
BIN
Cargo.lock
generated
BIN
Cargo.lock
generated
Binary file not shown.
|
@ -5,7 +5,9 @@ edition = "2018"
|
|||
authors = ["Tobias Reisinger <tobias@msrg.cc>"]
|
||||
|
||||
[dependencies]
|
||||
actix = "0.13"
|
||||
actix-web = "4.4"
|
||||
actix-web-actors = "4.2"
|
||||
|
||||
sqlx = { version = "0.7", features = ["sqlite", "runtime-async-std", "macros", "chrono"] }
|
||||
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
pub mod schedules;
|
||||
pub mod ws;
|
||||
|
|
53
src/handlers/v1/ws/controllers.rs
Normal file
53
src/handlers/v1/ws/controllers.rs
Normal file
|
@ -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<Sqlite>,
|
||||
}
|
||||
|
||||
impl Actor for ControllerWs {
|
||||
type Context = ws::WebsocketContext<Self>;
|
||||
}
|
||||
|
||||
async fn get_schedules(pool: &mut Pool<Sqlite>) -> Result<Vec<Schedule>, ApiError> {
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
||||
Ok(Schedule::get_all(&mut pool_conn).await?)
|
||||
}
|
||||
|
||||
/// Handler for ws::Message message
|
||||
impl StreamHandler<Result<Message, ProtocolError>> for ControllerWs {
|
||||
fn handle(&mut self, msg: Result<Message, ProtocolError>, 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<Pool<Sqlite>>,
|
||||
req: HttpRequest,
|
||||
stream: web::Payload,
|
||||
) -> Result<HttpResponse, ApiError> {
|
||||
let resp = ws::start(
|
||||
ControllerWs {
|
||||
pool: pool.get_ref().clone(),
|
||||
},
|
||||
&req,
|
||||
stream,
|
||||
)
|
||||
.map_err(|_| ApiError::InternalError(String::from("error starting websocket")));
|
||||
println!("{:?}", resp);
|
||||
resp
|
||||
}
|
1
src/handlers/v1/ws/mod.rs
Normal file
1
src/handlers/v1/ws/mod.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub mod controllers;
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue