From 68f0ba0cc7c066939b0d7e8b69eb441e8d3d03cf Mon Sep 17 00:00:00 2001 From: Felix Krause Date: Tue, 5 Jan 2016 21:34:07 +0100 Subject: [PATCH] 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 --- config.nims | 6 +++++- private/presenter.nim | 2 +- server/server.nim | 48 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 server/server.nim diff --git a/config.nims b/config.nims index ff7c85a..e705b68 100644 --- a/config.nims +++ b/config.nims @@ -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" \ No newline at end of file + setCommand "nop" + +task server, "Compile server daemon": + --d:release + setCommand "c", "server/server" \ No newline at end of file diff --git a/private/presenter.nim b/private/presenter.nim index 2d6bafc..1a709df 100644 --- a/private/presenter.nim +++ b/private/presenter.nim @@ -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!") diff --git a/server/server.nim b/server/server.nim new file mode 100644 index 0000000..2e79b26 --- /dev/null +++ b/server/server.nim @@ -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() \ No newline at end of file