all repos — litestore @ 3afb8b2e6abdca303efe7758e4cdae3ae7bf59ea

A minimalist nosql document store.

Updated middleware execution code.
h3rald h3rald@h3rald.com
Sun, 23 Feb 2020 11:25:20 +0100
commit

3afb8b2e6abdca303efe7758e4cdae3ae7bf59ea

parent

6a0e8ce7fb2fc778b28ba0ac082e5b118587d507

M .vscode/settings.json.vscode/settings.json

@@ -6,5 +6,6 @@ "dialect": "SQLite",

"name": "data" } ], - "sqltools.useNodeRuntime": true + "sqltools.useNodeRuntime": true, + "git.ignoreLimitWarning": true }
M src/litestorepkg/examples/config.jsonsrc/litestorepkg/examples/config.json

@@ -1,6 +1,7 @@

{ "settings": { - "log": "debug" + "log": "debug", + "middleware": "litestorepkg/examples/middleware" }, "resources": { "/info": {
A src/litestorepkg/examples/middleware/test.js

@@ -0,0 +1,4 @@

+(function() { + $res.content = $req; + return; +}())
D src/litestorepkg/examples/test.js

@@ -1,4 +0,0 @@

-(function() { - $res.content = $req; - return; -}())
M src/litestorepkg/lib/api_v6.nimsrc/litestorepkg/lib/api_v6.nim

@@ -1044,6 +1044,8 @@ # Attempt to retrieve resource from system documents

let options = newQueryOptions(true) let doc = LS.store.retrieveDocument("custom/" & id, options) result = doc.data + if result == "": + LOG.warn("Middleware '$1' not found" % id) else: result = LS.middleware[id]

@@ -1077,10 +1079,10 @@ let middleware = getMiddlewareSeq(resource, id, $req.reqMethod)

if middleware.len == 0: return route(req, LS, resource, id) var jReq = $(%* req) - echo jReq + LOG.debug("Request: ", jReq) var jRes = """{ "code": 200, - "content": "", + "content": {}, "final": false, "headers": { "Access-Control-Allow-Origin": "*",

@@ -1111,7 +1113,7 @@ )

discard duk_push_c_function(ctx, fNext, 0) discard ctx.duk_put_global_string("$next") var i = 0 - ctx.duk_push_boolean(1) + ctx.duk_push_boolean(0) discard ctx.duk_put_global_string("__next__") var abort = 0 var next = 0

@@ -1119,13 +1121,11 @@ while abort != 1 and i < middleware.len:

if ctx.duk_peval_string("__next__") != 0: return jError(ctx) next = ctx.duk_get_boolean(-1) - if next != 1: + if next == 0: abort = 1 - LOG.debug("next: $1 | abort: $2", [next, abort]) let code = LS.getMiddleware(middleware[i]) LOG.debug("Evaluating middleware '$1'" % middleware[i]) if ctx.duk_peval_string(code) != 0: - echo "error!" return jError(ctx) i.inc # Retrieve response, and request

@@ -1136,6 +1136,7 @@ if ctx.duk_peval_string("JSON.stringify($req);") != 0:

return jError(ctx) let fReq = parseJson($(ctx.duk_get_string(-1))).newLSRequest() ctx.duk_destroy_heap(); + LOG.debug("next: $1 | abort: $2", [$next, $abort]) if abort == 1: return fRes return route(fReq, LS, resource, id)
M src/litestorepkg/lib/cli.nimsrc/litestorepkg/lib/cli.nim

@@ -138,6 +138,7 @@ fail(116, "Middleware directory does not exist.")

for file in val.walkDir(): if file.kind == pcFile or file.kind == pcLinkToFile: middleware[file.path.splitFile[1]] = file.path.readFile() + cliSettings["middleware"] = %val of "operation", "o": if val == "": fail(106, "Operation not specified.")

@@ -208,6 +209,11 @@ if not cliSettings.hasKey("store") and settings.hasKey("store"):

file = settings["store"].getStr if not cliSettings.hasKey("directory") and settings.hasKey("directory"): directory = settings["directory"].getStr + if not cliSettings.hasKey("middleware") and settings.hasKey("middleware"): + let val = settings["middleware"].getStr + for file in val.walkDir(): + if file.kind == pcFile or file.kind == pcLinkToFile: + middleware[file.path.splitFile[1]] = file.path.readFile() if not cliSettings.hasKey("log") and settings.hasKey("log"): logLevel = settings["log"].getStr setLogLevel(logLevel)
M src/litestorepkg/lib/server.nimsrc/litestorepkg/lib/server.nim

@@ -11,6 +11,7 @@ json,

tables, strtabs, base64, + asyncnet, jwt import types,

@@ -230,6 +231,9 @@ var server = newAsyncHttpServer()

proc handleHttpRequest(origReq: Request): Future[void] {.async, gcsafe, closure.} = var client = origReq.client var req = newLSRequest(origReq) + let address = client.getLocalAddr() + req.url.hostname = address[0] + req.url.port = $int(address[1]) LOG.info(getReqInfo(req).replace("$", "$$")) let res = req.process(LS) var newReq = newRequest(req, client)
M src/litestorepkg/lib/types.nimsrc/litestorepkg/lib/types.nim

@@ -8,7 +8,9 @@ json,

strtabs, strutils, sequtils, + nativesockets, jwt, + uri, tables import config

@@ -158,9 +160,11 @@ let headers = %req.headers

result["headers"] = newJObject() for k, v in headers["table"].pairs: result["headers"][k] = %join(v.mapIt(it.getStr), ", ") - result["protocol"] = %req.protocol - result["url"] = %req.url - result["hostname"] = %req.hostname + result["protocol"] = %req.protocol.orig + result["hostname"] = %req.url.hostname + result["port"] = %req.url.port.parseInt + result["path"] = %req.url.path + result["query"] = %req.url.query result["body"] = %req.body proc `%`*(res: LSResponse): JsonNode =

@@ -171,20 +175,27 @@ result["content"] = %res.content

proc newLSResponse*(res: JsonNode): LSResponse = result.code = HttpCode(res["code"].getInt) - result.content = res["content"].getStr + result.content = $res["content"] result.headers = newHttpHeaders() for k, v in res["headers"].pairs: - result.headers[k] = v.getStr + result.headers[k] = $v proc newLSRequest*(req: JsonNode): LSRequest = result.reqMethod = httpMethod(req["method"].getStr) result.headers = newHttpHeaders() for k, v in req["headers"].pairs: - result.headers[k] = v.getStr - result.protocol = to(req["protocol"], tuple[orig: string, major, minor: int]) - result.url = to(req["url"], Uri) - result.hostname = req["hostname"].getStr - result.body = req["body"].getStr + result.headers[k] = $v + let protocol = req["protocol"].getStr + let parts = protocol.split("/") + let version = parts[1].split(".") + result.protocol = (orig: parts[0], major: version[0].parseInt, minor: version[1].parseInt) + result.url = initUri() + result.url.hostname = $req["hostname"] + result.url.port = $req["port"] + result.url.path = $req["path"] + result.url.query = $req["query"] + result.hostname = $req["hostname"] + result.body = $req["body"] proc newLSRequest*(req: Request): LSRequest = result.reqMethod = req.reqMethod