aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/index.ts27
1 files changed, 20 insertions, 7 deletions
diff --git a/src/index.ts b/src/index.ts
index 68cdadf..8e90fbe 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -5,6 +5,7 @@ import { promisify } from "util"
import { exec as cb_exec } from "child_process"
import tmp from "tmp"
import fs from "fs"
+import { RunnerApiSubmitRequest, RunnerApiSubmitResponse } from "slam-types"
const exec = promisify(cb_exec)
const app = new App()
@@ -12,23 +13,35 @@ const app = new App()
const PORT = Number(process.env.PORT) || 3000
console.log(`Serving API on port ${PORT}`)
+const interpretFile = async (sourceFile: string) => {
+ const cmd = `./interpreter ${sourceFile}`
+ try {
+ const result = await exec(cmd)
+ return result.stdout
+ } catch (err) {
+ console.error(err)
+ return err.stdout // the interpreter only outputs to stdout
+ }
+}
+
app
.use(logger({
timestamp: { format: "YYYY-MM-DDTHH:mm:ssZ" },
}))
.use(json())
- .post("/submit", async (req, res) => {
- if (!req.body || !req.body.source) {
+ .post(RunnerApiSubmitRequest.path, async (req, res) => {
+ const body: RunnerApiSubmitRequest.Body | undefined = req.body
+ if (!body || !body.source) {
return res.sendStatus(400)
}
- const { name: sourceFile } = tmp.fileSync()
- fs.writeFileSync(sourceFile, req.body.source)
+ const { name: sourceFile } = tmp.fileSync() as { name: string }
+ fs.writeFileSync(sourceFile, body.source)
- const cmd = `./interpreter ${sourceFile}`
- const result = await exec(cmd)
+ const result = await interpretFile(sourceFile)
fs.rmSync(sourceFile)
- res.send({ result })
+ const response: RunnerApiSubmitResponse.Body = { result }
+ res.send(response)
})
.listen(PORT)