diff --git a/h8r/TODO.md b/h8r/TODO.md index e232842..552ab52 100644 --- a/h8r/TODO.md +++ b/h8r/TODO.md @@ -1,10 +1,11 @@ - [x] server on fixed port responding "oh no" -- [ ] configurable port and working directory +- [x] configurable addr - [ ] string responses - [ ] error page paths - [ ] serve "index.txt" file if exists, else 404 - [ ] serve relative paths with mime type text/plain - [ ] guess mime type +- [ ] configurable working directory - [ ] automatic directory index - [ ] content ranges - [ ] liquid template rendering diff --git a/h8r/src/main.rs b/h8r/src/main.rs index 47ee049..2a206c9 100644 --- a/h8r/src/main.rs +++ b/h8r/src/main.rs @@ -1,6 +1,21 @@ use std::io::Write; use std::net::{Shutdown, TcpListener}; +fn main() -> std::io::Result<()> { + let addr = std::env::var("H8R_ADDR").unwrap_or("127.0.0.1:17321".to_string()); + let listener = TcpListener::bind(&addr).unwrap(); + + eprintln!("h8r: listening at {}", addr); + + for stream in listener.incoming() { + let mut stream = stream.unwrap(); + handle_conn(&mut stream)?; + stream.shutdown(Shutdown::Both)?; + } + + Ok(()) +} + fn handle_conn(stream: &mut impl Write) -> std::io::Result<()> { eprintln!("attempting to respond"); @@ -17,18 +32,6 @@ fn handle_conn(stream: &mut impl Write) -> std::io::Result<()> { Ok(()) } -fn main() -> std::io::Result<()> { - let listener = TcpListener::bind("127.0.0.1:17321").unwrap(); - - for stream in listener.incoming() { - let mut stream = stream.unwrap(); - handle_conn(&mut stream)?; - stream.shutdown(Shutdown::Both)?; - } - - Ok(()) -} - #[cfg(test)] mod tests { use super::*;