Configurable addr

This commit is contained in:
2026-04-12 22:06:49 -04:00
parent 5df6b9e233
commit c2ceee564b
2 changed files with 17 additions and 13 deletions
+2 -1
View File
@@ -1,10 +1,11 @@
- [x] server on fixed port responding "oh no" - [x] server on fixed port responding "oh no"
- [ ] configurable port and working directory - [x] configurable addr
- [ ] string responses - [ ] string responses
- [ ] error page paths - [ ] error page paths
- [ ] serve "index.txt" file if exists, else 404 - [ ] serve "index.txt" file if exists, else 404
- [ ] serve relative paths with mime type text/plain - [ ] serve relative paths with mime type text/plain
- [ ] guess mime type - [ ] guess mime type
- [ ] configurable working directory
- [ ] automatic directory index - [ ] automatic directory index
- [ ] content ranges - [ ] content ranges
- [ ] liquid template rendering - [ ] liquid template rendering
+15 -12
View File
@@ -1,6 +1,21 @@
use std::io::Write; use std::io::Write;
use std::net::{Shutdown, TcpListener}; 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<()> { fn handle_conn(stream: &mut impl Write) -> std::io::Result<()> {
eprintln!("attempting to respond"); eprintln!("attempting to respond");
@@ -17,18 +32,6 @@ fn handle_conn(stream: &mut impl Write) -> std::io::Result<()> {
Ok(()) 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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;