all repos — litestore @ 4f6b45c773c9488f8ee6e11ffbabd6c196b4270d

A minimalist nosql document store.

Now processing auth and config from system documents as well.
h3rald h3rald@h3rald.com
Fri, 06 Mar 2020 12:07:37 +0100
commit

4f6b45c773c9488f8ee6e11ffbabd6c196b4270d

parent

50acb806e40445b93f9778d6b9fdcf7477c65a09

M src/litestore.nimsrc/litestore.nim

@@ -26,6 +26,43 @@ {.passC: "-DSQLITE_ENABLE_FTS3=1 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_JSON1".}

when defined(linux): {.passL:"-static".} +proc processAuthConfig(configuration: JsonNode, auth: var JsonNode) = + if auth == newJNull() and configuration != newJNull() and configuration.hasKey("signature"): + auth = newJObject(); + auth["access"] = newJObject(); + auth["signature"] = configuration["signature"] + for k, v in configuration["resources"].pairs: + auth["access"][k] = newJObject() + for meth, content in v.pairs: + if content.hasKey("auth"): + auth["access"][k][meth] = content["auth"] + +proc processConfigSettings() = + # Process config settings if present and if no cli settings are set + if LS.config != newJNull() and LS.config.hasKey("settings"): + let settings = LS.config["settings"] + let cliSettings = LS.cliSettings + if not cliSettings.hasKey("address") and settings.hasKey("address"): + LS.address = settings["address"].getStr + if not cliSettings.hasKey("port") and settings.hasKey("port"): + LS.port = settings["port"].getInt + if not cliSettings.hasKey("store") and settings.hasKey("store"): + LS.file = settings["store"].getStr + if not cliSettings.hasKey("directory") and settings.hasKey("directory"): + LS.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: + LS.middleware[file.path.splitFile[1]] = file.path.readFile() + if not cliSettings.hasKey("log") and settings.hasKey("log"): + LS.logLevel = settings["log"].getStr + setLogLevel(LS.logLevel) + if not cliSettings.hasKey("mount") and settings.hasKey("mount"): + LS.mount = settings["mount"].getBool + if not cliSettings.hasKey("readonly") and settings.hasKey("readonly"): + LS.readonly = settings["readonly"].getBool + proc executeOperation*() = let file = LS.execution.file let body = LS.execution.body

@@ -101,13 +138,39 @@ else:

# Open Datastore setup(true) + if LS.configFile == "": + # Attempt to retrieve config.json from system documents + let options = newQueryOptions(true) + let rawDoc = LS.store.retrieveRawDocument("config.json", options) + if rawDoc != "": + LS.config = rawDoc.parseJson()["data"] + + if LS.config != newJNull(): + # Process config settings + processConfigSettings() + # Process auth from config settings + processAuthConfig(LS.config, LS.auth) + if LS.auth == newJNull(): # Attempt to retrieve auth.json from system documents let options = newQueryOptions(true) let rawDoc = LS.store.retrieveRawDocument("auth.json", options) if rawDoc != "": - LS.auth = rawDoc.parseJson() + LS.auth = rawDoc.parseJson()["data"] + # Validation + if LS.directory == "" and (LS.operation in [opDelete, opImport, opExport] or LS.mount): + fail(105, "--directory option not specified.") + + if LS.execution.file == "" and (LS.execution.operation in ["put", "post", "patch"]): + fail(109, "--file option not specified") + + if LS.execution.uri == "" and LS.operation == opExecute: + fail(110, "--uri option not specified") + + if LS.execution.operation == "" and LS.operation == opExecute: + fail(111, "--operation option not specified") + case LS.operation: of opRun: LS.serve
M src/litestorepkg/lib/api_v6.nimsrc/litestorepkg/lib/api_v6.nim

@@ -1042,7 +1042,7 @@ proc getMiddleware*(LS: LiteStore, id: string): string =

if not LS.middleware.hasKey(id): # Attempt to retrieve resource from system documents let options = newQueryOptions(true) - let doc = LS.store.retrieveDocument("custom/" & id, options) + let doc = LS.store.retrieveDocument("middleware/" & id & ".js", options) result = doc.data if result == "": LOG.warn("Middleware '$1' not found" % id)
M src/litestorepkg/lib/cli.nimsrc/litestorepkg/lib/cli.nim

@@ -69,7 +69,7 @@ -v, --version Display the program version.

-w, --middleware Specify a path to a folder containing middleware definitions. """ -proc setLogLevel(val: string) = +proc setLogLevel*(val: string) = case val: of "info": LOG.level = lvInfo

@@ -119,6 +119,8 @@ cliSettings["port"] = %port

of "store", "s": file = val cliSettings["store"] = %file + of "system": + system = true of "log", "l": if val == "": fail(102, "Log level not specified.")

@@ -185,59 +187,6 @@ discard

else: discard -# Process auth configuration if present - -if auth == newJNull() and configuration != newJNull() and configuration.hasKey("signature"): - auth = newJObject(); - auth["access"] = newJObject(); - auth["signature"] = configuration["signature"] - for k, v in configuration["resources"].pairs: - auth["access"][k] = newJObject() - for meth, content in v.pairs: - if content.hasKey("auth"): - auth["access"][k][meth] = content["auth"] - -# Process config settings if present and if no cli settings are set - -if configuration != newJNull() and configuration.hasKey("settings"): - let settings = configuration["settings"] - if not cliSettings.hasKey("address") and settings.hasKey("address"): - address = settings["address"].getStr - if not cliSettings.hasKey("port") and settings.hasKey("port"): - port = settings["port"].getInt - 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) - if not cliSettings.hasKey("mount") and settings.hasKey("mount"): - mount = settings["mount"].getBool - if not cliSettings.hasKey("readonly") and settings.hasKey("readonly"): - readonly = settings["readonly"].getBool - -# Validation - -if directory == "" and (operation in [opDelete, opImport, opExport] or mount): - fail(105, "--directory option not specified.") - -if exFile == "" and (exOperation in ["put", "post", "patch"]): - fail(109, "--file option not specified") - -if exUri == "" and operation == opExecute: - fail(110, "--uri option not specified") - -if exOperation == "" and operation == opExecute: - fail(111, "--operation option not specified") - - - LS.operation = operation LS.address = address LS.port = port

@@ -246,6 +195,7 @@ LS.directory = directory

LS.readonly = readonly LS.favicon = favicon LS.logLevel = logLevel +LS.cliSettings = cliSettings LS.auth = auth LS.manageSystemData = system LS.middleware = middleware
M src/litestorepkg/lib/core.nimsrc/litestorepkg/lib/core.nim

@@ -456,11 +456,11 @@

proc countDocuments*(store: Datastore): int64 = return store.db.getRow(SQL_COUNT_DOCUMENTS)[0].parseInt -proc importFile*(store: Datastore, f: string, dir = "", system = false) = +proc importFile*(store: Datastore, f: string, dir = "/", system = false) = if not f.fileExists: raise newException(EFileNotFound, "File '$1' not found." % f) let ext = f.splitFile.ext - var d_id = f.replace("\\", "/") + var d_id = f.replace("\\", "/")[dir.len+1..f.len-1]; var d_contents = f.readFile var d_ct = "application/octet-stream" if CONTENT_TYPES.hasKey(ext):

@@ -478,7 +478,7 @@ if system:

discard store.createSystemDocument(d_id, d_contents, d_ct, d_binary) else: discard store.createDocument(d_id, d_contents, d_ct, d_binary, d_searchable) - if dir != "" and not system: + if dir != "/" and not system: store.db.exec(SQL_INSERT_TAG, "$dir:"&dir, d_id) except: store.rollback()
M src/litestorepkg/lib/types.nimsrc/litestorepkg/lib/types.nim

@@ -84,6 +84,7 @@ port*: int

operation*: Operation config*: JsonNode configFile*: string + cliSettings*: JsonNode directory*: string manageSystemData*: bool file*: string