hff-entry-forms/api/src/hff_entry_forms_api.nim

71 lines
1.9 KiB
Nim

import cliutils, docopt, json, logging, sequtils, strutils, tables
import hff_entry_forms_apipkg/api
import hff_entry_forms_apipkg/version
import hff_entry_forms_apipkg/service
let DEFAULT_CONFIG = HffEntryFormsApiConfig(
integrationToken: "CHANGE ME",
knownOrigins: @["http://curl.localhost"],
notionVersion: "2021-08-16",
port: 8300'i16
)
proc loadConfig(args: Table[string, docopt.Value]): HffEntryFormsApiConfig =
let filePath =
if args["--config"]: $args["--config"]
else: "hff_entry_forms_api.config.json"
var json: JsonNode
try: json = parseFile(filePath)
except:
json = %DEFAULT_CONFIG
warn "unable to load configuration file (expected at " & filePath & ")"
let cfg = CombinedConfig(docopt: args, json: json)
result = HffEntryFormsApiConfig(
debug: parseBool(cfg.getVal("debug")),
eventParentId: cfg.getVal("event-parent-id"),
integrationToken: cfg.getVal("integration-token"),
knownOrigins: cfg.getVal("known-origins")[1..^2].split(',').mapIt(it[1..^2]),
notionApiBaseUrl: cfg.getVal("notion-api-base-url"),
notionVersion: cfg.getVal("notion-version"),
port: parseInt(cfg.getVal("port", "8300")))
when isMainModule:
try:
let doc = """
Usage:
hff_entry_forms_api serve [options]
Options:
-C, --config <cfgFile> Location of the config file to use (defaults to
hff_entry_forms_api.config.json)
-d, --debug Log debugging information.
-p, --port <portNumber> Port number for the API server.
"""
let consoleLogger = newConsoleLogger(
levelThreshold=lvlInfo,
fmtStr="$app - $levelname: ")
logging.addHandler(consoleLogger)
# Initialize our service context
let args = docopt(doc, version = HFF_ENTRY_FORMS_API_VERSION)
if args["--debug"]:
consoleLogger.levelThreshold = lvlDebug
let cfg = loadConfig(args)
if args["serve"]: start(cfg)
except:
fatal getCurrentExceptionMsg()
quit(QuitFailure)