diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2022-02-06 20:10:31 +0200 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2022-02-06 20:10:31 +0200 |
| commit | 37d5f09c2a7a335f5111663edea95f053eaa08b0 (patch) | |
| tree | 01561b3ffaad197d99e1f770fb88b45c2e0e4093 /src/index.ts | |
Initial commit
Diffstat (limited to 'src/index.ts')
| -rw-r--r-- | src/index.ts | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..68cdadf --- /dev/null +++ b/src/index.ts @@ -0,0 +1,34 @@ +import { App } from "@tinyhttp/app" +import { logger } from "@tinyhttp/logger" +import { json } from "milliparsec" +import { promisify } from "util" +import { exec as cb_exec } from "child_process" +import tmp from "tmp" +import fs from "fs" + +const exec = promisify(cb_exec) +const app = new App() + +const PORT = Number(process.env.PORT) || 3000 +console.log(`Serving API on port ${PORT}`) + +app + .use(logger({ + timestamp: { format: "YYYY-MM-DDTHH:mm:ssZ" }, + })) + .use(json()) + .post("/submit", async (req, res) => { + if (!req.body || !req.body.source) { + return res.sendStatus(400) + } + + const { name: sourceFile } = tmp.fileSync() + fs.writeFileSync(sourceFile, req.body.source) + + const cmd = `./interpreter ${sourceFile}` + const result = await exec(cmd) + fs.rmSync(sourceFile) + + res.send({ result }) + }) + .listen(PORT) |
