MCPcopy Create free account
hub / github.com/dylan-sutton-chavez/edge-python / serve

Function serve

cli/src/engine.rs:206–226  ·  view source on GitHub ↗

Serve the harness at `/` and the manifest at `/packages.json` on a free loopback port. The thread is a daemon.

(packages: String)

Source from the content-addressed store, hash-verified

204
205/// Serve the harness at `/` and the manifest at `/packages.json` on a free loopback port. The thread is a daemon.
206fn 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
228fn ctype(value: &str) -> Header {
229 Header::from_bytes(&b"Content-Type"[..], value.as_bytes()).expect("static header is valid")

Callers 1

openMethod · 0.85

Calls 2

ctypeFunction · 0.85
nextMethod · 0.80

Tested by

no test coverage detected