From 147ba89b42557edd45568afc87cb7068707a345f Mon Sep 17 00:00:00 2001 From: mehranhydary Date: Thu, 5 Sep 2024 20:19:01 -0400 Subject: [PATCH] Simplifying server for now --- server/bin/_main.rs | 72 +++++++++++++++++++++++++++++++++++++ server/bin/main.rs | 88 +++++++++------------------------------------ 2 files changed, 88 insertions(+), 72 deletions(-) create mode 100644 server/bin/_main.rs diff --git a/server/bin/_main.rs b/server/bin/_main.rs new file mode 100644 index 00000000..0202d3da --- /dev/null +++ b/server/bin/_main.rs @@ -0,0 +1,72 @@ +// NOTE: This is not being used right now, please ignore +use std::net::TcpListener; +use std::net::TcpStream; +use std::io::prelude::*; +use std::fs; +use std::thread; +use std::time::Duration; +use milady_pool_lib::fibonacci; + +fn main () { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + + let pool = ThreadPool::new(4); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } +} + +fn handle_connection(mut stream: TcpStream) { + let mut buffer = [0; 1024]; + stream.read(&mut buffer).unwrap(); + + let get = b"GET / HTTP/1.1\r\n"; + let sleep = b"GET /sleep HTTP/1.1\r\n"; + let post = b"POST /log HTTP/1.1\r\n"; + + + if buffer.starts_with(post) { + let contents = String::from_utf8_lossy(&buffer[..]); + println!("Received POST request with data: {}", contents); + // TODO: Come back to this + + } else if buffer.starts_with(get) { + send_response(stream, "HTTP/1.1 200 OK", "server/index.html"); + } else if buffer.starts_with(sleep) { + thread::sleep(Duration::from_secs(5)); + send_response(stream, "HTTP/1.1 200 OK", "server/index.html"); + } else if buffer.starts_with(get) { + let id = 5; + let (a, b) = call_fibonacci(id); + let response = format!("HTTP/1.1 200 OK\r\n\r\nThe fibonacci number at position {} is: {} and {}", id, a, b); + stream.write(response.as_bytes()).unwrap(); + stream.flush().unwrap(); + } else { + send_response(stream, "HTTP/1.1 404 NOT FOUND", "server/404.html"); + } +} + +fn send_response(mut stream: TcpStream, status_line: &str, filename: &str) { + let contents = fs::read_to_string(filename).unwrap(); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); + stream.write(response.as_bytes()).unwrap(); + stream.flush().unwrap(); +} + +fn call_fibonacci(n: u32) -> (u32, u32) { + let (a, b)= fibonacci(n); + (a, b) +} + + + diff --git a/server/bin/main.rs b/server/bin/main.rs index bfae788a..aed120cd 100644 --- a/server/bin/main.rs +++ b/server/bin/main.rs @@ -1,13 +1,5 @@ -// use std::net::TcpListener; -use std::net::TcpStream; -use std::io::prelude::*; -use std::fs; -use std::thread; use std::time::Duration; -use milady_pool_server::ThreadPool; -use milady_pool_lib::fibonacci; -// New use axum::{ extract::{Path, State}, http::StatusCode, @@ -17,6 +9,8 @@ use axum::{ use serde::{Deserialize, Serialize}; use serde_json::json; use tokio::net::TcpListener; +use milady_pool_lib::fibonacci; + #[tokio::main] async fn main() { @@ -26,73 +20,23 @@ async fn main() { println!("Server listening on port 7878"); let app = Router::new() - .route("/", get(|| async { "Welcome to Milady Pool ZKP API!" })); - // .route("/fibonacci/:id", get(fibonacci_handler)) - // .route("/log", post(log_handler)); + .route("/", get(|| async { "Welcome to Milady Pool ZKP API!" })) + .route("/order", post(order_handler)); + axum::serve(listener,app).await.expect("Server failed to start"); - } -// fn main () { -// let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); - -// let pool = ThreadPool::new(4); - -// for stream in listener.incoming() { -// let stream = stream.unwrap(); - -// pool.execute(|| { -// handle_connection(stream); -// }); -// } -// } - -fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 1024]; - stream.read(&mut buffer).unwrap(); - - let get = b"GET / HTTP/1.1\r\n"; - let sleep = b"GET /sleep HTTP/1.1\r\n"; - let post = b"POST /log HTTP/1.1\r\n"; - - if buffer.starts_with(post) { - let contents = String::from_utf8_lossy(&buffer[..]); - println!("Received POST request with data: {}", contents); - // TODO: Come back to this - - } else if buffer.starts_with(get) { - send_response(stream, "HTTP/1.1 200 OK", "server/index.html"); - } else if buffer.starts_with(sleep) { - thread::sleep(Duration::from_secs(5)); - send_response(stream, "HTTP/1.1 200 OK", "server/index.html"); - } else if buffer.starts_with(get) { - let id = 5; - let (a, b) = call_fibonacci(id); - let response = format!("HTTP/1.1 200 OK\r\n\r\nThe fibonacci number at position {} is: {} and {}", id, a, b); - stream.write(response.as_bytes()).unwrap(); - stream.flush().unwrap(); - } else { - send_response(stream, "HTTP/1.1 404 NOT FOUND", "server/404.html"); - } -} - -fn send_response(mut stream: TcpStream, status_line: &str, filename: &str) { - let contents = fs::read_to_string(filename).unwrap(); - let response = format!( - "{}\r\nContent-Length: {}\r\n\r\n{}", - status_line, - contents.len(), - contents - ); - stream.write(response.as_bytes()).unwrap(); - stream.flush().unwrap(); +async fn order_handler( + Json(task): Json, +) -> Result<(StatusCode, String), (StatusCode, String)>{ + // TODO: Handle the order (create the proof and trigger a + // smart contract transaction via avs) + Ok(( + StatusCode::CREATED, + json!({"success":true, "data":""}).to_string(), + )) } -fn call_fibonacci(n: u32) -> (u32, u32) { - let (a, b)= fibonacci(n); - (a, b) -} - - - +#[derive(Deserialize)] +struct Order {} \ No newline at end of file