summary refs log tree commit diff stats
path: root/tests/deps/jester-#head/jester.nim
blob: 013f0d16d1f53c2f539244296c0b5d4e33725c98 (plain) (blame)
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
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
# Copyright (C) 2015 Dominik Picheta
# MIT License - Look at license.txt for details.
import net, strtabs, re, tables, parseutils, os, strutils, uri,
       times, mimetypes, asyncnet, asyncdispatch, macros, md5,
       logging, httpcore, asyncfile, macrocache, json, options,
       strformat

import jester/private/[errorpages, utils]
import jester/[request, patterns]

from cgi import decodeData, decodeUrl, CgiError

export request
export strtabs
export tables
export httpcore
export MultiData
export HttpMethod
export asyncdispatch

export SameSite

when useHttpBeast:
  import httpbeast except Settings, Request
  import options
else:
  import asynchttpserver except Request

type
  MatchProc* = proc (request: Request): Future[ResponseData] {.gcsafe, closure.}
  MatchProcSync* = proc (request: Request): ResponseData{.gcsafe, closure.}

  Matcher = object
    case async: bool
    of false:
      syncProc: MatchProcSync
    of true:
      asyncProc: MatchProc

  ErrorProc* = proc (
    request: Request, error: RouteError
  ): Future[ResponseData] {.gcsafe, closure.}

  Jester* = object
    when not useHttpBeast:
      httpServer*: AsyncHttpServer
    settings: Settings
    matchers: seq[Matcher]
    errorHandlers: seq[ErrorProc]

  MatchType* = enum
    MRegex, MSpecial, MStatic

  RawHeaders* = seq[tuple[key, val: string]]
  ResponseData* = tuple[
    action: CallbackAction,
    code: HttpCode,
    headers: Option[RawHeaders],
    content: string,
    matched: bool
  ]

  CallbackAction* = enum
    TCActionNothing, TCActionSend, TCActionRaw, TCActionPass

  RouteErrorKind* = enum
    RouteException, RouteCode
  RouteError* = object
    case kind*: RouteErrorKind
    of RouteException:
      exc: ref Exception
    of RouteCode:
      data: ResponseData

const jesterVer = "0.4.0"

proc toStr(headers: Option[RawHeaders]): string =
  return $newHttpHeaders(headers.get(@({:})))

proc createHeaders(headers: RawHeaders): string =
  result = ""
  if headers != nil:
    for header in headers:
      let (key, value) = header
      result.add(key & ": " & value & "\c\L")

    result = result[0 .. ^3] # Strip trailing \c\L

proc createResponse(status: HttpCode, headers: RawHeaders): string =
  return "HTTP/1.1 " & $status & "\c\L" & createHeaders(headers) & "\c\L\c\L"

proc unsafeSend(request: Request, content: string) =
  when useHttpBeast:
    request.getNativeReq.unsafeSend(content)
  else:
    # TODO: This may cause issues if we send too fast.
    asyncCheck request.getNativeReq.client.send(content)

proc send(
  request: Request, code: HttpCode, headers: Option[RawHeaders], body: string
) =
  when useHttpBeast:
    let h =
      if headers.isNone: ""
      else: headers.get().createHeaders
    request.getNativeReq.send(code, body, h)
  else:
    # TODO: This may cause issues if we send too fast.
    asyncCheck request.getNativeReq.respond(
      code, body, newHttpHeaders(headers.get(@({:})))
    )

proc statusContent(request: Request, status: HttpCode, content: string,
                   headers: Option[RawHeaders]) =
  try:
    send(request, status, headers, content)
    when not defined(release):
      logging.debug("  $1 $2" % [$status, toStr(headers)])
  except:
    logging.error("Could not send response: $1" % osErrorMsg(osLastError()))

# TODO: Add support for proper Future Streams instead of this weird raw mode.
template enableRawMode* =
  # TODO: Use the effect system to make this implicit?
  result.action = TCActionRaw

proc send*(request: Request, content: string) =
  ## Sends ``content`` immediately to the client socket.
  ##
  ## Routes using this procedure must enable raw mode.
  unsafeSend(request, content)

proc sendHeaders*(request: Request, status: HttpCode,
                  headers: RawHeaders) =
  ## Sends ``status`` and ``headers`` to the client socket immediately.
  ## The user is then able to send the content immediately to the client on
  ## the fly through the use of ``response.client``.
  let headerData = createResponse(status, headers)
  try:
    request.send(headerData)
    logging.debug("  $1 $2" % [$status, $headers])
  except:
    logging.error("Could not send response: $1" % [osErrorMsg(osLastError())])

proc sendHeaders*(request: Request, status: HttpCode) =
  ## Sends ``status`` and ``Content-Type: text/html`` as the headers to the
  ## client socket immediately.
  let headers = @({"Content-Type": "text/html;charset=utf-8"})
  request.sendHeaders(status, headers)

proc sendHeaders*(request: Request) =
  ## Sends ``Http200`` and ``Content-Type: text/html`` as the headers to the
  ## client socket immediately.
  request.sendHeaders(Http200)

proc send*(request: Request, status: HttpCode, headers: RawHeaders,
           content: string) =
  ## Sends out a HTTP response comprising of the ``status``, ``headers`` and
  ## ``content`` specified.
  var headers = headers & @({"Content-Length": $content.len})
  request.sendHeaders(status, headers)
  request.send(content)

