Skip to content

Instantly share code, notes, and snippets.

@zacharycarter
Last active April 21, 2018 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zacharycarter/ad871978fc697d6d5af28411f1458b50 to your computer and use it in GitHub Desktop.
Save zacharycarter/ad871978fc697d6d5af28411f1458b50 to your computer and use it in GitHub Desktop.
import jester, json, asyncdispatch, parsecfg, strutils, uuids, os, osproc, threadpool, asyncfile
type
PlaygroundConfig = object
tmpDir: string
logFileName: string
CompilationTarget {.pure.} = enum
C, CPP
PlaygroundRequest = object
code: string
compilationTarget: CompilationTarget
const invalidRequestError = "Invalid request format: $1"
var playgroundConfig = createShared(PlaygroundConfig)
let loadedConfig = loadConfig("playground.cfg")
playgroundConfig.tmpDir = loadedConfig.getSectionValue("","tmpDir")
playgroundConfig.logFileName = loadedConfig.getSectionValue("","logFileName")
proc respondOnReady(flowVar: FlowVar[TaintedString], tmpDir: string): Future[string] {.async.} =
while true:
if flowVar.isReady:
# var errorsFile = openAsync("$1/errors.txt" % tmpDir, fmReadWrite)
# var logFile = openAsync("$1/logfile.txt" % tmpDir, fmReadWrite)
# var errors = await errorsFile.readAll()
# var log = await logFile.readAll()
# var ret = %* {"compileLog": errors, "log": log}
# errorsFile.close()
# logFile.close()
# return $ret
return ""
await sleepAsync(500)
proc compilationTask(code: string, compilationTarget: CompilationTarget, tmpDir: string): TaintedString =
discard existsOrCreateDir(tmpDir)
copyFileWithPermissions("./scripts/compile.sh", "$1/compile.sh" % tmpDir)
writeFile("$1/main.nim" % tmpDir, code)
# echo execProcess("""
# ./scripts/docker-timeout.sh 20s -i -t --net=none -v "$1":/usercode playground /usercode/compile.sh main.nim $2
# """ % [tmpDir, $compilationTarget])
return "foo"
proc fillRequestFromQueryString(playgroundRequest: var PlaygroundRequest, params: StringTableRef) =
if params.hasKey("code"):
playgroundRequest.code = params["code"]
if params.hasKey("compilationTarget"):
if params["compilationTarget"] == "cpp":
playgroundRequest.compilationTarget = CompilationTarget.CPP
proc compile(code: string, compilationTarget: CompilationTarget, tmpDir: string): Future[string] =
let flowVar = spawn compilationTask(code, compilationTarget, tmpDir)
return respondOnReady(flowVar, tmpDir)
routes:
echo playgroundConfig.tmpDir
post "/compile":
var playgroundRequest = PlaygroundRequest(compilationTarget: CompilationTarget.C, code: "")
if request.params.len > 0:
playgroundRequest.fillRequestFromQueryString(request.params)
else:
if request.body != nil:
try:
let parsed = parseJson(request.body)
playgroundRequest.code = parsed{"code"}.getStr()
if parsed{"compilationTarget"}.getStr() == "cpp":
playgroundRequest.compilationTarget = CompilationTarget.CPP
except KeyError:
resp(Http500, invalidRequestError % request.body)
if playgroundRequest.code == "":
resp(Http500, invalidRequestError % request.body)
let result = await compile(playgroundRequest.code, playgroundRequest.compilationTarget, "$1/$2" % [playgroundConfig.tmpDir, $genUUID()])
resp(Http200, result)
resp(Http200, "")
defer: freeShared(playgroundConfig)
runForever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment