aboutsummaryrefslogtreecommitdiffstats
path: root/src/index.ts
blob: 68cdadf115bfcc1f8b946695ae5ac98e72a907f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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)