-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from kanekoshoyu/feature/ws_framework
Feature/ws framework
- Loading branch information
Showing
10 changed files
with
133 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
[package] | ||
name = "chaiwala" | ||
version = "0.1.1" | ||
version = "0.1.2" | ||
edition = "2021" | ||
authors = ["Sho Kaneko <[email protected]>"] | ||
description = "Endpoints for Kucoin Arbitrage Deployment" | ||
description = "Endpoints for Kucoin Arbitrage ECS Deployment" | ||
repository = "https://github.com/kanekoshoyu/chaiwala" | ||
license = "MIT" | ||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Rust 2021 latest image | ||
FROM rust:1.71 | ||
|
||
# Metadata | ||
LABEL maintainer="[email protected]" | ||
|
||
# The /app directory should act as the main application directory | ||
WORKDIR /app | ||
|
||
# Either clone the repo remotely | ||
# RUN git clone https://github.com/kanekoshoyu/chaiwala.git | ||
# WORKDIR /app/chaiwala | ||
|
||
# Or copy the files locally | ||
COPY ./ ./ | ||
|
||
# Build release | ||
RUN cargo build --bin ws_broadcast --release | ||
|
||
# Open application endpoints | ||
EXPOSE 3000 | ||
|
||
# Run the binary | ||
# CMD ["cargo", "run", "--release", "--bin", "ws_broadcast"] | ||
CMD ["./target/release/ws_broadcast"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[toolchain] | ||
channel = "nightly" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use axum::extract::ws::{Message, WebSocket, WebSocketUpgrade}; | ||
use axum::extract::TypedHeader; | ||
use axum::Extension; | ||
use std::sync::Arc; | ||
use tokio::sync::{broadcast, Mutex}; | ||
|
||
/// HTTP handler, returns plain text | ||
pub async fn handle_http(user_agent: Option<TypedHeader<headers::UserAgent>>) -> &'static str { | ||
log::info!("Connected: {}", user_agent.unwrap().as_str()); | ||
"Hello, World!" | ||
} | ||
|
||
/// WebSocket handler, returns response from callback | ||
pub async fn handle_ws_broadcast( | ||
ws: WebSocketUpgrade, | ||
user_agent: Option<TypedHeader<headers::UserAgent>>, | ||
rx: Extension<Arc<Mutex<broadcast::Receiver<i32>>>>, | ||
) -> impl axum::response::IntoResponse { | ||
// callback upon reception | ||
log::info!("Connected: {}", user_agent.unwrap().as_str()); | ||
|
||
ws.on_upgrade(move |socket: WebSocket| ws_upgrade_callback(socket, rx.0)) | ||
} | ||
|
||
/// Websocket Callback that sends received data from broadcast | ||
async fn ws_upgrade_callback(mut ws: WebSocket, rx: Arc<Mutex<broadcast::Receiver<i32>>>) { | ||
// TODO spawn both the broadcast loop and the receiver loop for real-time control | ||
// while websocket is on connection | ||
while let Ok(number) = rx.lock().await.recv().await { | ||
ws.send(Message::Text(format!("{number}"))).await.unwrap(); | ||
} | ||
// sends Message::Close() | ||
ws.close().await.unwrap(); | ||
} | ||
|
||
/// WebSocket handler, returns response from callback | ||
pub async fn handle_ws_pingpong( | ||
ws: WebSocketUpgrade, | ||
user_agent: Option<TypedHeader<headers::UserAgent>>, | ||
) -> impl axum::response::IntoResponse { | ||
if let Some(TypedHeader(user_agent)) = user_agent { | ||
log::info!("Connected: {}", user_agent.as_str()); | ||
} | ||
|
||
ws.on_upgrade(ws_callback_pingpong) | ||
} | ||
|
||
/// Websocket Callback that sends received data | ||
async fn ws_callback_pingpong(mut socket: WebSocket) { | ||
loop { | ||
let res = socket.recv().await; | ||
if res.is_none() { | ||
break; | ||
} | ||
let res = res.unwrap(); | ||
if let Err(e) = res { | ||
log::warn!("Failed receiving message {e}"); | ||
break; | ||
} | ||
let msg = res.unwrap(); | ||
log::info!("RX: {:?}", msg); | ||
if let Message::Close(_) = msg { | ||
log::warn!("Close message received"); | ||
break; | ||
} | ||
if let Message::Text(text) = msg { | ||
let res = socket.send(Message::Text(text.clone())).await; | ||
if res.is_err() { | ||
log::warn!("Failed sending message, disconnecting client"); | ||
return; | ||
} | ||
log::info!("TX: {:?}", text); | ||
} | ||
} | ||
log::warn!("Escaping handler"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
/// Logger intialization | ||
pub mod logger; | ||
|
||
/// Handlers | ||
pub mod handler; |