all repos — litestore @ fd65a969088717d1af9011d573fdd6fadea4aa4e

A minimalist nosql document store.

Added passthrough function (not used for now)
h3rald h3rald@h3rald.com
Sun, 16 Feb 2020 09:58:53 +0100
commit

fd65a969088717d1af9011d573fdd6fadea4aa4e

parent

a68a5db2149e360351cc45062404424dbb130b1f

M src/litestorepkg/examples/test.jssrc/litestorepkg/examples/test.js

@@ -1,1 +1,3 @@

LiteStore.response.content = LiteStore.api.get('docs').content; + +console.log(LiteStore.api.passthrough());
M src/litestorepkg/lib/api_v6.nimsrc/litestorepkg/lib/api_v6.nim

@@ -986,7 +986,7 @@

proc head(resource, id: string): LSResponse = return newSimpleLSRequest(HttpHead, resource, id).head(LS, resource, id) -proc registerApi(LS: LiteStore, ctx: DTContext, obj: duk_idx_t) = +proc registerApi(LS: LiteStore, ctx: DTContext, obj: duk_idx_t, req: LSRequest, origResource, origId: string) = var api_idx = ctx.duk_push_object() # GET var get: DTCFunction = (proc (ctx: DTContext): cint{.stdcall.} =

@@ -1078,9 +1078,21 @@ return 1

) discard duk_push_c_function(ctx, head, 2) discard ctx.duk_put_prop_string(api_idx, "head") + # PASSTHROUGH + #var passthrough: DTCFunction = (proc (ctx: DTContext): cint{.stdcall.} = + # let resp = route(req, LS, origResource, origId) + # var res_idx = ctx.duk_push_object() + # ctx.duk_push_int(cast[cint](resp.code)) + # discard ctx.duk_put_prop_string(res_idx, "code") + # discard ctx.duk_push_string(resp.content) + # discard ctx.duk_put_prop_string(res_idx, "content") + # return 1 + #) + #discard duk_push_c_function(ctx, passthrough, 0) + #discard ctx.duk_put_prop_string(api_idx, "passthrough") discard ctx.duk_put_prop_string(obj, "api") - -proc execute*(req: LSRequest, LS:LiteStore, id: string): LSResponse = + +proc execute*(req: LSRequest, LS: LiteStore, resource, id: string): LSResponse = var resource: string if not LS.customResources.hasKey(id): # Attempt to retrieve resource from system documents

@@ -1096,7 +1108,7 @@ var ctx = duk_create_heap_default()

duk_console_init(ctx) duk_print_alert_init(ctx) var ctx_idx = ctx.duk_push_object() - LS.registerApi(ctx, ctx_idx) + LS.registerApi(ctx, ctx_idx, req, resource, id) LS.createRequest(ctx, ctx_idx, req) LS.createResponse(ctx, ctx_idx) discard ctx.duk_put_global_string("LiteStore")
M src/litestorepkg/lib/server.nimsrc/litestorepkg/lib/server.nim

@@ -15,6 +15,7 @@ jwt

import types, utils, + core, api_v1, api_v2, api_v3,

@@ -34,6 +35,15 @@ base64.decode(s)

proc decodeUrlSafe*(s: string): seq[byte] = cast[seq[byte]](decodeUrlSafeAsString(s)) + +proc getCustomResource*(LS: LiteStore, id: string): string = + if not LS.customResources.hasKey(id): + # Attempt to retrieve resource from system documents + let options = newQueryOptions(true) + let doc = LS.store.retrieveDocument("custom/" & id, options) + result = doc.data + else: + result = LS.customResources[id] proc getReqInfo(req: LSRequest): string = var url = req.url.path

@@ -114,13 +124,14 @@ if access.hasKey(uri):

auth(uri) break if info.version == "v6": - if info.resource.match(peg"^docs / info / tags / indexes$"): + if info.resource.match(peg"^docs$"): + let parts = info.id.split("/") + let custom = LS.getCustomResource(parts[0]) + if (custom != ""): + return api_v6.execute(req, LS, info.resource, info.id) + return api_v6.route(req, LS, info.resource, info.id) + elif info.resource.match(peg"^info / tags / indexes$"): return api_v6.route(req, LS, info.resource, info.id) - elif info.resource.match(peg"^custom$"): - if LS.customResources.len > 0: - return api_v6.execute(req, LS, info.id) - else: - return resError(Http400, "Bad Request - Not custom resources available." % info.version) elif info.resource.match(peg"^dir$"): if LS.directory.len > 0: return api_v6.serveFile(req, LS, info.id)