# TODO: Cannot capture 'paths: varargs[string]' here.
proc sendStaticIfExists(
  req: Request, paths: seq[string]
): Future[HttpCode] {.async.} =
  result = Http200
  for p in paths:
    if existsFile(p):

      var fp = getFilePermissions(p)
      if not fp.contains(fpOthersRead):
        return Http403

      let fileSize = getFileSize(p)
      let mimetype = req.settings.mimes.getMimetype(p.splitFile.ext[1 .. ^1])
      if fileSize < 10_000_000: # 10 mb
        var file = readFile(p)

        var hashed = getMD5(file)

        # If the user has a cached version of this file and it matches our
        # version, let them use it
        if req.headers.hasKey("If-None-Match") and req.headers["If-None-Match"] == hashed:
          req.statusContent(Http304, "", none[RawHeaders]())
        else:
          req.statusContent(Http200, file, some(@({
                              "Content-Type": mimetype,
                              "ETag": hashed
                            })))
      else:
        let headers = @({
          "Content-Type": mimetype,
          "Content-Length": $fileSize
        })
        req.statusContent(Http200, "", some(headers))

        var fileStream = newFutureStream[string]("sendStaticIfExists")
        var file = openAsync(p, fmRead)
        # Let `readToStream` write file data into fileStream in the
        # background.
        asyncCheck file.readToStream(fileStream)
        # The `writeFromStream` proc will complete once all the data in the
        # `bodyStream` has been written to the file.
        while true:
          let (hasValue, value) = await fileStream.read()
          if hasValue:
            req.unsafeSend(value)
          else:
            break
        file.close()

      return

  # If we get to here then no match could be found.
  return Http404

proc defaultErrorFilter(error: RouteError): ResponseData =
  case error.kind
  of RouteException:
    let e = error.exc
    let traceback = getStackTrace(e)
    var errorMsg = e.msg
    if errorMsg.isNil: errorMsg = "(nil)"

    let error = traceback & errorMsg
    logging.error(error)
    result.headers = some(@({
      "Content-Type": "text/html;charset=utf-8"
    }))
    result.content = routeException(
      error.replace("\n", "<br/>\n"),
      jesterVer
    )
    result.code = Http502
    result.matched = true
    result.action = TCActionSend
  of RouteCode:
    result.headers = some(@({
      "Content-Type": "text/html;charset=utf-8"
    }))
    result.content = error(
      $error.data.code,
      jesterVer
    )
    result.code = error.data.code
    result.matched = true
    result.action = TCActionSend

proc initRouteError(exc: ref Exception): RouteError =
  RouteError(
    kind: RouteException,
    exc: exc
  )

proc initRouteError(data: ResponseData): RouteError =
  RouteError(
    kind: RouteCode,
    data: data
  )

proc dispatchError(
  jes: Jester,
  request: Request,
  error: RouteError
): Future[ResponseData] {.async.} =
  for errorProc in jes.errorHandlers:
    let data = await errorProc(request, error)
    if data.matched:
      return data

  return defaultErrorFilter(error)

proc dispatch(
  self: Jester,
  req: Request
): Future[ResponseData] {.async.} =
  for matcher in self.matchers:
    if matcher.async:
      let data = await matcher.asyncProc(req)
      if data.matched:
        return data
    else:
      let data = matcher.syncProc(req)
      if data.matched:
        return data

proc handleFileRequest(
  jes: Jester, req: Request
): Future[ResponseData] {.async.} =
  # Find static file.
  # TODO: Caching.
  let path = normalizedPath(
    jes.settings.staticDir / cgi.decodeUrl(req.pathInfo)
  )

  # Verify that this isn't outside our static` dir.
  var status = Http400
  if path.splitFile.dir.startsWith(jes.settings.staticDir):
    if existsDir(path):
      status = await sendStaticIfExists(
        req,
        @[path / "index.html", path / "index.htm"]
      )
    else:
      status = await sendStaticIfExists(req, @[path])

    # Http200 means that the data was sent so there is nothing else to do.
    if status == Http200:
      result[0] = TCActionRaw
      when not defined(release):
        logging.debug("  -> $1" % path)
      return

  return (TCActionSend, status, none[seq[(string, string)]](), "", true)

proc handleRequestSlow(
  jes: Jester,
  req: Request,
  respDataFut: Future[ResponseData] | ResponseData,
  dispatchedError: bool
): Future[void] {.async.} =
  var dispatchedError = dispatchedError
  var respData: ResponseData

  # httpReq.send(Http200, "Hello, World!", "")
  try:
    when respDataFut is Future[ResponseData]:
      respData = await respDataFut
    else:
      respData = respDataFut
  except:
    # Handle any errors by showing them in the browser.
    # TODO: Improve the look of this.
    let exc = getCurrentException()
    respData = await dispatchError(jes, req, initRouteError(exc))
    dispatchedError = true

  # TODO: Put this in a custom matcher?
  if not respData.matched:
    respData = await handleFileRequest(jes, req)

  case respData.action
  of TCActionSend:
    if (respData.code.is4xx or respData.code.is5xx) and
        not dispatchedError and respData.content.len == 0:
      respData = await dispatchError(jes, req, initRouteError(respData))

    statusContent(
      req,
      respData.code,
      respData.content,
      respData.headers
    )
  else:
    when not defined(release):
      logging.debug("  $1" % [$respData.action])

  # Cannot close the client socket. AsyncHttpServer may be keeping it alive.

proc handleRequest(jes: Jester, httpReq: NativeRequest): Future[void] =
  var req = initRequest(httpReq, jes.settings)
  try:
    when not defined(release):
      logging.debug("$1 $2" % [$req.reqMethod, req.pathInfo])

    if likely(jes.matchers.len == 1 and not jes.matchers[0].async):
      let respData = jes.matchers[0].syncProc(req)
      if likely(respData.matched):
        statusContent(
          req,
          respData.code,
          respData.content,
          respData.headers
        )
      else:
        return handleRequestSlow(jes, req, respData, false)
    else:
      return handleRequestSlow(jes, req, dispatch(jes, req), false)
  except:
    let exc = getCurrentException()
    let respDataFut = dispatchError(jes, req, initRouteError(exc))
    return handleRequestSlow(jes, req, respDataFut, true)

