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
import json, asyncdispatch, asynchttpserver, options, strutils, httpcore
import nimhafas
import types, translations
import moustachu, moustachu_context
import assets
proc getPoint* (name: string): Future[Point] {.async} =
let points = (await suggestions(SuggestionsParams(
query: name,
results: some(1),
addresses: some(false),
poi: some(false)
)))
if points.len > 0:
result = points[0]
else:
raise newException(notFoundException, "Zur Eingabe: \"" & name & "\" konnte nichts gefunden werden.")
proc addTranslations* (old: JsonNode, new: JsonNode): JsonNode =
for key, value in pairs(new):
old.add("LABEL_" & key, value)
return old
template parseProduct* (data: untyped, form: untyped, name: string, new_name: string="") =
if new_name != "":
if form.hasKey(name) != true:
data.add(new_name, %false)
else:
data.add(new_name, %true)
else:
if form.hasKey(name) != true:
data.add(name, %false)
else:
data.add(name, %true)
template languageHandler* () =
let cookies = cookies(request)
if cookies.hasKey("lang") and availableTranslations.contains(cookies["lang"]):
lang = cookies["lang"]
if requestParams.hasKey("lang") and availableTranslations.contains(requestParams["lang"]):
lang = requestParams["lang"]
setCookie("lang", lang, daysForward(90))
proc respError* (request: Request, httpCode: HttpCode, header: string, body: string) {.async.} =
let response = render(readFile("templates/error.tpl"), %* {
"HEAD": header,
"BODY": body
})
await request.respond(httpCode, response)
proc respTemplate* (request: Request, httpCode: HttpCode, name: string, templateContext: JsonNode, lang: string="", headers: HttpHeaders=newHttpHeaders()) {.async.} =
var templateContext = templateContext
if lang != "":
templateContext = templateContext.addTranslations(translations["global"][lang])
templateContext = templateContext.addTranslations(translations[name][lang])
var response: string
if name == "search":
response = render(templateSearch , templateContext)
elif name == "journeys":
response = render(templateJourneys, templateContext)
elif name == "journey":
response = render(templateJourney, templateContext)
else:
response = render(templateError, templateContext)
await request.respond(httpCode, response, newHttpHeaders([("Content-Type", "text/html; charset=UTF-8")]))