Added jester-based YAML parsing webservice

* Added server/server.nim
 * Fixed a problem in presenter that made it crash when
   outputting a root map to JSON
This commit is contained in:
Felix Krause 2016-01-05 21:34:07 +01:00
parent f3f6f5de9e
commit 68f0ba0cc7
3 changed files with 54 additions and 2 deletions

View File

@ -33,4 +33,8 @@ task bench, "Benchmarking":
task clean, "Remove all generated files":
exec "rm -f yaml.html libyaml.* test/tests test/parsing test/lexing"
setCommand "nop"
setCommand "nop"
task server, "Compile server daemon":
--d:release
setCommand "c", "server/server"

View File

@ -323,7 +323,7 @@ proc present*(s: YamlStream, target: Stream, tagLib: YamlTagLibrary,
of ypsCanonical:
nextState = dFlowExplicitMapStart
of ypsJson:
if levels[levels.high] in
if levels.len > 0 and levels[levels.high] in
[dFlowImplicitMapStart, dFlowImplicitMapValue]:
raise newException(YamlPresenterJsonError,
"Cannot have map as map key in JSON output!")

48
server/server.nim Normal file
View File

@ -0,0 +1,48 @@
# 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 "/":
var style: YamlPresentationStyle
case @"style"
of "minimal": style = ypsMinimal
of "canonical": style = ypsCanonical
of "default": style = ypsDefault
of "json": style = ypsJson
of "blocks": style = ypsBlockOnly
var
output = newStringStream()
resultNode = newJObject()
headers["Access-Control-Allow-Origin"] = "flyx.github.io"
try:
try:
transform(newStringStream(@"input"), output, style)
resultNode["code"] = %0
resultNode["output"] = %output.data
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
except YamlPresenterJsonError:
let e = (ref YamlPresenterJsonError)(getCurrentException())
resultNode["code"] = %2
resultNode["message"] = %e.msg
headers["Content-Type"] = "application/json"
resp resultNode.pretty
except:
status = Http500
resp getCurrentException().repr
runForever()