proc newSettings*(
  port = Port(5000), staticDir = getCurrentDir() / "public",
  appName = "", bindAddr = "", reusePort = false,
  futureErrorHandler: proc (fut: Future[void]) {.closure, gcsafe.} = nil
): Settings =
  result = Settings(
    staticDir: staticDir,
    appName: appName,
    port: port,
    bindAddr: bindAddr,
    reusePort: reusePort,
    futureErrorHandler: futureErrorHandler
  )

proc register*(self: var Jester, matcher: MatchProc) =
  ## Adds the specified matcher procedure to the specified Jester instance.
  self.matchers.add(
    Matcher(
      async: true,
      asyncProc: matcher
    )
  )

proc register*(self: var Jester, matcher: MatchProcSync) =
  ## Adds the specified matcher procedure to the specified Jester instance.
  self.matchers.add(
    Matcher(
      async: false,
      syncProc: matcher
    )
  )

proc register*(self: var Jester, errorHandler: ErrorProc) =
  ## Adds the specified error handler procedure to the specified Jester instance.
  self.errorHandlers.add(errorHandler)

proc initJester*(
  settings: Settings = newSettings()
): Jester =
  result.settings = settings
  result.settings.mimes = newMimetypes()
  result.matchers = @[]
  result.errorHandlers = @[]

proc initJester*(
  matcher: MatchProc,
  settings: Settings = newSettings()
): Jester =
  result = initJester(settings)
  result.register(matcher)

proc initJester*(
  matcher: MatchProcSync, # TODO: Annoying nim bug: `MatchProc | MatchProcSync` doesn't work.
  settings: Settings = newSettings()
): Jester =
  result = initJester(settings)
  result.register(matcher)

proc serve*(
  self: var Jester
) =
  ## Creates a new async http server instance and registers
  ## it with the dispatcher.
  ##
  ## The event loop is executed by this function, so it will block forever.

  # Ensure we have at least one logger enabled, defaulting to console.
  if logging.getHandlers().len == 0:
    addHandler(logging.newConsoleLogger())
    setLogFilter(when defined(release): lvlInfo else: lvlDebug)

  if self.settings.bindAddr.len > 0:
    logging.info("Jester is making jokes at http://$1:$2$3" %
      [
        self.settings.bindAddr, $self.settings.port, self.settings.appName
      ]
    )
  else:
    logging.info("Jester is making jokes at http://0.0.0.0:$1$2" %
                 [$self.settings.port, self.settings.appName])

  var jes = self
  when useHttpBeast:
    run(
      proc (req: httpbeast.Request): Future[void] =
        result = handleRequest(jes, req),
      httpbeast.initSettings(self.settings.port, self.settings.bindAddr)
    )
  else:
    self.httpServer = newAsyncHttpServer(reusePort=self.settings.reusePort)
    let serveFut = self.httpServer.serve(
      self.settings.port,
      proc (req: asynchttpserver.Request): Future[void] {.gcsafe, closure.} =
        result = handleRequest(jes, req),
      self.settings.bindAddr)
    if not self.settings.futureErrorHandler.isNil:
      serveFut.callback = self.settings.futureErrorHandler
    else:
      asyncCheck serveFut
    runForever()

template resp*(code: HttpCode,
               headers: openarray[tuple[key, value: string]],
               content: string): typed =
  ## Sets ``(code, headers, content)`` as the response.
  bind TCActionSend, newHttpHeaders
  result = (TCActionSend, code, headers.newHttpHeaders.some(), content, true)
  break route

template setHeader(headers: var Option[RawHeaders], key, value: string): typed =
  bind isNone
  if isNone(headers):
    headers = some(@({key: value}))
  else:
    block outer:
      # Overwrite key if it exists.
      var h = headers.get()
      for i in 0 ..< h.len:
        if h[i][0] == key:
          h[i][1] = value
          headers = some(h)
          break outer

      # Add key if it doesn't exist.
      headers = some(h & @({key: value}))

template resp*(content: string, contentType = "text/html;charset=utf-8"): typed =
  ## Sets ``content`` as the response; ``Http200`` as the status code
  ## and ``contentType`` as the Content-Type.
  bind TCActionSend, newHttpHeaders, strtabs.`[]=`
  result[0] = TCActionSend
  result[1] = Http200
  setHeader(result[2], "Content-Type", contentType)
  result[3] = content
  # This will be set by our macro, so this is here for those not using it.
  result.matched = true
  break route

template resp*(content: JsonNode): typed =
  ## Serializes ``content`` as the response, sets ``Http200`` as status code
  ## and "application/json" Content-Type.
  resp($content, contentType="application/json")

template resp*(code: HttpCode, content: string,
               contentType = "text/html;charset=utf-8"): typed =
  ## Sets ``content`` as the response; ``code`` as the status code
  ## and ``contentType`` as the Content-Type.
  bind TCActionSend, newHttpHeaders
  result[0] = TCActionSend
  result[1] = code
  setHeader(result[2], "Content-Type", contentType)
  result[3] = content
  result.matched = true
  break route

template resp*(code: HttpCode): typed =
  ## Responds with the specified ``HttpCode``. This ensures that error handlers
  ## are called.
  bind TCActionSend, newHttpHeaders
  result[0] = TCActionSend
  result[1] = code
  result.matched = true
  break route

template redirect*(url: string): typed =
  ## Redirects to ``url``. Returns from this request handler immediately.
  ## Any set response headers are preserved for this request.
  bind TCActionSend, newHttpHeaders
  result[0] = TCActionSend
  result[1] = Http303
  setHeader(result[2], "Location", url)
  result[3] = ""
  result.matched = true
  break route

template pass*(): typed =
  ## Skips this request handler.
  ##
  ## If you want to stop this request from going further use ``halt``.
  result.action = TCActionPass
  break outerRoute

template cond*(condition: bool): typed =
  ## If ``condition`` is ``False`` then ``pass`` will be called,
  ## i.e. this request handler will be skipped.
  if not condition: break outerRoute

template halt*(code: HttpCode,
               headers: openarray[tuple[key, val: string]],
               content: string): typed =
  ## Immediately replies with the specified request. This means any further
  ## code will not be executed after calling this template in the current
  ## route.
  bind TCActionSend, newHttpHeaders
  result[0] = TCActionSend
  result[1] = code
  result[2] = some(@headers)
  result[3] = content
  result.matched = true
  break allRoutes

template halt*(): typed =
  ## Halts the execution of this request immediately. Returns a 404.
  ## All previously set values are **discarded**.
  halt(Http404, {"Content-Type": "text/html;charset=utf-8"}, error($Http404, jesterVer))

template halt*(code: HttpCode): typed =
  halt(code, {"Content-Type": "text/html;charset=utf-8"}, error($code, jesterVer))

template halt*(content: string): typed =
  halt(Http404, {"Content-Type": "text/html;charset=utf-8"}, content)

template halt*(code: HttpCode, content: string): typed =
  halt(code, {"Content-Type": "text/html;charset=utf-8"}, content)

template attachment*(filename = ""): typed =
  ## Instructs the browser that the response should be stored on disk
  ## rather than displayed in the browser.
  var disposition = "attachment"
  if filename != "":
    disposition.add("; filename=\"" & extractFilename(filename) & "\"")
    let ext = splitFile(filename).ext
    let contentTypeSet =
      isSome(result[2]) and result[2].get().toTable.hasKey("Content-Type")
    if not contentTypeSet and ext != "":
      setHeader(result[2], "Content-Type", getMimetype(request.settings.mimes, ext))
  setHeader(result[2], "Content-Disposition", disposition)

template sendFile*(filename: string): typed =
  ## Sends the file at the specified filename as the response.
  result[0] = TCActionRaw
  let sendFut = sendStaticIfExists(request, @[filename])
  yield sendFut
  let status = sendFut.read()
  if status != Http200:
    raise newException(JesterError, "Couldn't send requested file: " & filename)
  # This will be set by our macro, so this is here for those not using it.
  result.matched = true
  break route

template `@`*(s: string): untyped =
  ## Retrieves the parameter ``s`` from ``request.params``. ``""`` will be
  ## returned if parameter doesn't exist.
  if s in params(request):
    # TODO: Why does request.params not work? :(
    # TODO: This is some weird bug with macros/templates, I couldn't
    # TODO: reproduce it easily.
    params(request)[s]
  else:
    ""

proc setStaticDir*(request: Request, dir: string) =
  ## Sets the directory in which Jester will look for static files. It is
  ## ``./public`` by default.
  ##
  ## The files will be served like so:
  ##
  ## ./public/css/style.css ``->`` http://example.com/css/style.css
  ##
  ## (``./public`` is not included in the final URL)
  request.settings.staticDir = dir

proc getStaticDir*(request: Request): string =
  ## Gets the directory in which Jester will look for static files.
  ##
  ## ``./public`` by default.
  return request.settings.staticDir

proc makeUri*(request: Request, address = "", absolute = true,
              addScriptName = true): string =
  ## Creates a URI based on the current request. If ``absolute`` is true it will
  ## add the scheme (Usually 'http://'), `request.host` and `request.port`.
  ## If ``addScriptName`` is true `request.appName` will be prepended before
  ## ``address``.

  # Check if address already starts with scheme://
  var uri = parseUri(address)

  if uri.scheme != "": return address
  uri.path = "/"
  uri.query = ""
  uri.anchor = ""
  if absolute:
    uri.hostname = request.host
    uri.scheme = (if request.secure: "https" else: "http")
    if request.port != (if request.secure: 443 else: 80):
      uri.port = $request.port

  if addScriptName: uri = uri / request.appName
  if address != "":
    uri = uri / address
  else:
    uri = uri / request.pathInfo
  return $uri

template uri*(address = "", absolute = true, addScriptName = true): untyped =
  ## Convenience template which can be used in a route.
  request.makeUri(address, absolute, addScriptName)

proc daysForward*(days: int): DateTime =
  ## Returns a DateTime object referring to the current time plus ``days``.
  return getTime().utc + initInterval(days = days)

template setCookie*(name, value: string, expires="",
                    sameSite: SameSite=Lax): typed =
  ## Creates a cookie which stores ``value`` under ``name``.
  ##
  ## The SameSite argument determines the level of CSRF protection that
  ## you wish to adopt for this cookie. It's set to Lax by default which
  ## should protect you from most vulnerabilities. Note that this is only
  ## supported by some browsers:
  ## https://caniuse.com/#feat=same-site-cookie-attribute
  let newCookie = makeCookie(name, value, expires)
  if isSome(result[2]) and
     (let headers = result[2].get(); headers.toTable.hasKey("Set-Cookie")):
    result[2] = some(headers & @({"Set-Cookie": newCookie}))
  else:
    setHeader(result[2], "Set-Cookie", newCookie)

