mirror of
https://github.com/Steffo99/todocolors.git
synced 2024-11-22 00:04:18 +00:00
Create initial board server page
This commit is contained in:
parent
fb70caf7ba
commit
3ee1152c9f
5 changed files with 73 additions and 1 deletions
1
todored/Cargo.lock
generated
1
todored/Cargo.lock
generated
|
@ -1041,6 +1041,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"async-trait",
|
||||
"axum",
|
||||
"futures-util",
|
||||
"log",
|
||||
"micronfig",
|
||||
"pkg-version",
|
||||
|
|
|
@ -8,6 +8,7 @@ edition = "2021"
|
|||
[dependencies]
|
||||
async-trait = "0.1.72"
|
||||
axum = { version = "0.6.19", features = ["ws"] }
|
||||
futures-util = "0.3.28"
|
||||
log = "0.4.19"
|
||||
micronfig = "0.2.0"
|
||||
pkg-version = "1.0.0"
|
||||
|
|
|
@ -21,6 +21,7 @@ async fn main() {
|
|||
.route("/version", get(routes::root::version))
|
||||
.route("/", post(routes::root::healthcheck))
|
||||
.route("/healthcheck", post(routes::root::healthcheck))
|
||||
.route("/board/:board/ws", get(routes::board::websocket))
|
||||
.layer(axum::Extension(rclient))
|
||||
.layer(tower_http::cors::CorsLayer::new()
|
||||
.allow_origin(
|
||||
|
|
68
todored/src/routes/board.rs
Normal file
68
todored/src/routes/board.rs
Normal file
|
@ -0,0 +1,68 @@
|
|||
use axum::Extension;
|
||||
use axum::extract::{Path, WebSocketUpgrade};
|
||||
use axum::extract::ws::{Message, WebSocket};
|
||||
use futures_util::{SinkExt, stream::{StreamExt, SplitSink, SplitStream}};
|
||||
use axum::response::Response;
|
||||
|
||||
|
||||
pub(crate) async fn websocket(
|
||||
Path(board): Path<String>,
|
||||
Extension(rclient): Extension<redis::Client>,
|
||||
ws: WebSocketUpgrade,
|
||||
) -> Response {
|
||||
log::trace!("Received websocket request, upgrading...");
|
||||
ws.on_upgrade(|socket| splitter(socket, rclient, board))
|
||||
}
|
||||
|
||||
async fn splitter(
|
||||
socket: WebSocket,
|
||||
rclient: redis::Client,
|
||||
board: String,
|
||||
) {
|
||||
log::trace!("Splitting socket into two separate pipes...");
|
||||
let (mut sender, receiver) = socket.split();
|
||||
|
||||
log::trace!("Creating Redis connection for the reader thread...");
|
||||
let reader_redis = rclient.get_async_connection().await;
|
||||
if reader_redis.is_err() {
|
||||
log::error!("Could not open Redis connection for the reader thread.");
|
||||
let _ = sender.close().await;
|
||||
return;
|
||||
}
|
||||
let reader_redis = reader_redis.unwrap();
|
||||
log::trace!("Created Redis connection for the reader thread!");
|
||||
|
||||
log::trace!("Creating Redis connection for the writer thread...");
|
||||
let writer_redis = rclient.get_async_connection().await;
|
||||
if writer_redis.is_err() {
|
||||
log::error!("Could not open Redis connection for the writer thread.");
|
||||
let _ = sender.close().await;
|
||||
return;
|
||||
}
|
||||
let writer_redis = writer_redis.unwrap();
|
||||
log::trace!("Created Redis connection for the writer thread!");
|
||||
|
||||
let reader_thread = tokio::spawn(reader(receiver, reader_redis));
|
||||
let writer_thread = tokio::spawn(writer(sender, writer_redis));
|
||||
}
|
||||
|
||||
async fn reader(
|
||||
receiver: SplitStream<WebSocket>,
|
||||
reader_redis: redis::aio::Connection,
|
||||
) -> SplitStream<WebSocket> {
|
||||
log::trace!("Reader thread spawned successfully!");
|
||||
todo!()
|
||||
}
|
||||
|
||||
async fn writer(
|
||||
mut sender: SplitSink<WebSocket, Message>,
|
||||
writer_redis: redis::aio::Connection,
|
||||
) -> SplitSink<WebSocket, Message> {
|
||||
log::trace!("Writer thread spawned successfully!");
|
||||
|
||||
log::trace!("Sending test message...");
|
||||
let _ = sender.send(Message::Text("\"Garasauto\"".to_string())).await;
|
||||
log::trace!("Sent test message!");
|
||||
|
||||
todo!()
|
||||
}
|
|
@ -1 +1,2 @@
|
|||
pub(crate) mod root;
|
||||
pub(crate) mod root;
|
||||
pub(crate) mod board;
|
||||
|
|
Loading…
Reference in a new issue