diff --git a/Cargo.lock b/Cargo.lock index eb538d5..de0cd96 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "anyhow" +version = "1.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" + [[package]] name = "atomic-waker" version = "1.1.2" @@ -129,6 +135,7 @@ dependencies = [ name = "enclave-server" version = "0.1.0" dependencies = [ + "anyhow", "axum", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index 7a1cdad..1ca7234 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,6 @@ version = "0.1.0" edition = "2024" [dependencies] +anyhow = "1.0.104" axum = { version = "0.8.9", features = ["ws"] } tokio = { version = "1.53.1", features = ["rt", "rt-multi-thread", "macros", "sync"] } diff --git a/src/main.rs b/src/main.rs index cc74903..0b6f08c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,31 @@ -use axum::{Router, routing::get}; +pub mod protocol; + +use axum::{ + Router, + extract::{WebSocketUpgrade, ws::WebSocket}, + response::Response, + routing::{any, get}, +}; + +use crate::protocol::Client; #[tokio::main] async fn main() { - // build our application with a single route - let app = Router::new().route("/", get(|| async { "Hello, World!" })); + let app = Router::new() + .route("/meta", get(|| async { "Hello, World!" })) + .route("/", any(ws_handler)); - // run our app with hyper, listening globally on port 3000 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); axum::serve(listener, app).await.unwrap(); } + +async fn ws_handler(ws: WebSocketUpgrade) -> Response { + ws.on_upgrade(|socket: WebSocket| async { + match Client::initialize(socket).await { + Ok(client) => {} + Err(e) => { + eprintln!("Failed to initialize client {e}") + } + } + }) +} diff --git a/src/protocol/initialize.rs b/src/protocol/initialize.rs new file mode 100644 index 0000000..225e8ff --- /dev/null +++ b/src/protocol/initialize.rs @@ -0,0 +1,11 @@ +use axum::extract::ws::WebSocket; + +impl super::Client { + pub async fn initialize(mut socket: WebSocket) -> anyhow::Result { + Ok(Self { + socket, + meta: super::ClientMeta {}, + pub_key: String::new(), + }) + } +} diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs new file mode 100644 index 0000000..8497cab --- /dev/null +++ b/src/protocol/mod.rs @@ -0,0 +1,22 @@ +use axum::extract::ws::WebSocket; + +pub mod initialize; + +pub struct Client { + pub socket: WebSocket, + pub meta: ClientMeta, + pub pub_key: String, +} + +pub struct ClientMeta {} + +pub enum ClientMessage {} + +pub enum ServerMessage {} + +impl Client { + pub async fn read_socket(mut socket: WebSocket) -> anyhow::Result { + unimplemented!() + // Ok(socket.recv().await?) + } +}