-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.nim
63 lines (53 loc) · 1.43 KB
/
main.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
import strformat
import strutils
import re
import os
import jester
import json
import asyncnet
import asyncdispatch
import nwt
var
tvTemplates {.threadvar.}: Nwt
proc getTemplates(): Nwt =
if tvTemplates.isNil: # If thread local `tvTemplates` is Nil,
tvTemplates = newNwt("templates/*.html") # initialize for the current thread,
return tvTemplates # and finally return it.
settings:
port = 8000.Port
staticDir = "static"
appName = ""
bindAddr = "0.0.0.0"
reusePort = false
routes:
get "/":
let templates = getTemplates() # Retrieve the thread local `tvTemplates`,
resp templates.renderTemplate("index.html") # and use it.
get re"^\/(.*)\.(?:html|txt|css|js|min\.js|jpg|png|bmp|svg|gif)$":
if "templates" notin request.path:
sendFile(request.path.strip(chars={'/'}, leading=true))
get "/explicitJSON":
const data = $(%*{"message": "Hello, World!"})
resp data, "application/json"
get "/explicitJsonFromSeq":
let test = @[
%*{
"message": "Hello, World!"
},
%*{
"message2": @[
%*{
"nested": "works",
}
]
},
]
resp ($test).strip(chars={'@'}, leading=true), "application/json"
get "/implicitJSON":
resp %*{
"string": "string",
"number": 1,
"float": 1.33
}
# get re"^\/(.*)\.txt$":
# resp request.matches[0]