-
-
Save patrickdevivo/b3d2a430ec9dec8020c46cf3eb0f11c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { listenAndServe } from "https://deno.land/std@0.91.0/http/server.ts" | |
import { Status } from "https://deno.land/std@0.91.0/http/http_status.ts" | |
const HTTP_PORT = 8080; | |
const options = { hostname: "0.0.0.0", port: HTTP_PORT } | |
console.log(`HTTP server running on localhost:${HTTP_PORT}`) | |
listenAndServe(options, (request) => { | |
if (request.method !== "GET") { | |
request.respond({ status: Status.MethodNotAllowed, body: "must GET" }) | |
return | |
} | |
const start = new Date() | |
const url = `https://${request.url.substring(1)}` | |
let completed = false | |
const p = Deno.run({ | |
cmd: [ "deno", "run", url ], | |
stdout: "piped", | |
stderr: "null" | |
}); | |
// pipe the stdout of the process to the http response | |
request.respond({ body: p.stdout }) | |
// whenever the process exits, mark it as done | |
p.status().finally(() => { | |
completed = true | |
let elapsed = (new Date()).getTime() - start.getTime() | |
console.log({ url, time: `${elapsed}ms` }) | |
}) | |
// once the request is done (or canceled) | |
Deno.readAll(request.r).then(async () => { | |
// if the process hasn't completed, end it | |
if (!completed) p.kill(2) | |
}) | |
// don't let requests run indefinitely | |
setTimeout(() => { | |
if (!completed) p.kill(2) | |
}, 10*1000) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment