all repos — litestore @ 2114e9d3e3fe70c059fdf2889229d2fb36c078ad

A minimalist nosql document store.

src/litestorepkg/lib/api_v6.nim

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
 1000
 1001
 1002
 1003
 1004
 1005
 1006
 1007
 1008
 1009
 1010
 1011
 1012
 1013
 1014
 1015
 1016
 1017
 1018
 1019
 1020
 1021
 1022
 1023
 1024
 1025
 1026
 1027
 1028
 1029
 1030
 1031
 1032
 1033
 1034
 1035
 1036
 1037
 1038
 1039
 1040
 1041
 1042
 1043
 1044
 1045
 1046
 1047
 1048
 1049
 1050
 1051
 1052
 1053
 1054
 1055
 1056
 1057
 1058
 1059
 1060
 1061
 1062
 1063
 1064
 1065
 1066
 1067
 1068
 1069
 1070
 1071
 1072
 1073
 1074
 1075
 1076
 1077
 1078
 1079
 1080
 1081
 1082
 1083
 1084
 1085
 1086
 1087
 1088
 1089
 1090
 1091
 1092
 1093
 1094
 1095
 1096
 1097
 1098
 1099
 1100
 1101
 1102
 1103
 1104
 1105
 1106
 1107
 1108
 1109
 1110
 1111
 1112
 1113
 1114
 1115
 1116
 1117
 1118
 1119
 1120
 1121
 1122
 1123
 1124
 1125
 1126
 1127
 1128
 1129
 1130
 1131
 1132
 1133
 1134
 1135
 1136
 1137
 1138
 1139
 1140
 1141
 1142
 1143
 1144
 1145
 1146
 1147
 1148
import
  asynchttpserver,
  strutils,
  sequtils,
  cgi,
  strtabs,
  pegs,
  json,
  os,
  uri,
  times
import
  types,
  contenttypes,
  core,
  utils,
  logger,
  duktape

# Helper procs

proc sqlOp(op: string): string =
  let table = newStringTable()
  table["not eq"] = "<>"
  table["eq"] = "=="
  table["gt"] = ">"
  table["gte"] = ">="
  table["lt"] = "<"
  table["lte"] = "<="
  table["contains"] = "contains"
  table["like"] = "like"
  return table[op]

proc orderByClauses*(str: string): string =
  var clauses = newSeq[string]()
  var fragments = str.split(",")
  let clause = peg"""
    clause <- {[-+]} {field}
    field <- ('id' / 'created' / 'modified' / path)
    path <- '$' (objField)+
    ident <- [a-zA-Z0-9_]+
    objField <- '.' ident
  """
  for f in fragments:
    var matches = @["", ""]
    if f.find(clause, matches) != -1:
      var field = matches[1]
      if field[0] == '$':
        field = "json_extract(documents.data, '$1')" % matches[1]
      if matches[0] == "-":
        clauses.add("$1 COLLATE NOCASE DESC" % field)
      else:
        clauses.add("$1 COLLATE NOCASE ASC" % field)
  return clauses.join(", ")

proc selectClause*(str: string, options: var QueryOptions) =
  let tokens = """
    path <- '$' (objItem / objField)+
    ident <- [a-zA-Z0-9_]+
    objIndex <- '[' \d+ ']'
    objField <- '.' ident
    objItem <- objField objIndex
  """
  let fields = peg("""
    fields <- ^{field} (\s* ',' \s* {field})*$
    field <- path \s+ ('as' / 'AS') \s+ ident
  """ & tokens)
  let field = peg("""
    field <- ^{path} \s+ ('as' / 'AS') \s+ {ident}$
  """ & tokens)
  var fieldMatches = newSeq[string](10)
  if str.strip.match(fields, fieldMatches):
    for m in fieldMatches:
      if m.len > 0:
        var rawTuple = newSeq[string](2)
        if m.match(field, rawTuple):
          options.jsonSelect.add((path: rawTuple[0], alias: rawTuple[1]))

proc filterClauses*(str: string, options: var QueryOptions) =
  let tokens = """
    operator <- 'not eq' / 'eq' / 'gte' / 'gt' / 'lte' / 'lt' / 'contains' / 'like'
    value <- string / number / 'null' / 'true' / 'false'
    string <- '"' ('\\"' . / [^"])* '"'
    number <- '-'? '0' / [1-9] [0-9]* ('.' [0-9]+)? (( 'e' / 'E' ) ( '+' / '-' )? [0-9]+)?
    path <- '$' (objItem / objField)+
    ident <- [a-zA-Z0-9_]+
    objIndex <- '[' \d+ ']'
    objField <- '.' ident
    objItem <- objField objIndex
  """
  let clause = peg("""
    clause <- {path} \s+ {operator} \s+ {value}
  """ & tokens)
  let andClauses = peg("""
    andClauses <- ^{clause} (\s+ 'and' \s+ {clause})*$
    clause <- path \s+ operator \s+ value
  """ & tokens)
  let orClauses = peg("""
    orClauses <- ^{andClauses} (\s+ 'or' \s+ {andClauses})*$
    andClauses <- clause (\s+ 'and' \s+ clause)*
    clause <- path \s+ operator \s+ value
  """ & tokens)
  var orClausesMatches = newSeq[string](10)
  discard str.strip.match(orClauses, orClausesMatches)
  var parsedClauses = newSeq[seq[seq[string]]]()
  for orClause in orClausesMatches:
    if orClause.len > 0:
      var andClausesMatches = newSeq[string](10)
      discard orClause.strip.match(andClauses, andClausesMatches)
      var parsedAndClauses = newSeq[seq[string]]()
      for andClause in andClausesMatches:
        if andClause.len > 0:
          var clauses = newSeq[string](3)
          discard andClause.strip.match(clause, clauses)
          clauses[1] = sqlOp(clauses[1])
          if clauses[2] == "true":
            clauses[2] = "1"
          elif clauses[2] == "false":
            clauses[2] = "0"
          parsedAndClauses.add clauses
      if parsedAndClauses.len > 0:
        parsedClauses.add parsedAndClauses
  if parsedClauses.len == 0:
    return
  var currentArr = 0
  var tables = newSeq[string]()
  let resOrClauses = parsedClauses.map do (it: seq[seq[string]]) -> string:
    let resAndClauses = it.map do (x: seq[string]) -> string:
      if x[1] == "contains":
        currentArr = currentArr + 1
        tables.add "json_each(documents.data, '$1') AS arr$2" % [x[0], $currentArr]
        return "arr$1.value == $2" % [$currentArr, x[2]]
      else:
        var arr = @[x[0], x[1], x[2]]
        if x[1] == "like":
          arr[2] = x[2].replace('*', '%')
        return "json_extract(documents.data, '$1') $2 $3 " % arr
    return resAndClauses.join(" AND ")
  options.tables = options.tables & tables
  options.jsonFilter = resOrClauses.join(" OR ")