template setCookie*(name, value: string, expires: DateTime,
                    sameSite: SameSite=Lax): typed =
  ## Creates a cookie which stores ``value`` under ``name``.
  setCookie(name, value, format(expires, "ddd',' dd MMM yyyy HH:mm:ss 'GMT'"))

proc normalizeUri*(uri: string): string =
  ## Remove any trailing ``/``.
  if uri[uri.len-1] == '/': result = uri[0 .. uri.len-2]
  else: result = uri

# -- Macro

proc checkAction*(respData: var ResponseData): bool =
  case respData.action
  of TCActionSend, TCActionRaw:
    result = true
  of TCActionPass:
    result = false
  of TCActionNothing:
    assert(false)

proc skipDo(node: NimNode): NimNode {.compiletime.} =
  if node.kind == nnkDo:
    result = node[6]
  else:
    result = node

proc ctParsePattern(pattern, pathPrefix: string): NimNode {.compiletime.} =
  result = newNimNode(nnkPrefix)
  result.add newIdentNode("@")
  result.add newNimNode(nnkBracket)

  proc addPattNode(res: var NimNode, typ, text,
                   optional: NimNode) {.compiletime.} =
    var objConstr = newNimNode(nnkObjConstr)

    objConstr.add bindSym("Node")
    objConstr.add newNimNode(nnkExprColonExpr).add(
        newIdentNode("typ"), typ)
    objConstr.add newNimNode(nnkExprColonExpr).add(
        newIdentNode("text"), text)
    objConstr.add newNimNode(nnkExprColonExpr).add(
        newIdentNode("optional"), optional)

    res[1].add objConstr

  var patt = parsePattern(pattern)
  if pathPrefix.len > 0:
    result.addPattNode(
      bindSym("NodeText"), # Node kind
      newStrLitNode(pathPrefix), # Text
      newIdentNode("false") # Optional?
    )

  for node in patt:
    result.addPattNode(
      case node.typ
      of NodeText: bindSym("NodeText")
      of NodeField: bindSym("NodeField"),
      newStrLitNode(node.text),
      newIdentNode(if node.optional: "true" else: "false"))

template setDefaultResp*(): typed =
  # TODO: bindSym this in the 'routes' macro and put it in each route
  bind TCActionNothing, newHttpHeaders
  result.action = TCActionNothing
  result.code = Http200
  result.content = ""

template declareSettings(): typed {.dirty.} =
  when not declaredInScope(settings):
    var settings = newSettings()

proc createJesterPattern(
  routeNode, patternMatchSym: NimNode,
  pathPrefix: string
): NimNode {.compileTime.} =
  var ctPattern = ctParsePattern(routeNode[1].strVal, pathPrefix)
  # -> let <patternMatchSym> = <ctPattern>.match(request.path)
  return newLetStmt(patternMatchSym,
      newCall(bindSym"match", ctPattern, parseExpr("request.pathInfo")))

proc escapeRegex(s: string): string =
  result = ""
  for i in s:
    case i
    # https://stackoverflow.com/a/400316/492186
    of '.', '^', '$', '*', '+', '?', '(', ')', '[', '{', '\\', '|':
      result.add('\\')
      result.add(i)
    else:
      result.add(i)

proc createRegexPattern(
  routeNode, reMatchesSym, patternMatchSym: NimNode,
  pathPrefix: string
): NimNode {.compileTime.} =
  # -> let <patternMatchSym> = find(request.pathInfo, <pattern>, <reMatches>)
  var strNode = routeNode[1].copyNimTree()
  strNode[1].strVal = escapeRegex(pathPrefix) & strNode[1].strVal
  return newLetStmt(
    patternMatchSym,
    newCall(
      bindSym"find",
      parseExpr("request.pathInfo"),
      strNode,
      reMatchesSym
    )
  )

proc determinePatternType(pattern: NimNode): MatchType {.compileTime.} =
  case pattern.kind
  of nnkStrLit:
    var patt = parsePattern(pattern.strVal)
    if patt.len == 1 and patt[0].typ == NodeText:
      return MStatic
    else:
      return MSpecial
  of nnkCallStrLit:
    expectKind(pattern[0], nnkIdent)
    case ($pattern[0]).normalize
    of "re": return MRegex
    else:
      macros.error("Invalid pattern type: " & $pattern[0])
  else:
    macros.error("Unexpected node kind: " & $pattern.kind)

proc createCheckActionIf(): NimNode =
  var checkActionIf = parseExpr(
    "if checkAction(result): result.matched = true; break routesList"
  )
  checkActionIf[0][0][0] = bindSym"checkAction"
  return checkActionIf

proc createGlobalMetaRoute(routeNode, dest: NimNode) {.compileTime.} =
  ## Creates a ``before`` or ``after`` route with no pattern, i.e. one which
  ## will be always executed.

  # -> block route: <ifStmtBody>
  var innerBlockStmt = newStmtList(
    newNimNode(nnkBlockStmt).add(newIdentNode("route"), routeNode[1].skipDo())
  )

  # -> block outerRoute: <innerBlockStmt>
  var blockStmt = newNimNode(nnkBlockStmt).add(
    newIdentNode("outerRoute"), innerBlockStmt)
  dest.add blockStmt

