add websocket stub

This commit is contained in:
Ilion Beyst 2021-12-27 22:18:46 +01:00
parent 109e06ecad
commit 5ca8dd4c84
3 changed files with 24 additions and 2 deletions

View file

@ -20,5 +20,5 @@ clap = { version = "3.0.0-rc.8", features = ["derive"] }
chrono = { version = "0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }
rust-embed = "6.3.0" rust-embed = "6.3.0"
axum = "0.4" axum = { version = "0.4", features = ["ws"] }
mime_guess = "2" mime_guess = "2"

View file

@ -1,6 +1,6 @@
use axum::{ use axum::{
body::{boxed, Full}, body::{boxed, Full},
extract::{Extension, Path}, extract::{ws::WebSocket, Extension, Path, WebSocketUpgrade},
handler::Handler, handler::Handler,
http::{header, StatusCode, Uri}, http::{header, StatusCode, Uri},
response::{IntoResponse, Response}, response::{IntoResponse, Response},
@ -36,6 +36,7 @@ pub async fn run(workspace_root: PathBuf) {
// build our application with a route // build our application with a route
let app = Router::new() let app = Router::new()
.route("/", get(index_handler)) .route("/", get(index_handler))
.route("/ws", get(ws_handler))
.route("/api/matches", get(list_matches)) .route("/api/matches", get(list_matches))
.route("/api/matches/:match_id", get(get_match)) .route("/api/matches/:match_id", get(get_match))
.fallback(static_handler.into_service()) .fallback(static_handler.into_service())
@ -50,6 +51,26 @@ pub async fn run(workspace_root: PathBuf) {
.unwrap(); .unwrap();
} }
async fn ws_handler(ws: WebSocketUpgrade) -> impl IntoResponse {
ws.on_upgrade(handle_socket)
}
async fn handle_socket(mut socket: WebSocket) {
while let Some(msg) = socket.recv().await {
let msg = if let Ok(msg) = msg {
msg
} else {
// client disconnected
return;
};
if socket.send(msg).await.is_err() {
// client disconnected
return;
}
}
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct MatchData { struct MatchData {
name: String, name: String,

View file

@ -20,6 +20,7 @@ export default defineConfig({
server: { server: {
proxy: { proxy: {
"/api/": "http://localhost:5000", "/api/": "http://localhost:5000",
"/ws": "ws://localhost:5000/ws",
}, },
}, },
}) })