diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-03-08 10:51:41 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-03-08 10:51:41 +0900 |
| commit | a7ce31249948e4f0c1950de93f3c4f7cdda51cf4 (patch) | |
| tree | c4c740f0cccd15f825596f7a115f3b8f8eb8ffa7 /worker/index.mjs | |
| parent | 7f4d16dca85263dcbc7b3bb29f5fc50f4371739d (diff) | |
| parent | c06d46eae30c9468535fb6af5e9b822acadbbdb6 (diff) | |
| download | iosdc-japan-2025-albatross-a7ce31249948e4f0c1950de93f3c4f7cdda51cf4.tar.gz iosdc-japan-2025-albatross-a7ce31249948e4f0c1950de93f3c4f7cdda51cf4.tar.zst iosdc-japan-2025-albatross-a7ce31249948e4f0c1950de93f3c4f7cdda51cf4.zip | |
Merge branch 'phperkaigi-2025'
Diffstat (limited to 'worker/index.mjs')
| -rw-r--r-- | worker/index.mjs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/worker/index.mjs b/worker/index.mjs new file mode 100644 index 0000000..9950ffa --- /dev/null +++ b/worker/index.mjs @@ -0,0 +1,39 @@ +import { fork } from "node:child_process"; +import { serve } from "@hono/node-server"; +import { Hono } from "hono"; + +const execPhp = (code, input, timeoutMsec) => { + return new Promise((resolve, _reject) => { + const proc = fork("./exec.mjs"); + + proc.send({ code, input }); + + proc.on("message", (result) => { + resolve(result); + proc.kill(); + }); + + setTimeout(() => { + resolve({ + status: "timeout", + stdout: "", + stderr: `Time Limit Exceeded: ${timeoutMsec} msec`, + }); + proc.kill(); + }, timeoutMsec); + }); +}; + +const app = new Hono(); + +app.post("/exec", async (c) => { + console.log("worker/exec"); + const { code, stdin, max_duration_ms } = await c.req.json(); + const result = await execPhp(code, stdin, max_duration_ms); + return c.json(result); +}); + +serve({ + fetch: app.fetch, + port: 80, +}); |
