Skip to content

Commit

Permalink
Simplifying server for now
Browse files Browse the repository at this point in the history
  • Loading branch information
mehranhydary committed Sep 6, 2024
1 parent e80a2f1 commit 147ba89
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 72 deletions.
72 changes: 72 additions & 0 deletions server/bin/_main.rs
Original file line number Diff line number Diff line change
@@ -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)
}



88 changes: 16 additions & 72 deletions server/bin/main.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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() {
Expand All @@ -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<Order>,
) -> 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 {}

0 comments on commit 147ba89

Please sign in to comment.