From d1610d133197198dc0ac51764771da77a8a68ae0 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 12 Apr 2025 17:28:03 +0000 Subject: [PATCH] service (#11) Reviewed-on: https://git.kundeng.us/phoenix/songparser/pulls/11 Co-authored-by: phoenix Co-committed-by: phoenix --- Cargo.toml | 1 + src/main.rs | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6e920aa..4ca89b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,5 @@ version = "0.1.0" edition = "2024" [dependencies] +tokio = { version = "1.44.1", features = ["full"] } icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.2.0" } diff --git a/src/main.rs b/src/main.rs index e7a11a9..ddbab38 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,38 @@ -fn main() { - println!("Hello, world!"); +use std::error::Error; +use tokio::io::AsyncReadExt; +use tokio::net::{TcpListener, TcpStream}; +use tokio::spawn; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let listener = TcpListener::bind("127.0.0.1:8080").await?; + println!("API calling service listening on 127.0.0.1:8080"); + + loop { + let (stream, addr) = listener.accept().await?; + println!("Accepted connection from: {}", addr); + + spawn(async move { + if let Err(e) = handle_connection(stream).await { + eprintln!("Error handling connection from {}: {}", addr, e); + } + }); + } +} + +async fn handle_connection(mut stream: TcpStream) -> Result<(), Box> { + let mut buffer = [0; 1024]; + + loop { + let n = stream.read(&mut buffer).await?; + + if n == 0 { + break; // Connection closed + } + + let request_data = String::from_utf8_lossy(&buffer[..n]).trim().to_string(); + println!("Received request: {}", request_data); + } + + Ok(()) }