all repos — litestore @ 467097eee7c2c4c9b11cfd6d8639cb4f3c7734b2

A minimalist nosql document store.

Merge branch 'master' into middleware
h3rald h3rald@h3rald.com
Sun, 16 Feb 2020 11:20:06 +0100
commit

467097eee7c2c4c9b11cfd6d8639cb4f3c7734b2

parent

6e179b8996470ebdb356ced424b3f95dddb283e8

A src/litestorepkg/examples/config.json

@@ -0,0 +1,24 @@

+{ + "settings": { + "mount": true, + "directory": "admin" + }, + "resources": { + "/info": { + "GET": { "auth": ["admin:server"] } + }, + "/docs/*": { + "POST": { "auth": ["admin:server"] }, + "PATCH": { "auth": ["admin:server"] }, + "PUT": { "auth": ["admin:server"] }, + "DELETE": { "auth": ["admin:server"] } + }, + "/docs/wiki/*": { + "POST": { "auth": ["admin:wiki"] }, + "PUT": { "auth": ["admin:wiki"] }, + "PATCH": { "auth": ["admin:wiki"] }, + "DELETE": { "auth": ["admin:wiki"] } + } + }, + "signature": "\n-----BEGIN CERTIFICATE-----\n<certificate text goes here>\n-----END CERTIFICATE-----\n" +}
M src/litestorepkg/lib/cli.nimsrc/litestorepkg/lib/cli.nim

@@ -20,23 +20,26 @@ logLevel = "warn"

system = false mount = false auth = newJNull() - exOperation:string = "" customResources = newStringTable() - exFile:string = "" - exBody:string = "" - exType:string = "" - exUri:string = "" + configuration = newJNull() + authFile = "" + configFile = "" + exOperation = "" + exFile = "" + exBody = "" + exType = "" + exUri = "" + cliSettings = newJObject() let usage* = appname & " v" & pkgVersion & " - Lightweight REST Document Store" & """ -(c) 2015-2018 Fabio Cevasco +(c) 2015-2020 Fabio Cevasco Usage: litestore [command] [option1 option2 ...] Commands: - run Start LiteStore server (default if no command specified). delete Delete a previously-imported specified directory (requires -d). execute Execute an operation on data stored in the datastore (requires -o, -u, and in certain cases -f or -b and -t). import Import the specified directory into the datastore (requires -d).

@@ -50,6 +53,7 @@ --auth Specify an authentication/authorization configuration file.

-b, --body Specify a string containing input data for an operation to be executed. -w, --middleware Specify a path to a folder containing middleware definitions. -d, --directory Specify a directory to serve, import, export, delete, or mount. + -c, --config Specify a configuration file. -f, --file Specify a file containing input data for an operation to be executed. -h, --help Display this message. -l, --log Specify the log level: debug, info, warn, error, none (default: info)

@@ -91,12 +95,15 @@ of "address", "a":

if val == "": fail(100, "Address not specified.") address = val + cliSettings["address"] = %address of "port", "p": if val == "": fail(101, "Port not specified.") port = val.parseInt + cliSettings["port"] = %port of "store", "s": file = val + cliSettings["store"] = %file of "log", "l": if val == "": fail(102, "Log level not specified.")

@@ -114,10 +121,12 @@ LOG.level = lvNone

else: fail(103, "Invalid log level '$1'" % val) loglevel = val + cliSettings["log"] = %logLevel of "directory", "d": if val == "": fail(104, "Directory not specified.") directory = val + cliSettings["directory"] = %directory of "middleware", "w": if val == "": fail(115, "Middleware path not specified.")

@@ -149,9 +158,15 @@ exType = val

of "auth": if val == "": fail(114, "Authentication/Authorization configuration file not specified.") - auth = val.parseFile + authFile = val + of "config", "c": + if val == "": + fail(115, "Configuration file not specified.") + configuration = val.parseFile + configFile = val of "mount", "m": mount = true + cliSettings["mounnt"] = %mount of "version", "v": echo pkgVersion quit(0)

@@ -160,11 +175,39 @@ echo usage

quit(0) of "readonly", "r": readonly = true + cliSettings["readonly"] = %readonly else: 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: + if v.hasKey("auth"): + auth["access"][k] = v["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("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):

@@ -179,6 +222,8 @@

if exOperation == "" and operation == opExecute: fail(111, "--operation option not specified") + + LS.operation = operation LS.address = address LS.port = port

@@ -190,6 +235,9 @@ LS.loglevel = loglevel

LS.auth = auth LS.manageSystemData = system LS.customResources = customResources +LS.authFile = authFile +LS.config = configuration +LS.configFile = configFile LS.mount = mount LS.execution.file = exFile LS.execution.body = exBody
M src/litestorepkg/lib/server.nimsrc/litestorepkg/lib/server.nim

@@ -244,6 +244,16 @@ let res = req.process(LS)

let areq = asynchttpserver.Request(req) await areq.respond(res.code, res.content, res.headers) echo(LS.appname & " v" & LS.appversion & " started on " & LS.address & ":" & $LS.port & ".") + if LS.configFile != "": + echo "- Configuration File: " & LS.configFile + if LS.authFile != "": + echo "- Auth File: " & LS.authFile if LS.mount: - echo("Mirroring datastore changes to: " & LS.directory) + echo "- Mirroring datastore changes to: " & LS.directory + if LS.readonly: + echo "- Read-only mode" + echo "- Log Level: " & LS.loglevel + echo "- Store: " & LS.file + if LS.auth != newJNull(): + echo "- Authorization configured" asyncCheck server.serve(LS.port.Port, handleHttpRequest, LS.address)
M src/litestorepkg/lib/types.nimsrc/litestorepkg/lib/types.nim

@@ -16,6 +16,9 @@ EDirectoryNotFound* = object of Exception

EFileNotFound* = object of Exception EFileExists* = object of Exception EInvalidRequest* = object of Exception + ConfigFiles* = object + auth*: string + config*: string ExecutionData* = object operation*: string file*: string

@@ -71,6 +74,8 @@ execution*: ExecutionData

address*: string port*: int operation*: Operation + config*: JsonNode + configFile*: string directory*: string manageSystemData*: bool file*: string

@@ -80,6 +85,7 @@ appname*: string

customResources*: StringTableRef appversion*: string auth*: JsonNode + authFile*: string favicon*:string loglevel*:string LSRequest* = asynchttpserver.Request