proc parseQueryOption*(fragment: string, options: var QueryOptions) =
  if fragment == "":
    return
  var pair = fragment.split('=')
  if pair.len < 2 or pair[1] == "":
    raise newException(EInvalidRequest, "Invalid query string fragment '$1'" % fragment)
  try:
    pair[1] = pair[1].replace("+", "%2B").decodeURL
  except:
    raise newException(EInvalidRequest, "Unable to decode query string fragment '$1'" % fragment)
  case pair[0]:
    of "filter":
      filterClauses(pair[1], options)
      if options.jsonFilter == "":
        raise newException(EInvalidRequest, "Invalid filter clause: $1" % pair[1].replace("\"", "\\\""))
    of "select":
      selectClause(pair[1], options)
      if options.jsonSelect.len == 0:
        raise newException(EInvalidRequest, "Invalid select clause: $1" % pair[1].replace("\"", "\\\""))
    of "like":
      options.like = pair[1]
    of "search":
      options.search = pair[1]
    of "tags":
      options.tags = pair[1]
    of "created-after":
      try:
        options.createdAfter = pair[1].parseInt.fromUnix.utc.format("yyyy-MM-dd'T'HH:mm:ss'Z'")
      except:
        raise newException(EInvalidRequest, "Invalid created-after value: $1" % getCurrentExceptionMsg())
    of "created-before":
      try:
        options.createdBefore = pair[1].parseInt.fromUnix.utc.format("yyyy-MM-dd'T'HH:mm:ss'Z'")
      except:
        raise newException(EInvalidRequest, "Invalid created-before value: $1" % getCurrentExceptionMsg())
    of "modified-after":
      try:
        options.modifiedAfter = pair[1].parseInt.fromUnix.utc.format("yyyy-MM-dd'T'HH:mm:ss'Z'")
      except:
        raise newException(EInvalidRequest, "Invalid modified.after value: $1" % getCurrentExceptionMsg())
    of "modified-before":
      try:
        options.modifiedBefore = pair[1].parseInt.fromUnix.utc.format("yyyy-MM-dd'T'HH:mm:ss'Z'")
      except:
        raise newException(EInvalidRequest, "Invalid modified-before value: $1" % getCurrentExceptionMsg())
    of "limit":
      try:
        options.limit = pair[1].parseInt
      except:
        raise newException(EInvalidRequest, "Invalid limit value: $1" % getCurrentExceptionMsg())
    of "offset":
      try:
        options.offset = pair[1].parseInt
      except:
        raise newException(EInvalidRequest, "Invalid offset value: $1" % getCurrentExceptionMsg())
    of "sort":
      let orderby = pair[1].orderByClauses()
      if orderby != "":
        options.orderby = orderby
      else:
        raise newException(EInvalidRequest, "Invalid sort value: $1" % pair[1])
    of "contents", "raw":
      discard
    else:
      discard

proc parseQueryOptions*(querystring: string, options: var QueryOptions) =
  var fragments = querystring.split('&')
  for f in fragments:
    f.parseQueryOption(options)

proc validate*(req: LSRequest, LS: LiteStore, resource: string, id: string, cb: proc(req: LSRequest, LS: LiteStore, resource: string, id: string):LSResponse): LSResponse =
  if req.reqMethod == HttpPost or req.reqMethod == HttpPut or req.reqMethod == HttpPatch:
    var ct =  ""
    let body = req.body.strip
    if body == "":
      return resError(Http400, "Bad request: No content specified for document.")
    if req.headers.hasKey("Content-Type"):
      ct = req.headers["Content-Type"]
      case ct:
        of "application/json":
          try:
            discard body.parseJson()
          except:
            return resError(Http400, "Invalid JSON content - $1" % getCurrentExceptionMsg())
        else:
          discard
  return cb(req, LS, resource, id)

proc patchTag(tags: var seq[string], index: int, op, path, value: string): bool =
  LOG.debug("- PATCH -> $1 tag['$2'] = \"$3\" - Total tags: $4." % [op, $index, $value, $tags.len])
  case op:
    of "remove":
      let tag = tags[index]
      if not tag.startsWith("$"):
        tags[index] = "" # Not removing element, otherwise subsequent indexes won't work!
      else:
        raise newException(EInvalidRequest, "cannot remove system tag: $1" % tag)
    of "add":
      if value.match(PEG_USER_TAG):
        tags.insert(value, index)
      else:
        if value.strip == "":
          raise newException(EInvalidRequest, "tag not specified." % value)
        else:
          raise newException(EInvalidRequest, "invalid tag: $1" % value)
    of "replace":
      if value.match(PEG_USER_TAG):
        if tags[index].startsWith("$"):
          raise newException(EInvalidRequest, "cannot replace system tag: $1" % tags[index])
        else:
          tags[index] = value
      else:
        if value.strip == "":
          raise newException(EInvalidRequest, "tag not specified." % value)
        else:
          raise newException(EInvalidRequest, "invalid tag: $1" % value)
    of "test":
      if tags[index] != value:
        return false
    else:
      raise newException(EInvalidRequest, "invalid patch operation: $1" % op)
  return true

proc patchData*(data: var JsonNode, origData: JsonNode, op: string, path: string, value: JsonNode): bool =
  LOG.debug("- PATCH -> $1 path $2 with $3" % [op, path, $value])
  var keys = path.replace(peg"^\/data\/", "").split("/")
  if keys.len == 0:
    raise newException(EInvalidRequest, "no valid path specified: $1" % path)
  var d = data
  var dorig = origData
  var c = 1
  for key in keys:
    if d.kind == JArray:
      try:
        var index = key.parseInt
        if c >= keys.len:
          d.elems[index] = value
          case op:
            of "remove":
              d.elems.del(index)
            of "add":
              d.elems.insert(value, index)
            of "replace":
              d.elems[index] = value
            of "test":
              if d.elems[index] != value:
                return false
            else:
              raise newException(EInvalidRequest, "invalid patch operation: $1" % op)
        else:
          d = d[index]
          dorig = dorig[index]
      except:
        raise newException(EInvalidRequest, "invalid index key '$1' in path '$2'" % [key, path])
    else:
      if c >= keys.len:
        case op:
          of "remove":
            if d.hasKey(key):
              d.delete(key)
            else:
              raise newException(EInvalidRequest, "key '$1' not found in path '$2'" % [key, path])
          of "add":
            d[key] = value
          of "replace":
            if d.hasKey(key):
              d[key] = value
            else:
              raise newException(EInvalidRequest, "key '$1' not found in path '$2'" % [key, path])
          of "test":
            if dorig.hasKey(key):
              if dorig[key] != value:
                return false
            else:
              raise newException(EInvalidRequest, "key '$1' not found in path '$2'" % [key, path])
          else:
            raise newException(EInvalidRequest, "invalid patch operation: $1" % op)
      else:
        d = d[key]
        dorig = dorig[key]
    c += 1
  return true


proc applyPatchOperation*(data: var JsonNode, origData: JsonNode, tags: var seq[string], op: string, path: string, value: JsonNode): bool =
  var matches = @[""]
  let p = peg"""
    path <- ^tagPath / fieldPath$
    tagPath <- '\/tags\/' {\d+}
    fieldPath <- '\/data\/' ident ('\/' ident)*
    ident <- [a-zA-Z0-9_]+ / '-'
  """
  if path.find(p, matches) == -1:
    raise newException(EInvalidRequest, "cannot patch path '$1'" % path)
  if path.match(peg"^\/tags\/"):
    let index = matches[0].parseInt
    if value.kind != JString:
      raise newException(EInvalidRequest, "tag '$1' is not a string." % $value)
    let tag = value.getStr
    return patchTag(tags, index, op, path, tag)
  elif tags.contains("$subtype:json"):
    return patchData(data, origData, op, path, value)
  else:
    raise newException(EInvalidRequest, "cannot patch data of a non-JSON document.")

# Low level procs

proc getTag*(LS: LiteStore, id: string, options = newQueryOptions(), req: LSRequest): LSResponse =
  let doc = LS.store.retrieveTag(id, options)
  result.headers = ctJsonHeader()
  setOrigin(LS, req, result.headers)
  if doc == newJNull():
    result = resTagNotFound(id)
  else:
    result.content = $doc
    result.code = Http200

proc getIndex*(LS: LiteStore, id: string, options = newQueryOptions(), req: LSRequest): LSResponse =
  let doc = LS.store.retrieveIndex(id, options)
  result.headers = ctJsonHeader()
  setOrigin(LS, req, result.headers)
  if doc == newJNull():
    result = resIndexNotFound(id)
  else:
    result.content = $doc
    result.code = Http200

proc getRawDocument*(LS: LiteStore, id: string, options = newQueryOptions(), req: LSRequest): LSResponse =
  let doc = LS.store.retrieveRawDocument(id, options)
  result.headers = ctJsonHeader()
  setOrigin(LS, req, result.headers)
  if doc == "":
    result = resDocumentNotFound(id)
  else:
    result.content = doc
    result.code = Http200

proc getDocument*(LS: LiteStore, id: string, options = newQueryOptions(), req: LSRequest): LSResponse =
  let doc = LS.store.retrieveDocument(id, options)
  if doc.data == "":
    result = resDocumentNotFound(id)
  else:
    result.headers = doc.contenttype.ctHeader
    setOrigin(LS, req, result.headers)
    result.content = doc.data
    result.code = Http200

proc deleteDocument*(LS: LiteStore, id: string, req: LSRequest): LSResponse =
  let doc = LS.store.retrieveDocument(id)
  if doc.data == "":
    result = resDocumentNotFound(id)
  else:
    try:
      let res = LS.store.destroyDocument(id)
      if res == 0:
        result = resError(Http500, "Unable to delete document '$1'" % id)
      else:
        result.headers = newHttpHeaders(TAB_HEADERS)
        setOrigin(LS, req, result.headers)
        result.headers["Content-Length"] = "0"
        result.content = ""
        result.code = Http204
    except:
      result = resError(Http500, "Unable to delete document '$1'" % id)

proc getTags*(LS: LiteStore, options: QueryOptions = newQueryOptions(), req: LSRequest): LSResponse =
  var options = options
  let t0 = cpuTime()
  let docs = LS.store.retrieveTags(options)
  let orig_limit = options.limit
  let orig_offset = options.offset
  options.limit = 0
  options.offset = 0
  options.select = @["COUNT(tag_id)"]
  let total = LS.store.countTags(prepareSelectTagsQuery(options), options.like.replace("*", "%"))
  var content = newJObject()
  if options.like != "":
    content["like"] = %(options.like.decodeURL)
  if orig_limit > 0:
    content["limit"] = %orig_limit
    if orig_offset > 0:
      content["offset"] = %orig_offset
  content["total"] = %total
  content["execution_time"] = %(cputime()-t0)
  content["results"] = docs
  result.headers = ctJsonHeader()
  setOrigin(LS, req, result.headers)
  result.content = content.pretty
  result.code = Http200

proc getIndexes*(LS: LiteStore, options: QueryOptions = newQueryOptions(), req: LSRequest): LSResponse =
  var options = options
  let t0 = cpuTime()
  let docs = LS.store.retrieveIndexes(options)
  let orig_limit = options.limit
  let orig_offset = options.offset
  options.limit = 0
  options.offset = 0
  options.select = @["COUNT(name)"]
  let total = LS.store.countIndexes(prepareSelectIndexesQuery(options), options.like.replace("*", "%"))
  var content = newJObject()
  if options.like != "":
    content["like"] = %(options.like.decodeURL)
  if orig_limit > 0:
    content["limit"] = %orig_limit
    if orig_offset > 0:
      content["offset"] = %orig_offset
  content["total"] = %total
  content["execution_time"] = %(cputime()-t0)
  content["results"] = docs
  result.headers = ctJsonHeader()
  setOrigin(LS, req, result.headers)
  result.content = content.pretty
  result.code = Http200

proc getRawDocuments*(LS: LiteStore, options: QueryOptions = newQueryOptions(), req: LSRequest): LSResponse =
  var options = options
  let t0 = cpuTime()
  let docs = LS.store.retrieveRawDocuments(options)
  let orig_limit = options.limit
  let orig_offset = options.offset
  options.limit = 0
  options.offset = 0
  options.select = @["COUNT(docid)"]
  let total = LS.store.retrieveRawDocuments(options)[0].num
  var content = newJObject()
  if options.folder != "":
    content["folder"] = %(options.folder)
  if options.search != "":
    content["search"] = %(options.search.decodeURL)
  if options.tags != "":
    content["tags"] = newJArray()
    for tag in options.tags.replace("+", "%2B").decodeURL.split(","):
      content["tags"].add(%tag)
  if orig_limit > 0:
    content["limit"] = %orig_limit
    if orig_offset > 0:
      content["offset"] = %orig_offset
  if options.orderby != "":
    content["sort"] = %options.orderby
  content["total"] = %total
  content["execution_time"] = %(cputime()-t0)
  content["results"] = docs
  result.headers = ctJsonHeader()
  setOrigin(LS, req, result.headers)
  result.content = content.pretty
  result.code = Http200

proc getInfo*(LS: LiteStore, req: LSRequest): LSResponse =
  let info = LS.store.retrieveInfo()
  let version = info[0]
  let total_documents = info[1]
  let total_tags = LS.store.countTags()
  let tags = LS.store.retrieveTagsWithTotals()
  var content = newJObject()
  content["version"] = %(LS.appname & " v" & LS.appversion)
  content["datastore_version"] = %version
  content["size"] = %($((LS.file.getFileSize().float/(1024*1024)).formatFloat(ffDecimal, 2)) & " MB")
  content["read_only"] = %LS.readonly
  content["log_level"] = %LS.loglevel
  if LS.directory.len == 0:
    content["directory"] = newJNull()
  else:
    content["directory"] = %LS.directory
  content["mount"] = %LS.mount
  content["total_documents"] = %total_documents
  content["total_tags"] = %total_tags
  content["tags"] = tags
  result.headers = ctJsonHeader()
  setOrigin(LS, req, result.headers)
  result.content = content.pretty
  result.code = Http200

proc putIndex*(LS: LiteStore, id, field: string, req: LSRequest): LSResponse =
  try:
    if (not id.match(PEG_INDEX)):
      return resError(Http400, "invalid index ID: $1" % id)
    if (not field.match(PEG_JSON_FIELD)):
      return resError(Http400, "invalid field path: $1" % field)
    if (LS.store.retrieveIndex(id) != newJNull()):
      return resError(Http409, "Index already exists: $1" % id)
    LS.store.createIndex(id, field)
    result.headers = ctJsonHeader()
    setOrigin(LS, req, result.headers)
    result.content = "{\"id\": \"$1\", \"field\": \"$2\"}" % [id, field]
    result.code = Http200
  except:
    eWarn()
    result = resError(Http500, "Unable to create index.")

proc deleteIndex*(LS: LiteStore, id: string, req: LSRequest): LSResponse =
  if (not id.match(PEG_INDEX)):
    return resError(Http400, "invalid index ID: $1" % id)
  if (LS.store.retrieveIndex(id) == newJNull()):
    return resError(Http404, "Index not found: $1" % id)
  try:
    LS.store.dropIndex(id)
    result.headers = newHttpHeaders(TAB_HEADERS)
    setOrigin(LS, req, result.headers)
    result.headers["Content-Length"] = "0"
    result.content = ""
    result.code = Http204
  except:
    eWarn()
    result = resError(Http500, "Unable to delete index.")

proc postDocument*(LS: LiteStore, body: string, ct: string, folder="", req: LSRequest): LSResponse =
  if not folder.isFolder:
    return resError(Http400, "Invalid folder specified when creating document: $1" % folder)
  try:
    var doc = LS.store.createDocument(folder, body, ct)
    if doc != "":
      result.headers = ctJsonHeader()
      setOrigin(LS, req, result.headers)
      result.content = doc
      result.code = Http201
    else:
      result = resError(Http500, "Unable to create document.")
  except:
    eWarn()
    result = resError(Http500, "Unable to create document.")

proc putDocument*(LS: LiteStore, id: string, body: string, ct: string, req: LSRequest): LSResponse =
  if id.isFolder:
    return resError(Http400, "Invalid ID '$1' (Document IDs cannot end with '/')." % id)
  let doc = LS.store.retrieveDocument(id)
  if doc.data == "":
    # Create a new document
    var doc = LS.store.createDocument(id, body, ct)
    if doc != "":
      result.headers = ctJsonHeader()
      setOrigin(LS, req, result.headers)
      result.content = doc
      result.code = Http201
    else:
      result = resError(Http500, "Unable to create document.")
  else:
    # Update existing document
    try:
      var doc = LS.store.updateDocument(id, body, ct)
      if doc != "":
        result.headers = ctJsonHeader()
        setOrigin(LS, req, result.headers)
        result.content = doc
        result.code = Http200
      else:
        result = resError(Http500, "Unable to update document '$1'." % id)
    except:
      result = resError(Http500, "Unable to update document '$1'." % id)

proc patchDocument*(LS: LiteStore, id: string, body: string, req: LSRequest): LSResponse =
  var apply = true
  let jbody = body.parseJson
  if jbody.kind != JArray:
    return resError(Http400, "Bad request: PATCH request body is not an array.")
  var options = newQueryOptions()
  options.select = @["documents.id AS id", "created", "modified", "data"]
  let doc = LS.store.retrieveRawDocument(id, options)
  if doc == "":
    return resDocumentNotFound(id)
  let jdoc = doc.parseJson
  var tags = newSeq[string]()
  var origTags = newSeq[string]()
  for tag in jdoc["tags"].items:
    tags.add(tag.str)
    origTags.add(tag.str)
  var data: JsonNode
  var origData: JsonNode
  if tags.contains("$subtype:json"):
    try:
      origData = jdoc["data"].getStr.parseJson
      data = origData.copy
    except:
      discard
  var c = 1
  for item in jbody.items:
    if item.hasKey("op") and item.hasKey("path"):
      if not item.hasKey("value"):
        item["value"] = %""
      try:
        apply = applyPatchOperation(data, origData, tags, item["op"].str, item["path"].str, item["value"])
        if not apply:
          break
      except:
        return resError(Http400, "Bad request - $1" % getCurrentExceptionMsg())
    else:
        return resError(Http400, "Bad request: patch operation #$1 is malformed." % $c)
    c.inc
  if apply:
    if origData.len > 0 and origData != data:
      try:
        var doc = LS.store.updateDocument(id, data.pretty, "application/json")
        if doc == "":
          return resError(Http500, "Unable to patch document '$1'." % id)
      except:
        return resError(Http500, "Unable to patch document '$1' - $2" % id, getCurrentExceptionMsg())
    if origTags != tags:
      try:
        for t1 in jdoc["tags"].items:
          discard LS.store.destroyTag(t1.str, id, true)
        for t2 in tags:
          if t2 != "":
            LS.store.createTag(t2, id, true)
      except:
        return resError(Http500, "Unable to patch document '$1' - $2" % [id, getCurrentExceptionMsg()])
  return LS.getRawDocument(id, newQueryOptions(), req)

# Main routing

proc options*(req: LSRequest, LS: LiteStore, resource: string, id = ""): LSResponse =
  case resource:
    of "info":
      result.headers = newHttpHeaders(TAB_HEADERS)
      setOrigin(LS, req, result.headers)
      result.headers["Allow"] = "GET, OPTIONS"
      result.headers["Access-Control-Allow-Methods"] = "GET, OPTIONS"
      if id != "":
        return resError(Http404, "Info '$1' not found." % id)
      else:
        result.code = Http204
        result.content = ""
    of "dir":
      result.code = Http204
      result.content = ""
      result.headers = newHttpHeaders(TAB_HEADERS)
      setOrigin(LS, req, result.headers)
      result.headers["Allow"] = "GET, OPTIONS"
      result.headers["Access-Control-Allow-Methods"] = "GET, OPTIONS"
    of "tags":
      result.code = Http204
      result.content = ""
      result.headers = newHttpHeaders(TAB_HEADERS)
      setOrigin(LS, req, result.headers)
      result.headers["Allow"] = "GET, OPTIONS"
      result.headers["Access-Control-Allow-Methods"] = "GET, OPTIONS"
    of "indexes":
      result.code = Http204
      result.content = ""
      result.headers = newHttpHeaders(TAB_HEADERS)
      setOrigin(LS, req, result.headers)
      if id != "":
        result.code = Http204
        result.content = ""
        if LS.readonly:
          result.headers["Allow"] = "GET, OPTIONS"
          result.headers["Access-Control-Allow-Methods"] = "GET, OPTIONS"
        else:
          result.headers["Allow"] = "GET, OPTIONS, PUT, DELETE"
          result.headers["Access-Control-Allow-Methods"] = "GET, OPTIONS, PUT, DELETE"
      else:
        result.code = Http204
        result.content = ""
        if LS.readonly:
          result.headers = newHttpHeaders(TAB_HEADERS)
          setOrigin(LS, req, result.headers)
          result.headers["Allow"] = "GET, OPTIONS"
          result.headers["Access-Control-Allow-Methods"] = "GET, OPTIONS"
        else:
          result.headers = newHttpHeaders(TAB_HEADERS)
          setOrigin(LS, req, result.headers)
          result.headers["Allow"] = "GET, OPTIONS"
          result.headers["Access-Control-Allow-Methods"] = "GET, OPTIONS"
    of "docs":
      var folder: string
      if id.isFolder:
        folder = id
      if folder.len > 0:
        result.code = Http204
        result.content = ""
        if LS.readonly:
          result.headers = newHttpHeaders(TAB_HEADERS)
          setOrigin(LS, req, result.headers)
          result.headers["Allow"] = "HEAD, GET, OPTIONS"
          result.headers["Access-Control-Allow-Methods"] = "HEAD, GET, OPTIONS"
        else:
          result.headers = newHttpHeaders(TAB_HEADERS)
          setOrigin(LS, req, result.headers)
          result.headers["Allow"] = "HEAD, GET, OPTIONS, POST, PUT"
          result.headers["Access-Control-Allow-Methods"] = "HEAD, GET, OPTIONS, POST, PUT"
      elif id != "":
        result.code = Http204
        result.content = ""
        if LS.readonly:
          result.headers = newHttpHeaders(TAB_HEADERS)
          setOrigin(LS, req, result.headers)
          result.headers["Allow"] = "HEAD, GET, OPTIONS"
          result.headers["Access-Control-Allow-Methods"] = "HEAD, GET, OPTIONS"
        else:
          result.headers = newHttpHeaders(TAB_HEADERS)
          setOrigin(LS, req, result.headers)
          result.headers["Allow"] = "HEAD, GET, OPTIONS, PUT, PATCH, DELETE"
          result.headers["Allow-Patch"] = "application/json-patch+json"
          result.headers["Access-Control-Allow-Methods"] = "HEAD, GET, OPTIONS, PUT, PATCH, DELETE"
      else:
        result.code = Http204
        result.content = ""
        if LS.readonly:
          result.headers = newHttpHeaders(TAB_HEADERS)
          setOrigin(LS, req, result.headers)
          result.headers["Allow"] = "HEAD, GET, OPTIONS"
          result.headers["Access-Control-Allow-Methods"] = "HEAD, GET, OPTIONS"
        else:
          result.headers = newHttpHeaders(TAB_HEADERS)
          setOrigin(LS, req, result.headers)
          result.headers["Allow"] = "HEAD, GET, OPTIONS, POST"
          result.headers["Access-Control-Allow-Methods"] = "HEAD, GET, OPTIONS, POST"
    else:
      discard # never happens really.

proc head*(req: LSRequest, LS: LiteStore, resource: string, id = ""): LSResponse =
  var options = newQueryOptions()
  options.select = @["documents.id AS id", "created", "modified"]
  if id.isFolder:
    options.folder = id
  try:
    parseQueryOptions(req.url.query, options);
    if id != "" and options.folder == "":
      result = LS.getRawDocument(id, options, req)
      result.content = ""
    else:
      result = LS.getRawDocuments(options, req)
      result.content = ""
  except:
    return resError(Http400, "Bad request - $1" % getCurrentExceptionMsg())

proc get*(req: LSRequest, LS: LiteStore, resource: string, id = ""): LSResponse =
  case resource:
    of "docs":
      var options = newQueryOptions()
      if id.isFolder:
        options.folder = id
      if req.url.query.contains("contents=false"):
        options.select = @["documents.id AS id", "created", "modified"]
      try:
        parseQueryOptions(req.url.query, options);
        if id != "" and options.folder == "":
          if req.url.query.contains("raw=true") or req.headers.hasKey("Accept") and req.headers["Accept"] == "application/json":
            return LS.getRawDocument(id, options, req)
          else:
            return LS.getDocument(id, options, req)
        else:
          return LS.getRawDocuments(options, req)
      except:
        let e = getCurrentException()
        let trace = e.getStackTrace()
        echo trace
        return resError(Http400, "Bad Request - $1" % getCurrentExceptionMsg())
    of "tags":
      var options = newQueryOptions()
      try:
        parseQueryOptions(req.url.query, options);
        if id != "":
          return LS.getTag(id, options, req)
        else:
          return LS.getTags(options, req)
      except:
        return resError(Http400, "Bad Request - $1" % getCurrentExceptionMsg())
    of "indexes":
      var options = newQueryOptions()
      try:
        parseQueryOptions(req.url.query, options);
        if id != "":
          return LS.getIndex(id, options, req)
        else:
          return LS.getIndexes(options, req)
      except:
        return resError(Http400, "Bad Request - $1" % getCurrentExceptionMsg())
    of "info":
      if id != "":
        return resError(Http404, "Info '$1' not found." % id)
      return LS.getInfo(req)
    else:
      discard # never happens really.

proc post*(req: LSRequest, LS: LiteStore, resource: string, id = ""): LSResponse =
  var ct = "text/plain"
  if req.headers.hasKey("Content-Type"):
    ct = req.headers["Content-Type"]
  return LS.postDocument(req.body.strip, ct, id, req)

proc put*(req: LSRequest, LS: LiteStore, resource: string, id = ""): LSResponse =
  if id != "":
    if resource == "indexes":
      var field = ""
      try:
        field = parseJson(req.body.strip)["field"].getStr
      except:
        return resError(Http400, "Bad Request - Invalid JSON body - $1" % getCurrentExceptionMsg())
      return LS.putIndex(id, field, req)
    else: # Assume docs
      var ct = "text/plain"
      if req.headers.hasKey("Content-Type"):
        ct = req.headers["Content-Type"]
      return LS.putDocument(id, req.body.strip, ct, req)
  else:
    return resError(Http400, "Bad request: document ID must be specified in PUT requests.")

