all repos — litestore @ 5af10df4c63b1ce884c7bbcded6dfb603e4ed633

A minimalist nosql document store.

Added example middleware, tests
h3rald h3rald@h3rald.com
Wed, 04 Mar 2020 18:16:34 +0100
commit

5af10df4c63b1ce884c7bbcded6dfb603e4ed633

parent

b90a02382daa38c897a345b6be10dadf3110b693

M .gitignore.gitignore

@@ -16,3 +16,4 @@ LiteStore_UserGuide.htm

jester_integration js *_backup +config.json
M src/litestorepkg/examples/config.jsonsrc/litestorepkg/examples/config.json

@@ -1,7 +1,8 @@

{ "settings": { "log": "debug", - "middleware": "litestorepkg/examples/middleware" + "middleware": "litestorepkg/examples/middleware", + "port": 9100 }, "resources": { "/info": {

@@ -9,7 +10,7 @@ "GET": { "auth": ["admin:server"] }

}, "/docs/*": { "GET": { - "middleware": ["test"] + "middleware": ["validate", "log"] }, "POST": { "auth": ["admin:server"] }, "PATCH": { "auth": ["admin:server"] },
A src/litestorepkg/examples/middleware/log.js

@@ -0,0 +1,10 @@

+(function(){ + var doc = { + sub: $req.jwt.claims && $req.jwt.claims.sub || null, + agent: $req.headers['user-agent'], + language: $req.headers['accept-language'] && $req.headers['accept-language'].replace(/,.+$/, ''), + path: $req.path, + timestamp: Date.now() + } + $store.post('docs', 'logs', JSON.stringify(doc), 'application/json'); +}())
A src/litestorepkg/examples/middleware/req.js

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

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

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

-(function() { - $res.content = $req; - return false; -}())
A src/litestorepkg/examples/middleware/validate.js

@@ -0,0 +1,11 @@

+(function () { + var id = $req.path.replace(/^\/docs\//, ''); + var valid = /[A-Z]{2}[0-9]{3}[A-Z]{2}/; + if (!id.match(valid)) { + $res.content = { + error: 'Invalid number plate' + }; + $res.code = 400; + return true; + } +}())
M src/litestorepkg/lib/api_v6.nimsrc/litestorepkg/lib/api_v6.nim

@@ -1058,11 +1058,12 @@ if reqUri[^1] == '/':

reqUri.removeSuffix({'/'}) let parts = reqUri.split("/") let ancestors = parts[1..parts.len-2] - var currentPath = "/" + var currentPath = "" var currentPaths = "" for p in ancestors: - currentPath &= p + currentPath &= "/" & p currentPaths = currentPath & "/*" + echo currentPaths if LS.config["resources"].hasKey(currentPaths) and LS.config["resources"][currentPaths].hasKey(meth) and LS.config["resources"][currentPaths][meth].hasKey("middleware"): let mw = LS.config["resources"][currentPaths][meth]["middleware"] if (mw.kind == JArray):

@@ -1076,6 +1077,7 @@ result.add m.getStr

proc execute*(req: var LSRequest, LS: LiteStore, resource, id: string): LSResponse = let middleware = getMiddlewareSeq(resource, id, $req.reqMethod) + LOG.debug("Middleware: ", middleware); if middleware.len == 0: return route(req, LS, resource, id) var jReq = $(%* req)
M src/litestorepkg/lib/types.nimsrc/litestorepkg/lib/types.nim

@@ -165,7 +165,7 @@ 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 + result["content"] = %req.body proc `%`*(res: LSResponse): JsonNode = result = newJObject()

@@ -178,7 +178,7 @@ result.code = HttpCode(res["code"].getInt)

result.content = $res["content"] result.headers = newHttpHeaders() for k, v in res["headers"].pairs: - result.headers[k] = $v + result.headers[k] = v.getStr proc newLSRequest*(req: JsonNode): LSRequest = result.reqMethod = httpMethod(req["method"].getStr)

@@ -190,12 +190,12 @@ 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"] + result.url.hostname = req["hostname"].getStr + result.url.port = req["port"].getStr + result.url.path = req["path"].getStr + result.url.query = req["query"].getStr + result.hostname = req["hostname"].getStr + result.body = $req["content"] proc newLSRequest*(req: Request): LSRequest = result.reqMethod = req.reqMethod