2
0
mirror of https://github.com/xvxx/phd synced 2024-11-14 18:12:54 +00:00
This commit is contained in:
dvkt 2019-12-26 21:01:13 -08:00
parent 20e73ffdf1
commit 31ddc48721
2 changed files with 6 additions and 6 deletions

View File

@ -39,7 +39,7 @@ fn main() {
let addr = format!("{}:{}", host, port); let addr = format!("{}:{}", host, port);
println!("-> Serving {} on {}", root, addr); println!("-> Serving {} on {}", root, addr);
if let Err(e) = phd::server::start(addr, &root) { if let Err(e) = phd::server::start(&addr, &root) {
eprintln!("{}", e); eprintln!("{}", e);
} }
} }

View File

@ -1,11 +1,10 @@
use async_std::{ use async_std::{
fs, fs,
io::BufReader, io::BufReader,
net::{TcpListener, TcpStream, ToSocketAddrs}, net::{TcpListener, TcpStream},
prelude::*, prelude::*,
task, task,
}; };
use std::path;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>; type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
@ -20,18 +19,19 @@ async fn listen(addr: &str, root: &str) -> Result<()> {
while let Some(stream) = incoming.next().await { while let Some(stream) = incoming.next().await {
let stream = stream?; let stream = stream?;
println!("-> Connection from: {}", stream.peer_addr()?); println!("-> Connection from: {}", stream.peer_addr()?);
task::spawn(client_loop(stream, &root.to_string())); let root = root.to_string();
task::spawn(client_loop(stream, root));
} }
Ok(()) Ok(())
} }
async fn client_loop(mut stream: TcpStream, root: &str) -> Result<()> { async fn client_loop(mut stream: TcpStream, root: String) -> Result<()> {
let reader = BufReader::new(&stream); let reader = BufReader::new(&stream);
let mut lines = reader.lines(); let mut lines = reader.lines();
if let Some(Ok(line)) = lines.next().await { if let Some(Ok(line)) = lines.next().await {
println!("-> client sent: {:?}", line); println!("-> client sent: {:?}", line);
respond(&mut stream, &line, root).await?; respond(&mut stream, &line, &root).await?;
} }
Ok(()) Ok(())
} }