From 9992e3c73c0ff23df2c494316488b5e45c03f5f8 Mon Sep 17 00:00:00 2001 From: Cecylia Bocovich Date: Tue, 15 Nov 2022 19:11:16 -0500 Subject: [PATCH] Simple lox server Right now only issues open invite tokens --- crates/lox-distributor/.gitignore | 1 + crates/lox-distributor/Cargo.toml | 16 +++++++ crates/lox-distributor/src/main.rs | 71 ++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 crates/lox-distributor/.gitignore create mode 100644 crates/lox-distributor/Cargo.toml create mode 100644 crates/lox-distributor/src/main.rs diff --git a/crates/lox-distributor/.gitignore b/crates/lox-distributor/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/crates/lox-distributor/.gitignore @@ -0,0 +1 @@ +/target diff --git a/crates/lox-distributor/Cargo.toml b/crates/lox-distributor/Cargo.toml new file mode 100644 index 0000000..77910e3 --- /dev/null +++ b/crates/lox-distributor/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "lox-server" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +hyper = "0.13" +tokio = { version = "0.2", features = ["macros", "signal"] } + +serde = "1" +serde_with = "1.9.1" +serde_json = "1.0.87" + +lox = { git = "https://git-crysp.uwaterloo.ca/iang/lox.git" } diff --git a/crates/lox-distributor/src/main.rs b/crates/lox-distributor/src/main.rs new file mode 100644 index 0000000..ce9033b --- /dev/null +++ b/crates/lox-distributor/src/main.rs @@ -0,0 +1,71 @@ +use std::{ + net::SocketAddr, + sync::{Arc, Mutex}, + convert::Infallible, +}; + +use hyper::{ + service::{make_service_fn, service_fn}, + Body, Request, Server, Response, +}; + +use serde::{Deserialize, Serialize}; +use serde_with::serde_as; +use serde_json; + +use lox::{BridgeDb, OPENINV_LENGTH}; + +#[serde_as] +#[derive(Serialize, Deserialize)] +pub struct Invite { + #[serde_as(as = "[_; OPENINV_LENGTH]")] + invite: [u8; OPENINV_LENGTH], +} + +#[tokio::main] +async fn main() { + + // Create and initialize a new db + let mut bridgedb = BridgeDb::new(); + for i in &[1u32, 5, 7, 12, 19, 20, 22] { + bridgedb.insert_openinv(*i); + } + + let db = Arc::new(Mutex::new(bridgedb)); + + let new_service = make_service_fn(move |_conn| { + let db = db.clone(); + async move { + Ok::<_, Infallible>(service_fn(move |_req: Request| { + let db = db.clone(); + async move { + Ok::<_, Infallible>(generate_invite(db)) + } + })) + } + }); + + let addr = SocketAddr::from(([127, 0, 0, 1], 8001)); + let server = Server::bind(&addr).serve(new_service); + let graceful = server.with_graceful_shutdown(shutdown_signal()); + println!("Listening on {}", addr); + + if let Err(e) = graceful.await { + eprintln!("server error: {}", e); + } +} + +async fn shutdown_signal() { + tokio::signal::ctrl_c() + .await + .expect("failed to listen for ctrl+c signal"); +} + +fn generate_invite(db: Arc>) -> Response { + let obj = db.lock().unwrap(); + let invite = Invite { + invite: obj.invite(), + }; + let token = serde_json::to_string(&invite).unwrap(); + Response::new(Body::from(token)) +}