proc createRoute(
  routeNode, dest: NimNode, pathPrefix: string, isMetaRoute: bool = false
) {.compileTime.} =
  ## Creates code which checks whether the current request path
  ## matches a route.
  ##
  ## The `isMetaRoute` parameter determines whether the route to be created is
  ## one of either a ``before`` or an ``after`` route.

  var patternMatchSym = genSym(nskLet, "patternMatchRet")

  # Only used for Regex patterns.
  var reMatchesSym = genSym(nskVar, "reMatches")
  var reMatches = parseExpr("var reMatches: array[20, string]")
  reMatches[0][0] = reMatchesSym
  reMatches[0][1][1] = bindSym("MaxSubpatterns")

  let patternType = determinePatternType(routeNode[1])
  case patternType
  of MStatic:
    discard
  of MSpecial:
    dest.add createJesterPattern(routeNode, patternMatchSym, pathPrefix)
  of MRegex:
    dest.add reMatches
    dest.add createRegexPattern(
      routeNode, reMatchesSym, patternMatchSym, pathPrefix
    )

  var ifStmtBody = newStmtList()
  case patternType
  of MStatic: discard
  of MSpecial:
    # -> setPatternParams(request, ret.params)
    ifStmtBody.add newCall(bindSym"setPatternParams", newIdentNode"request",
                           newDotExpr(patternMatchSym, newIdentNode"params"))
  of MRegex:
    # -> setReMatches(request, <reMatchesSym>)
    ifStmtBody.add newCall(bindSym"setReMatches", newIdentNode"request",
                           reMatchesSym)

  ifStmtBody.add routeNode[2].skipDo()

  let checkActionIf =
    if isMetaRoute:
      parseExpr("break routesList")
    else:
      createCheckActionIf()
  # -> block route: <ifStmtBody>; <checkActionIf>
  var innerBlockStmt = newStmtList(
    newNimNode(nnkBlockStmt).add(newIdentNode("route"), ifStmtBody),
    checkActionIf
  )

  let ifCond =
    case patternType
    of MStatic:
      infix(
        parseExpr("request.pathInfo"),
        "==",
        newStrLitNode(pathPrefix & routeNode[1].strVal)
      )
    of MSpecial:
      newDotExpr(patternMatchSym, newIdentNode("matched"))
    of MRegex:
      infix(patternMatchSym, "!=", newIntLitNode(-1))

  # -> if <patternMatchSym>.matched: <innerBlockStmt>
  var ifStmt = newIfStmt((ifCond, innerBlockStmt))

  # -> block outerRoute: <ifStmt>
  var blockStmt = newNimNode(nnkBlockStmt).add(
    newIdentNode("outerRoute"), ifStmt)
  dest.add blockStmt

proc createError(
  errorNode: NimNode,
  httpCodeBranches,
  exceptionBranches: var seq[tuple[cond, body: NimNode]]
) =
  if errorNode.len != 3:
    error("Missing error condition or body.", errorNode)

  let routeIdent = newIdentNode("route")
  let outerRouteIdent = newIdentNode("outerRoute")
  let checkActionIf = createCheckActionIf()
  let exceptionIdent = newIdentNode("exception")
  let errorIdent = newIdentNode("error") # TODO: Ugh. I shouldn't need these...
  let errorCond = errorNode[1]
  let errorBody = errorNode[2]
  let body = quote do:
    block `outerRouteIdent`:
      block `routeIdent`:
        `errorBody`
      `checkActionIf`

  case errorCond.kind
  of nnkIdent:
    let name = errorCond.strVal
    if name.len == 7 and name.startsWith("Http"):
      # HttpCode.
      httpCodeBranches.add(
        (
          infix(parseExpr("error.data.code"), "==", errorCond),
          body
        )
      )
    else:
      # Exception
      exceptionBranches.add(
        (
          infix(parseExpr("error.exc"), "of", errorCond),
          quote do:
            let `exceptionIdent` = (ref `errorCond`)(`errorIdent`.exc)
            `body`
        )
      )
  of nnkCurly:
    expectKind(errorCond[0], nnkInfix)
    httpCodeBranches.add(
      (
        infix(parseExpr("error.data.code"), "in", errorCond),
        body
      )
    )
  else:
    error("Expected exception type or set[HttpCode].", errorCond)

const definedRoutes = CacheTable"jester.routes"

