Serve the harness at `/` and the manifest at `/packages.json` on a free loopback port. The thread is a daemon.
(packages: String)
| 204 | |
| 205 | /// Serve the harness at `/` and the manifest at `/packages.json` on a free loopback port. The thread is a daemon. |
| 206 | fn serve(packages: String) -> Result<u16> { |
| 207 | let server = Server::http("127.0.0.1:0").map_err(|e| anyhow!("starting local server: {e}"))?; |
| 208 | let port = server |
| 209 | .server_addr() |
| 210 | .to_ip() |
| 211 | .ok_or_else(|| anyhow!("local server has no TCP address"))? |
| 212 | .port(); |
| 213 | |
| 214 | thread::spawn(move || { |
| 215 | for req in server.incoming_requests() { |
| 216 | let path = req.url().split('?').next().unwrap_or("/"); |
| 217 | let resp = match path { |
| 218 | "/" => Response::from_string(HARNESS).with_header(ctype("text/html; charset=utf-8")), |
| 219 | "/packages.json" => Response::from_string(packages.clone()).with_header(ctype("application/json")), |
| 220 | _ => Response::from_string("not found").with_status_code(404), |
| 221 | }; |
| 222 | let _ = req.respond(resp); |
| 223 | } |
| 224 | }); |
| 225 | Ok(port) |
| 226 | } |
| 227 | |
| 228 | fn ctype(value: &str) -> Header { |
| 229 | Header::from_bytes(&b"Content-Type"[..], value.as_bytes()).expect("static header is valid") |