all repos — litestore @ 3fd542f5f56662801de79b1b7262ee7f333a5977

A minimalist nosql document store.

Implemented HTTP forwarding for stores.
h3rald h3rald@h3rald.com
Sun, 15 Mar 2020 15:23:11 +0100
commit

3fd542f5f56662801de79b1b7262ee7f333a5977

parent

7d6d4798459e6cc27e4763c10eba034dbe78c4bc

3 files changed, 22 insertions(+), 3 deletions(-)

jump to
M src/litestore.nimsrc/litestore.nim

@@ -172,7 +172,6 @@ # }

# } # } proc initStores() = - LSDICT["main"] = LS if LS.config.kind == JObject and LS.config.hasKey("stores"): for k, v in LS.config["stores"].pairs: # TODO error handling

@@ -182,8 +181,12 @@ if v.hasKey("config"):

LSDICT[k].config = v["config"] for k in LSDICT.keys: LOG.info("Initializing store '$1'" % k) - LSDICT[k].setup() + LSDICT[k].setup(true) LSDICT[k].initStore() + LOG.info("Initializing main store") + LS.setup(true) + LS.initStore() + LSDICT["main"] = LS when isMainModule:
M src/litestorepkg/lib/server.nimsrc/litestorepkg/lib/server.nim

@@ -248,6 +248,20 @@ let e = getCurrentException()

let trace = e.getStackTrace() return resError(Http500, "Internal Server Error: $1" % getCurrentExceptionMsg(), trace) + +proc process*(req: LSRequest, LSDICT: Table[string, LiteStore]): LSResponse {.gcsafe.}= + var matches = @["", ""] + if req.url.path.find(PEG_STORE_URL, matches) != -1: + let id = matches[0] + let path = matches[1] + var newReq = req + newReq.url.path = "/$1" % path + if not LSDICT.hasKey(id): + return resError(Http400, "Unknown store '$1'" % id) + return process(newReq, LSDICT[id]) + else: + return process(req, LS) + setControlCHook(handleCtrlC) proc serve*(LS: LiteStore) =

@@ -259,7 +273,7 @@ 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) + let res = req.process(LSDICT) var newReq = newRequest(req, client) await newReq.respond(res.code, res.content, res.headers) echo(LS.appname & " v" & LS.appversion & " started on " & LS.address & ":" & $LS.port & ".")
M src/litestorepkg/lib/types.nimsrc/litestorepkg/lib/types.nim

@@ -234,6 +234,7 @@ PEG_USER_TAG* {.threadvar.}: Peg

PEG_INDEX* {.threadvar}: Peg PEG_JSON_FIELD* {.threadvar.}: Peg PEG_DEFAULT_URL* {.threadvar.}: Peg + PEG_STORE_URL* {.threadvar.}: Peg PEG_URL* {.threadvar.}: Peg PEG_TAG = peg"""^\$? [a-zA-Z0-9_\-?~:.@#^!+]+$"""

@@ -241,6 +242,7 @@ PEG_USER_TAG = peg"""^[a-zA-Z0-9_\-?~:.@#^!+]+$"""

PEG_INDEX = peg"""^[a-zA-Z0-9_]+$""" PEG_JSON_FIELD = peg"""'$' ('.' [a-z-A-Z0-9_]+)+""" PEG_DEFAULT_URL = peg"""^\/{(docs / info / dir / tags / indexes / custom)} (\/ {(.+)} / \/?)$""" +PEG_STORE_URL = peg"""^\/stores \/ {([a-z0-9_]+)} (\/ {(.+)} / \/?)$""" PEG_URL = peg"""^\/({(v\d+)} \/) {([^\/]+)} (\/ {(.+)} / \/?)$""" # Initialize LiteStore