proc processRoutesBody(
  body: NimNode,
  # For HTTP methods.
  caseStmtGetBody,
  caseStmtPostBody,
  caseStmtPutBody,
  caseStmtDeleteBody,
  caseStmtHeadBody,
  caseStmtOptionsBody,
  caseStmtTraceBody,
  caseStmtConnectBody,
  caseStmtPatchBody: var NimNode,
  # For `error`.
  httpCodeBranches,
  exceptionBranches: var seq[tuple[cond, body: NimNode]],
  # For before/after stmts.
  beforeStmts,
  afterStmts: var NimNode,
  # For other statements.
  outsideStmts: var NimNode,
  pathPrefix: string
) =
  for i in 0..<body.len:
    case body[i].kind
    of nnkCall:
      let cmdName = body[i][0].`$`.normalize
      case cmdName
      of "before":
        createGlobalMetaRoute(body[i], beforeStmts)
      of "after":
        createGlobalMetaRoute(body[i], afterStmts)
      else:
        outsideStmts.add(body[i])
    of nnkCommand:
      let cmdName = body[i][0].`$`.normalize
      case cmdName
      # HTTP Methods
      of "get":
        createRoute(body[i], caseStmtGetBody, pathPrefix)
      of "post":
        createRoute(body[i], caseStmtPostBody, pathPrefix)
      of "put":
        createRoute(body[i], caseStmtPutBody, pathPrefix)
      of "delete":
        createRoute(body[i], caseStmtDeleteBody, pathPrefix)
      of "head":
        createRoute(body[i], caseStmtHeadBody, pathPrefix)
      of "options":
        createRoute(body[i], caseStmtOptionsBody, pathPrefix)
      of "trace":
        createRoute(body[i], caseStmtTraceBody, pathPrefix)
      of "connect":
        createRoute(body[i], caseStmtConnectBody, pathPrefix)
      of "patch":
        createRoute(body[i], caseStmtPatchBody, pathPrefix)
      # Other
      of "error":
        createError(body[i], httpCodeBranches, exceptionBranches)
      of "before":
        createRoute(body[i], beforeStmts, pathPrefix, isMetaRoute=true)
      of "after":
        createRoute(body[i], afterStmts, pathPrefix, isMetaRoute=true)
      of "extend":
        # Extend another router.
        let extend = body[i]
        if extend[1].kind != nnkIdent:
          error("Expected identifier.", extend[1])

        let prefix =
          if extend.len > 1:
            extend[2].strVal
          else:
            ""
        if prefix.len != 0 and prefix[0] != '/':
          error("Path prefix for extended route must start with '/'", extend[2])

        processRoutesBody(
          definedRoutes[extend[1].strVal],
          caseStmtGetBody,
          caseStmtPostBody,
          caseStmtPutBody,
          caseStmtDeleteBody,
          caseStmtHeadBody,
          caseStmtOptionsBody,
          caseStmtTraceBody,
          caseStmtConnectBody,
          caseStmtPatchBody,
          httpCodeBranches,
          exceptionBranches,
          beforeStmts,
          afterStmts,
          outsideStmts,
          pathPrefix & prefix
        )
      else:
        outsideStmts.add(body[i])
    of nnkCommentStmt:
      discard
    of nnkPragma:
      if body[i][0].strVal.normalize notin ["async", "sync"]:
        outsideStmts.add(body[i])
    else:
      outsideStmts.add(body[i])

type
  NeedsAsync = enum
    ImplicitTrue, ImplicitFalse, ExplicitTrue, ExplicitFalse
proc needsAsync(node: NimNode): NeedsAsync =
  result = ImplicitFalse
  case node.kind
  of nnkCommand, nnkCall:
    if node[0].kind == nnkIdent:
      case node[0].strVal.normalize
      of "await", "sendfile":
        return ImplicitTrue
      of "resp", "halt", "attachment", "pass", "redirect", "cond", "get",
         "post", "patch", "delete":
        # This is just a simple heuristic. It's by no means meant to be
        # exhaustive.
        discard
      else:
        return ImplicitTrue
  of nnkYieldStmt:
    return ImplicitTrue
  of nnkPragma:
    if node[0].kind == nnkIdent:
      case node[0].strVal.normalize
      of "sync":
        return ExplicitFalse
      of "async":
        return ExplicitTrue
      else: discard
  else: discard

  for c in node:
    let r = needsAsync(c)
    if r in {ImplicitTrue, ExplicitTrue, ExplicitFalse}: return r

