Last active: a year ago
tokio http simple server
use anyhow::Result;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::TcpListener;
#[tokio::main]
async fn main() -> Result<()> {
let listener = TcpListener::bind("localhost:4000").await?;
loop {
let (mut socket, _) = listener.accept().await?;
tokio::spawn(async move {
let (reader, mut write) = socket.split();
let mut buf = BufReader::new(reader);
let mut req = String::new();
loop {
let count = buf.read_line(&mut req).await.unwrap();
dbg!(count);
if count < 3 {
break;
}
}
dbg!(&req);
write.write_all(b"HTTP/1.1 200 OK\r\n\r\n").await.unwrap();
write.flush().await.unwrap();
});
}
}