Server structure

This commit is contained in:
2026-08-11 22:07:02 -04:00
parent 79366a28d0
commit c910769eb3
5 changed files with 278 additions and 28 deletions
+41
View File
@@ -0,0 +1,41 @@
use std::sync::Arc;
use axum::{
extract::{WebSocketUpgrade, ws::WebSocket},
response::Response,
};
use ed25519_dalek::SigningKey;
use crate::protocol::Client;
pub struct Server {
pub key: SigningKey,
}
impl Server {
pub async fn new() -> anyhow::Result<Arc<Self>> {
Ok(Arc::new(Self {
key: crate::signature::get().await?,
}))
}
}
impl Server {
pub async fn ws_handler(self: &Arc<Self>, ws: WebSocketUpgrade) -> Response {
ws.on_upgrade(|socket: WebSocket| async {
match Client::initialize(socket).await {
Ok(mut client) => {
if let Err(e) = client.read_loop().await {
eprintln!("Failed to handle client: {e}");
} else {
println!("Client connection closed")
}
}
Err(e) => {
eprintln!("Failed to initialize client: {e}")
}
}
})
}
}