-
Notifications
You must be signed in to change notification settings - Fork 3
/
playground.nim
63 lines (52 loc) · 2.03 KB
/
playground.nim
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import jester, asyncdispatch, htmlgen, strutils, strtabs, osproc, os, oids, times, marshal, json
type Result = object of RootObj
status: string
result: string
compileTime: float
executionTime: float
proc execute(body: string, appendCompilerOutput = true): string =
var status = "success"
var output = ""
var compileTime, executionTime: float = 0
let start = times.epochTime()
let dir = os.joinPath(os.getTempDir(), "nim_playground")
if not existsDir(dir):
createDir(dir)
let filePath = os.joinPath(dir, "nim_" & $genOid())
echo("Writing file $1" % [filePath])
system.writeFile(filePath & ".nim", body)
echo("Compiling file $1" % [filePath])
var (rawOutput, errCode) = osproc.execCmdEx("nim c --threads:on --opt:none " & filePath & ".nim")
compileTime = times.epochTime() - start
output = $rawOutput
if errCode > 0:
status = "compileError"
echo("Compilation error for $1: $2" % [filePath, output])
else:
(rawOutput, errCode) = osproc.execCmdEx(filePath)
if appendCompilerOutput:
output = $rawOutput & "\n\n" & "#".repeat(60) & "\n###" & " Compiler output " & "#".repeat(40) & "\n" & "#".repeat(60) & "\n\n" & output
else:
output = $rawOutput
executionTime = times.epochTime() - start - compileTime
if errCode > 0:
status = "executionError"
echo("Execution error for $1: $2" % [filePath, output])
else:
echo("Execution of $1 succeded." % [filePath])
var response = Result(status: status, result: output, compileTime: compileTime, executionTime: executionTime)
return $$response
settings:
staticDir = joinPath(getAppDir(), "public")
port = 5000.Port
bindAddr = "127.0.0.1"
routes:
post "/api/execute":
let headers = newStringTable(modeCaseSensitive)
headers["Content-Type"] = "application/json"
let body = try: request.body.parseJson
except: newJNull()
let code = body["code"].getStr()
let compilerOutput = body["compilerOutput"].getBool()
resp(execute(code, appendCompilerOutput = compilerOutput))
runForever()