proc delete*(req: LSRequest, LS: LiteStore, resource: string, id = ""): LSResponse =
  if id != "":
    if resource == "indexes":
      return LS.deleteIndex(id, req)
    else: # Assume docs
      return LS.deleteDocument(id, req)
  else:
    return resError(Http400, "Bad request: document ID must be specified in DELETE requests.")

proc patch*(req: LSRequest, LS: LiteStore, resource: string, id = ""): LSResponse =
  if id != "":
    return LS.patchDocument(id, req.body, req)
  else:
    return resError(Http400, "Bad request: document ID must be specified in PATCH requests.")

proc serveFile*(req: LSRequest, LS: LiteStore, id: string): LSResponse =
  let path = LS.directory / id
  var reqMethod = $req.reqMethod
  if req.headers.hasKey("X-HTTP-Method-Override"):
    reqMethod = req.headers["X-HTTP-Method-Override"]
  case reqMethod.toUpperAscii:
    of "OPTIONS":
      return validate(req, LS, "dir", id, options)
    of "GET":
      if path.fileExists:
        try:
          let contents = path.readFile
          let parts = path.splitFile
          if CONTENT_TYPES.hasKey(parts.ext):
            result.headers = CONTENT_TYPES[parts.ext].ctHeader
          else:
            result.headers = ctHeader("text/plain")
          setOrigin(LS, req, result.headers)
          result.content = contents
          result.code = Http200
        except:
          return resError(Http500, "Unable to read file '$1'." % path)
      else:
        return resError(Http404, "File '$1' not found." % path)
    else:
      return resError(Http405, "Method not allowed: $1" % $req.reqMethod)

proc route*(req: LSRequest, LS: LiteStore, resource = "docs", id = ""): LSResponse =
  var reqMethod = $req.reqMethod
  if req.headers.hasKey("X-HTTP-Method-Override"):
    reqMethod = req.headers["X-HTTP-Method-Override"]
  case reqMethod.toUpperAscii:
    of "POST":
      if LS.readonly:
        return resError(Http405, "Method not allowed: $1" % $req.reqMethod)
      return validate(req, LS, resource, id, post)
    of "PUT":
      if LS.readonly:
        return resError(Http405, "Method not allowed: $1" % $req.reqMethod)
      return validate(req, LS, resource, id, put)
    of "DELETE":
      if LS.readonly:
        return resError(Http405, "Method not allowed: $1" % $req.reqMethod)
      return validate(req, LS, resource, id, delete)
    of "HEAD":
      return validate(req, LS, resource, id, head)
    of "OPTIONS":
      return validate(req, LS, resource, id, options)
    of "GET":
      return validate(req, LS, resource, id, get)
    of "PATCH":
      if LS.readonly:
        return resError(Http405, "Method not allowed: $1" % $req.reqMethod)
      return validate(req, LS, resource, id, patch)
    else:
      return resError(Http405, "Method not allowed: $1" % $req.reqMethod)

proc newSimpleLSRequest(meth: HttpMethod, resource, id,  body = "", params = "", headers = newHttpHeaders()): LSRequest =
  result.reqMethod = meth
  result.body = body
  result.headers = headers
  result.url = parseUri("$1://$2:$3/$4/$5?$6" % @["http", "localhost", "9500", resource, id, params])
  
proc get(resource, id: string, params = ""): LSResponse =
  return newSimpleLSRequest(HttpGet, resource, id, "", params).get(LS, resource, id)

proc post(resource, folder, body: string, ct = ""): LSResponse =
  var headers = newHttpHeaders()
  if ct != "":
    headers["Content-Type"] = ct
  return newSimpleLSRequest(HttpPost, resource, "", body, "", headers).post(LS, resource, folder & "/")

proc put(resource, id, body: string, ct = ""): LSResponse =
  var headers = newHttpHeaders()
  if ct != "":
    headers["Content-Type"] = ct
  return newSimpleLSRequest(HttpPut, resource, id, body, "", headers).put(LS, resource, id)

proc patch(resource, id, body: string): LSResponse =
  var headers = newHttpHeaders()
  headers["Content-Type"] = "application/json"
  return newSimpleLSRequest(HttpPatch, resource, id, body, "", headers).patch(LS, resource, id)

proc delete(resource, id: string): LSResponse =
  return newSimpleLSRequest(HttpPatch, resource, id).delete(LS, resource, id)

proc head(resource, id: string): LSResponse =
  return newSimpleLSRequest(HttpHead, resource, id).head(LS, resource, id)

proc registerStoreApi(LS: LiteStore, ctx: DTContext, origResource, origId: string) =
  var api_idx = ctx.duk_push_object()
  # GET
  var get: DTCFunction = (proc (ctx: DTContext): cint{.stdcall.} =
    let resource = duk_get_string(ctx, 0)
    let id = duk_get_string(ctx, 1)
    let params = duk_get_string(ctx, 2)
    let resp = get($resource, $id, $params)
    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, get, 3)
  discard ctx.duk_put_prop_string(api_idx, "get")
  # POST
  var post: DTCFunction = (proc (ctx: DTContext): cint{.stdcall.} =
    let resource = duk_get_string(ctx, 0)
    let folder = duk_get_string(ctx, 1)
    let body = duk_get_string(ctx, 2)
    let ct = duk_get_string(ctx, 3)
    let resp = post($resource, $folder, $body, $ct)
    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, post, 4)
  discard ctx.duk_put_prop_string(api_idx, "post")
  # PUT
  var put: DTCFunction = (proc (ctx: DTContext): cint{.stdcall.} =
    let resource = duk_get_string(ctx, 0)
    let id = duk_get_string(ctx, 1)
    let body = duk_get_string(ctx, 2)
    let ct = duk_get_string(ctx, 3)
    let resp = put($resource, $id, $body, $ct)
    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, put, 4)
  discard ctx.duk_put_prop_string(api_idx, "put")
  # PATCH
  var patch: DTCFunction = (proc (ctx: DTContext): cint{.stdcall.} =
    let resource = duk_get_string(ctx, 0)
    let id = duk_get_string(ctx, 1)
    let body = duk_get_string(ctx, 2)
    let resp = patch($resource, $id, $body)
    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, patch, 3)
  discard ctx.duk_put_prop_string(api_idx, "patch")
  # DELETE
  var delete: DTCFunction = (proc (ctx: DTContext): cint{.stdcall.} =
    let resource = duk_get_string(ctx, 0)
    let id = duk_get_string(ctx, 1)
    let resp = delete($resource, $id)
    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, delete, 2)
  discard ctx.duk_put_prop_string(api_idx, "delete")
  # HEAD
  var head: DTCFunction = (proc (ctx: DTContext): cint{.stdcall.} =
    let resource = duk_get_string(ctx, 0)
    let id = duk_get_string(ctx, 1)
    let resp = head($resource, $id)
    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, head, 2)
  discard ctx.duk_put_prop_string(api_idx, "head")
  discard ctx.duk_put_global_string("$store")

