Skip to content

Instantly share code, notes, and snippets.

@zacharycarter
Created April 23, 2017 15:49
Show Gist options
  • Save zacharycarter/9b93705067c0647f5c910db6206e18eb to your computer and use it in GitHub Desktop.
Save zacharycarter/9b93705067c0647f5c910db6206e18eb to your computer and use it in GitHub Desktop.
import jester, asyncdispatch, os, osproc, strutils, json, threadpool, asyncfile
proc respondOnReady(fv: FlowVar[TaintedString], resp: Response) {.async.} =
while true:
if fv.isReady:
echo ^fv
var errorsFile = openAsync("tmp/errors.txt", fmReadWrite)
var logFile = openAsync("tmp/logfile.txt", fmReadWrite)
var errors = await errorsFile.readAll()
var log = await logFile.readAll()
var ret = %* {"compileLog": errors, "log": log}
errorsFile.close()
logFile.close()
result = resp.send(Http200, newStringTable(), $ret)
await sleepAsync(500)
proc prepareAndCompile(code: string): TaintedString =
let currentDir = getCurrentDir()
discard existsOrCreateDir("./tmp")
copyFileWithPermissions("./test/script.sh", "./tmp/script.sh")
writeFile("./tmp/in.nim", code)
execProcess("""
./docker_timeout.sh 20s -i -t --net=none -v "$1/tmp":/usercode virtual_machine /usercode/script.sh in.nim
""" % currentDir)
proc compile(resp: Response, code: string) =
let fv = spawn prepareAndCompile(code)
asyncCheck respondOnReady(fv, resp)
routes:
post "/compile":
if not request.formData.hasKey("code"):
resp Http400, "code missing from requests' form data."
response.data.action = TCActionRaw
response.compile(request.formData["code"].body)
runForever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment