(dir: PathBuf, port: u16, open: bool)
| 10 | use tiny_http::{Header, Response, Server}; |
| 11 | |
| 12 | pub fn run(dir: PathBuf, port: u16, open: bool) -> Result<()> { |
| 13 | let server = Server::http(("127.0.0.1", port)).map_err(|e| anyhow!("could not bind port {port}: {e}"))?; |
| 14 | |
| 15 | // Bumped by the watcher; the injected client polls it and reloads on change. |
| 16 | let version = Arc::new(AtomicU64::new(0)); |
| 17 | spawn_watcher(dir.clone(), version.clone()); |
| 18 | |
| 19 | crate::ui::serve_banner(port, &dir); |
| 20 | if open { |
| 21 | let _ = open_url(&format!("http://localhost:{port}")); |
| 22 | } |
| 23 | |
| 24 | for req in server.incoming_requests() { |
| 25 | let url = req.url().split('?').next().unwrap_or("/").to_string(); |
| 26 | if url == "/__livereload" { |
| 27 | let _ = req.respond(Response::from_string(version.load(Ordering::Relaxed).to_string())); |
| 28 | } else { |
| 29 | serve_file(req, &dir, &url); |
| 30 | } |
| 31 | } |
| 32 | Ok(()) |
| 33 | } |
| 34 | |
| 35 | fn serve_file(req: tiny_http::Request, dir: &Path, url: &str) { |
| 36 | let rel = url.trim_start_matches('/'); |
nothing calls this directly
no test coverage detected