1
Fork 0
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:
Steffo 2023-07-30 02:24:43 +02:00
parent fb70caf7ba
commit 3ee1152c9f
Signed by: steffo
GPG key ID: 2A24051445686895
5 changed files with 73 additions and 1 deletions

1
todored/Cargo.lock generated
View file

@ -1041,6 +1041,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"axum", "axum",
"futures-util",
"log", "log",
"micronfig", "micronfig",
"pkg-version", "pkg-version",

View file

@ -8,6 +8,7 @@ edition = "2021"
[dependencies] [dependencies]
async-trait = "0.1.72" async-trait = "0.1.72"
axum = { version = "0.6.19", features = ["ws"] } axum = { version = "0.6.19", features = ["ws"] }
futures-util = "0.3.28"
log = "0.4.19" log = "0.4.19"
micronfig = "0.2.0" micronfig = "0.2.0"
pkg-version = "1.0.0" pkg-version = "1.0.0"

View file

@ -21,6 +21,7 @@ async fn main() {
.route("/version", get(routes::root::version)) .route("/version", get(routes::root::version))
.route("/", post(routes::root::healthcheck)) .route("/", post(routes::root::healthcheck))
.route("/healthcheck", post(routes::root::healthcheck)) .route("/healthcheck", post(routes::root::healthcheck))
.route("/board/:board/ws", get(routes::board::websocket))
.layer(axum::Extension(rclient)) .layer(axum::Extension(rclient))
.layer(tower_http::cors::CorsLayer::new() .layer(tower_http::cors::CorsLayer::new()
.allow_origin( .allow_origin(

View 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!()
}

View file

@ -1 +1,2 @@
pub(crate) mod root; pub(crate) mod root;
pub(crate) mod board;