forked from itsumura-h/nim-basolato
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.nim
106 lines (91 loc) · 2.99 KB
/
response.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
import httpcore, json, options, os, times, strutils
# framework
import ./base, ./baseEnv, ./header, ./security, ./logger
# 3rd party
import httpbeast
from ./core/core import RawHeaders, CallbackAction, ResponseData
import ./core/core/request
template setHeader(headers: var Option[RawHeaders], key, value: string) =
## Customized jester code
bind isNone
if isNone(headers):
headers = some(@({key: value}))
else:
let h = headers.get()
headers = some(h & @({key: value}))
template resp*(code: HttpCode,
headers: openarray[tuple[key, val: string]],
content: string) =
## Set ``(code, headers, content)`` as the response.
bind TCActionSend
result = (TCActionSend, code, none[RawHeaders](), content, true)
for header in headers:
setHeader(result[2], header[0], header[1])
break route
# ========== Header ====================
proc setHeader*(response:Response, headers:Headers):Response =
for header in headers:
var index = 0
var tmpValue = ""
for i, row in response.headers:
if row.key == header.key:
index = i
tmpValue = row.val
break
if tmpValue.len == 0:
response.headers.add((header.key, header.val))
else:
response.headers[index] = (header.key, tmpValue & ", " & header.val)
return response
# ========== Cookie ====================
proc setCookie*(response:Response, cookie:Cookie):Response =
for cookieData in cookie.cookies:
let cookieStr = cookieData.toCookieStr()
response.headers.add(("Set-cookie", cookieStr))
return response
# ========== Auth ====================
proc setAuth*(response:Response, auth:Auth):Response =
let sessionId = auth.getToken()
let cookie = if SESSION_TIME.len > 0:
newCookieData(
"session_id",
sessionId,
timeForward(SESSION_TIME.parseInt, Minutes)
)
.toCookieStr()
else:
newCookieData("session_id", sessionId).toCookieStr()
response.headers.add(("Set-cookie", cookie))
return response
proc destroyAuth*(response:Response, auth:Auth):Response =
if auth.isLogin:
let sessionId = auth.getToken()
let cookie = newCookieData("session_id", sessionId, timeForward(-1, Days))
.toCookieStr()
response.headers.add(("Set-cookie", cookie))
auth.destroy()
else:
echoErrorMsg("Tried to destroy auth but not logged in")
return response
# =============================================================================
proc response*(arg:ResponseData):Response =
if not arg[4]: raise newException(Error404, "")
return Response(
status: arg[1],
headers: arg[2].get,
body: arg[3],
bodyString: arg[3],
match: arg[4]
)
proc response*(status:HttpCode, body:string): Response =
return Response(
status:status,
bodyString: body,
responseType: String
)
proc html*(r_path:string):string =
## arg r_path is relative path from /resources/
let path = getCurrentDir() & "/resources/" & r_path
let f = open(path, fmRead)
result = $(f.readAll)
defer: f.close()