diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-03-08 00:59:36 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-03-08 00:59:36 +0900 |
| commit | a10e8e64305e5002c682b45471b417ca4e33773d (patch) | |
| tree | 23bf4a707c1f168c252cc4d24d06c2d2ffb8c7a8 /worker/index.mjs | |
| parent | efd34b970d0d1f88fa7d7e9d69e569f039867ca2 (diff) | |
| download | phperkaigi-2025-albatross-a10e8e64305e5002c682b45471b417ca4e33773d.tar.gz phperkaigi-2025-albatross-a10e8e64305e5002c682b45471b417ca4e33773d.tar.zst phperkaigi-2025-albatross-a10e8e64305e5002c682b45471b417ca4e33773d.zip | |
worker: swift to php
Diffstat (limited to 'worker/index.mjs')
| -rw-r--r-- | worker/index.mjs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/worker/index.mjs b/worker/index.mjs new file mode 100644 index 0000000..525fe63 --- /dev/null +++ b/worker/index.mjs @@ -0,0 +1,38 @@ +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) => { + 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, +}); |