proc routesEx(name: string, body: NimNode): NimNode =
  # echo(treeRepr(body))
  # echo(treeRepr(name))

  # Save this route's body so that it can be incorporated into another route.
  definedRoutes[name] = body.copyNimTree

  result = newStmtList()

  # -> declareSettings()
  result.add newCall(bindSym"declareSettings")

  var outsideStmts = newStmtList()

  var matchBody = newNimNode(nnkStmtList)
  let setDefaultRespIdent = bindSym"setDefaultResp"
  matchBody.add newCall(setDefaultRespIdent)
  # TODO: This diminishes the performance. Would be nice to only include it
  # TODO: when setPatternParams or setReMatches is used.
  matchBody.add parseExpr("var request = request")

  # HTTP router case statement nodes:
  var caseStmt = newNimNode(nnkCaseStmt)
  caseStmt.add parseExpr("request.reqMethod")

  var caseStmtGetBody = newNimNode(nnkStmtList)
  var caseStmtPostBody = newNimNode(nnkStmtList)
  var caseStmtPutBody = newNimNode(nnkStmtList)
  var caseStmtDeleteBody = newNimNode(nnkStmtList)
  var caseStmtHeadBody = newNimNode(nnkStmtList)
  var caseStmtOptionsBody = newNimNode(nnkStmtList)
  var caseStmtTraceBody = newNimNode(nnkStmtList)
  var caseStmtConnectBody = newNimNode(nnkStmtList)
  var caseStmtPatchBody = newNimNode(nnkStmtList)

  # Error handler nodes:
  var httpCodeBranches: seq[tuple[cond, body: NimNode]] = @[]
  var exceptionBranches: seq[tuple[cond, body: NimNode]] = @[]

  # Before/After nodes:
  var beforeRoutes = newStmtList()
  var afterRoutes = newStmtList()

  processRoutesBody(
    body,
    caseStmtGetBody,
    caseStmtPostBody,
    caseStmtPutBody,
    caseStmtDeleteBody,
    caseStmtHeadBody,
    caseStmtOptionsBody,
    caseStmtTraceBody,
    caseStmtConnectBody,
    caseStmtPatchBody,
    httpCodeBranches,
    exceptionBranches,
    beforeRoutes,
    afterRoutes,
    outsideStmts,
    ""
  )

  var ofBranchGet = newNimNode(nnkOfBranch)
  ofBranchGet.add newIdentNode("HttpGet")
  ofBranchGet.add caseStmtGetBody
  caseStmt.add ofBranchGet

  var ofBranchPost = newNimNode(nnkOfBranch)
  ofBranchPost.add newIdentNode("HttpPost")
  ofBranchPost.add caseStmtPostBody
  caseStmt.add ofBranchPost

  var ofBranchPut = newNimNode(nnkOfBranch)
  ofBranchPut.add newIdentNode("HttpPut")
  ofBranchPut.add caseStmtPutBody
  caseStmt.add ofBranchPut

  var ofBranchDelete = newNimNode(nnkOfBranch)
  ofBranchDelete.add newIdentNode("HttpDelete")
  ofBranchDelete.add caseStmtDeleteBody
  caseStmt.add ofBranchDelete

  var ofBranchHead = newNimNode(nnkOfBranch)
  ofBranchHead.add newIdentNode("HttpHead")
  ofBranchHead.add caseStmtHeadBody
  caseStmt.add ofBranchHead

  var ofBranchOptions = newNimNode(nnkOfBranch)
  ofBranchOptions.add newIdentNode("HttpOptions")
  ofBranchOptions.add caseStmtOptionsBody
  caseStmt.add ofBranchOptions

  var ofBranchTrace = newNimNode(nnkOfBranch)
  ofBranchTrace.add newIdentNode("HttpTrace")
  ofBranchTrace.add caseStmtTraceBody
  caseStmt.add ofBranchTrace

  var ofBranchConnect = newNimNode(nnkOfBranch)
  ofBranchConnect.add newIdentNode("HttpConnect")
  ofBranchConnect.add caseStmtConnectBody
  caseStmt.add ofBranchConnect

  var ofBranchPatch = newNimNode(nnkOfBranch)
  ofBranchPatch.add newIdentNode("HttpPatch")
  ofBranchPatch.add caseStmtPatchBody
  caseStmt.add ofBranchPatch

  # Wrap the routes inside ``routesList`` blocks accordingly, and add them to
  # the `match` procedure body.
  let routesListIdent = newIdentNode("routesList")
  matchBody.add(
    quote do:
      block `routesListIdent`:
        `beforeRoutes`
  )

  matchBody.add(
    quote do:
      block `routesListIdent`:
        `caseStmt`
  )

  matchBody.add(
    quote do:
      block `routesListIdent`:
        `afterRoutes`
  )

  let matchIdent = newIdentNode(name)
  let reqIdent = newIdentNode("request")
  let needsAsync = needsAsync(body)
  case needsAsync
  of ImplicitFalse, ExplicitFalse:
    hint(fmt"Synchronous route `{name}` has been optimised. Use `{{.async.}}` to change.")
  of ImplicitTrue, ExplicitTrue:
    hint(fmt"Asynchronous route: {name}.")
  var matchProc =
    if needsAsync in {ImplicitTrue, ExplicitTrue}:
      quote do:
        proc `matchIdent`(
          `reqIdent`: Request
        ): Future[ResponseData] {.async, gcsafe.} =
          discard
    else:
      quote do:
        proc `matchIdent`(
          `reqIdent`: Request
        ): ResponseData {.gcsafe.} =
          discard

  # The following `block` is for `halt`. (`return` didn't work :/)
  let allRoutesBlock = newTree(
    nnkBlockStmt,
    newIdentNode("allRoutes"),
    matchBody
  )
  matchProc[6] = newTree(nnkStmtList, allRoutesBlock)
  result.add(outsideStmts)
  result.add(matchProc)

  # Error handler proc
  let errorHandlerIdent = newIdentNode(name & "ErrorHandler")
  let errorIdent = newIdentNode("error")
  let exceptionIdent = newIdentNode("exception")
  let resultIdent = newIdentNode("result")
  var errorHandlerProc = quote do:
    proc `errorHandlerIdent`(
      `reqIdent`: Request, `errorIdent`: RouteError
    ): Future[ResponseData] {.gcsafe, async.} =
      block `routesListIdent`:
        `setDefaultRespIdent`()
        case `errorIdent`.kind
        of RouteException:
          discard
        of RouteCode:
          discard
  if exceptionBranches.len != 0:
    var stmts = newStmtList()
    for branch in exceptionBranches:
      stmts.add(newIfStmt(branch))
    errorHandlerProc[6][0][1][^1][1][1][0] = stmts
  if httpCodeBranches.len > 1:
    var stmts = newStmtList()
    for branch in httpCodeBranches:
      stmts.add(newIfStmt(branch))
    errorHandlerProc[6][0][1][^1][2][1][0] = stmts
  result.add(errorHandlerProc)

  # TODO: Replace `body`, `headers`, `code` in routes with `result[i]` to
  # get these shortcuts back without sacrificing usability.
  # TODO2: Make sure you replace what `guessAction` used to do for this.

  # echo toStrLit(result)
  # echo treeRepr(result)

macro routes*(body: untyped): typed =
  result = routesEx("match", body)
  let jesIdent = genSym(nskVar, "jes")
  let matchIdent = newIdentNode("match")
  let errorHandlerIdent = newIdentNode("matchErrorHandler")
  let settingsIdent = newIdentNode("settings")
  result.add(
    quote do:
      var `jesIdent` = initJester(`matchIdent`, `settingsIdent`)
      `jesIdent`.register(`errorHandlerIdent`)
  )
  result.add(
    quote do:
      serve(`jesIdent`)
  )

macro router*(name: untyped, body: untyped): typed =
  if name.kind != nnkIdent:
    error("Need an ident.", name)

  routesEx($name.ident, body)

macro settings*(body: untyped): typed =
  #echo(treeRepr(body))
  expectKind(body, nnkStmtList)

  result = newStmtList()

  # var settings = newSettings()
  let settingsIdent = newIdentNode("settings")
  result.add newVarStmt(settingsIdent, newCall("newSettings"))

  for asgn in body.children:
    expectKind(asgn, nnkAsgn)
    result.add newAssignment(newDotExpr(settingsIdent, asgn[0]), asgn[1])