proc jError(ctx: DTContext): LSResponse  =
  return resError(Http500, "Middleware Error: " & $ctx.duk_safe_to_string(-1))

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)
    result = doc.data
  else:
    result = LS.middleware[id]

proc getMiddlewareSeq(resource, id, meth: string): seq[string] =
  result = newSeq[string]() 
  if LS.config.kind != JObject or not LS.config.hasKey("resources"):
    return 
  var reqUri = "/" & resource & "/" & id
  if reqUri[^1] == '/':
    reqUri.removeSuffix({'/'})
  let parts = reqUri.split("/")
  let ancestors = parts[1..parts.len-2]
  var currentPath = "/"
  var currentPaths = ""
  for p in ancestors:
    currentPath &= p
    currentPaths = currentPath & "/*"
    if LS.config["resources"].hasKey(currentPaths) and LS.config["resources"][currentPaths].hasKey(meth) and LS.config["resources"][currentPaths][meth].hasKey("middleware"):
      let mw = LS.config["resources"][currentPaths][meth]["middleware"]
      if (mw.kind == JArray):
        for m in mw:
          result.add m.getStr
  if LS.config["resources"].hasKey(reqUri) and LS.config["resources"][reqUri].hasKey(meth) and LS.config["resources"][reqUri][meth].hasKey("middleware"):
    let mw = LS.config["resources"][reqUri][meth]["middleware"]
    if (mw.kind == JArray):
      for m in mw:
        result.add m.getStr

proc execute*(req: var LSRequest, LS: LiteStore, resource, id: string): LSResponse =
  let middleware = getMiddlewareSeq(resource, id, $req.reqMethod)
  var jReq = $(%* req)
  echo jReq
  var jRes = """{
    "code": 200,
    "content": "",
    "final": false,
    "headers": {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Headers": "Authorization, Content-Type",
      "Server": "$1",
      "Content-Type": "application/json"
    }
  }""" % [LS.appname & "/" & LS.appversion]
  var context = "{}"
  # Create execution context
  var ctx = duk_create_heap_default()
  duk_console_init(ctx)
  duk_print_alert_init(ctx)
  LS.registerStoreApi(ctx, resource, id)
  if ctx.duk_peval_string("JSON.parse('$1');" % jReq) != 0:
    return jError(ctx)
  discard ctx.duk_put_global_string("$req")
  if ctx.duk_peval_string("JSON.parse('$1');" % jRes.replace("\n", "")) != 0:
    return jError(ctx)
  discard ctx.duk_put_global_string("$res")
  if ctx.duk_peval_string("JSON.parse('$1');" % context) != 0:
    return jError(ctx)
  discard ctx.duk_put_global_string("$ctx")
  # Middleware-specific functions
  let fNext: DTCFunction = (proc (ctx: DTContext): cint{.stdcall.} =
    return ctx.duk_peval_string("__next__  = true;")
  )
  discard duk_push_c_function(ctx, fNext, 0)
  discard ctx.duk_put_global_string("$next")
  let fAbort: DTCFunction = (proc (ctx: DTContext): cint{.stdcall.} =
    return ctx.duk_peval_string("__abort__  = true;")
  )
  discard duk_push_c_function(ctx, fAbort, 0)
  discard ctx.duk_put_global_string("$abort")
  var i = 0
  ctx.duk_push_boolean(1)
  discard ctx.duk_put_global_string("__next__")
  ctx.duk_push_boolean(0)
  discard ctx.duk_put_global_string("__abort__")
  var next = 1
  var abort = 0
  while abort != 1 and i < middleware.len:
    if ctx.duk_peval_string("__next__") != 0:
      return jError(ctx) 
    next = ctx.duk_get_boolean(-1)
    if ctx.duk_peval_string("__abort__") != 0:
      return jError(ctx) 
    abort = ctx.duk_get_boolean(-1)
    if next != 1:
      return resError(Http500, "Middleware does not explicitly call $next().")
    let code = LS.getMiddleware(middleware[i])
    echo "evaluating code"
    if ctx.duk_peval_string(code) != 0:
      echo "error!"
      return jError(ctx)
    i.inc
  # Retrieve response, and request
  if ctx.duk_peval_string("JSON.stringify($res);") != 0:
    return jError(ctx)
  let fRes = parseJson($(ctx.duk_get_string(-1))).newLSResponse
  if ctx.duk_peval_string("JSON.stringify($req);") != 0:
    return jError(ctx)
  let fReq = parseJson($(ctx.duk_get_string(-1))).newLSRequest()
  ctx.duk_destroy_heap();
  if abort == 1:
    return fRes
  return route(fReq, LS, resource, id)