chore: initial commit
This commit is contained in:
14
backend/Cargo.toml
Normal file
14
backend/Cargo.toml
Normal file
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "backend"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
axum = { workspace = true, features = ["tokio", "ws"] }
|
||||
axum-extra = { version = "0.12.5", features = ["typed-routing"] }
|
||||
futures = { workspace = true }
|
||||
postage = { version = "0.5.0", default-features = false, features = [
|
||||
"futures-traits",
|
||||
] }
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
tokio = { workspace = true, features = ["rt"] }
|
||||
tracing = { workspace = true }
|
||||
75
backend/src/lib.rs
Normal file
75
backend/src/lib.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
use std::num::NonZero;
|
||||
|
||||
use axum::{
|
||||
Router,
|
||||
extract::{State, WebSocketUpgrade, ws::WebSocket},
|
||||
response::IntoResponse,
|
||||
};
|
||||
use axum_extra::routing::{RouterExt, TypedPath};
|
||||
use futures::{SinkExt, Stream, StreamExt};
|
||||
use serde::Deserialize;
|
||||
use tokio::{runtime::Handle, task::LocalSet};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct AppState {
|
||||
websocket_tx: postage::dispatch::Sender<WebSocket>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, TypedPath)]
|
||||
#[typed_path("/ws")]
|
||||
struct Ws {}
|
||||
|
||||
#[tracing::instrument]
|
||||
async fn get_ws(
|
||||
Ws {}: Ws,
|
||||
upgrade: WebSocketUpgrade,
|
||||
State(app_state): State<AppState>,
|
||||
) -> impl IntoResponse {
|
||||
let mut websocket_tx = app_state.websocket_tx;
|
||||
|
||||
upgrade.on_upgrade(|websocket| async move {
|
||||
websocket_tx.send(websocket).await.unwrap();
|
||||
})
|
||||
}
|
||||
|
||||
async fn run_rpc_server(mut websockets: impl Stream<Item = WebSocket> + Unpin) {
|
||||
let local_set = LocalSet::new();
|
||||
|
||||
local_set
|
||||
.run_until(async move {
|
||||
while let Some(websocket) = websockets.next().await {
|
||||
tracing::debug!(?websocket);
|
||||
}
|
||||
})
|
||||
.await;
|
||||
|
||||
local_set.await;
|
||||
}
|
||||
|
||||
fn create_router() -> Router<AppState> {
|
||||
Router::new().typed_get(get_ws)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CreateAppArgs {
|
||||
tokio_runtime_handle: Handle,
|
||||
websocket_channel_capacity: NonZero<usize>,
|
||||
rpc_workers: NonZero<usize>,
|
||||
}
|
||||
|
||||
pub fn create_app(
|
||||
CreateAppArgs {
|
||||
tokio_runtime_handle,
|
||||
websocket_channel_capacity,
|
||||
rpc_workers,
|
||||
}: CreateAppArgs,
|
||||
) -> Router<()> {
|
||||
let (websocket_tx, websocket_rx) = postage::dispatch::channel(websocket_channel_capacity.get());
|
||||
|
||||
for _ in 0..=rpc_workers.get() {
|
||||
let websocket_rx = websocket_rx.clone();
|
||||
}
|
||||
|
||||
let state = AppState { websocket_tx };
|
||||
create_router().with_state(state)
|
||||
}
|
||||
Reference in New Issue
Block a user