2016-01-05 20:34:07 +00:00
|
|
|
# NimYAML - YAML implementation in Nim
|
|
|
|
# (c) Copyright 2015 Felix Krause
|
|
|
|
#
|
|
|
|
# See the file "copying.txt", included in this
|
|
|
|
# distribution, for details about the copyright.
|
|
|
|
|
|
|
|
import jester, asyncdispatch, json, streams
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
routes:
|
|
|
|
get "/":
|
|
|
|
headers["Content-Type"] = "text/plain"
|
|
|
|
resp "I am a friendly NimYAML parser webservice."
|
|
|
|
post "/":
|
2016-02-18 14:52:19 +00:00
|
|
|
var
|
|
|
|
style: PresentationStyle
|
|
|
|
resultNode = newJObject()
|
|
|
|
tokens = false
|
|
|
|
headers["Access-Control-Allow-Origin"] = "*"
|
|
|
|
headers["Pragma"] = "no-cache"
|
|
|
|
headers["Cache-Control"] = "no-cache"
|
|
|
|
headers["Expires"] = "0"
|
2016-01-05 20:34:07 +00:00
|
|
|
case @"style"
|
2016-01-24 20:41:06 +00:00
|
|
|
of "minimal": style = psMinimal
|
|
|
|
of "canonical": style = psCanonical
|
|
|
|
of "default": style = psDefault
|
|
|
|
of "json": style = psJson
|
|
|
|
of "block": style = psBlockOnly
|
2016-02-18 14:52:19 +00:00
|
|
|
of "tokens":
|
|
|
|
var
|
|
|
|
output = ""
|
|
|
|
parser = newYamlParser()
|
|
|
|
events = parser.parse(newStringStream(@"input"))
|
|
|
|
try:
|
|
|
|
for event in events:
|
|
|
|
output.add($event & "\n")
|
|
|
|
resultNode["code"] = %0
|
|
|
|
resultNode["output"] = %output
|
|
|
|
resp resultNode.pretty, "application/json"
|
|
|
|
tokens = true
|
|
|
|
except YamlParserError:
|
|
|
|
let e = (ref YamlParserError)(getCurrentException())
|
|
|
|
resultNode["code"] = %1
|
|
|
|
resultNode["line"] = %e.line
|
|
|
|
resultNode["column"] = %e.column
|
|
|
|
resultNode["message"] = %e.msg
|
|
|
|
resultNode["detail"] = %e.lineContent
|
|
|
|
resp resultNode.pretty, "application/json"
|
|
|
|
if not tokens:
|
|
|
|
echo "outputting YAML"
|
|
|
|
var
|
2016-01-05 20:34:07 +00:00
|
|
|
output = newStringStream()
|
2016-02-18 14:52:19 +00:00
|
|
|
try:
|
2016-01-07 10:31:45 +00:00
|
|
|
transform(newStringStream(@"input"), output, style)
|
|
|
|
resultNode["code"] = %0
|
|
|
|
resultNode["output"] = %output.data
|
|
|
|
resp resultNode.pretty, "application/json"
|
2016-02-18 14:52:19 +00:00
|
|
|
except YamlParserError:
|
2016-01-24 20:43:33 +00:00
|
|
|
let e = (ref YamlParserError)(getCurrentException())
|
2016-01-07 10:31:45 +00:00
|
|
|
resultNode["code"] = %1
|
|
|
|
resultNode["line"] = %e.line
|
|
|
|
resultNode["column"] = %e.column
|
|
|
|
resultNode["message"] = %e.msg
|
|
|
|
resultNode["detail"] = %e.lineContent
|
|
|
|
resp resultNode.pretty, "application/json"
|
2016-02-18 14:52:19 +00:00
|
|
|
except YamlPresenterJsonError:
|
2016-01-07 10:31:45 +00:00
|
|
|
let e = getCurrentException()
|
|
|
|
resultNode["code"] = %2
|
|
|
|
resultNode["message"] = %e.msg
|
2016-01-05 20:34:07 +00:00
|
|
|
headers["Content-Type"] = "application/json"
|
2016-01-07 10:31:45 +00:00
|
|
|
resp resultNode.pretty, "application/json"
|
2016-02-18 14:52:19 +00:00
|
|
|
except:
|
2016-01-07 10:31:45 +00:00
|
|
|
let e = getCurrentException()
|
2016-01-24 20:43:33 +00:00
|
|
|
let msg = "Name: " & $e.name & "\nMessage: " & e.msg &
|
|
|
|
"\nTrace:\n" & e.getStackTrace
|
2016-01-07 10:31:45 +00:00
|
|
|
resp Http500, msg, "text/plain;charset=utf-8"
|
2016-01-05 20:34:07 +00:00
|
|
|
|
2016-01-05 22:19:00 +00:00
|
|
|
runForever()
|