2019-12-22 00:45:07 +00:00
|
|
|
import strutils, sequtils, asyncdispatch, httpclient
|
2020-05-08 00:48:47 +00:00
|
|
|
from jester import Request
|
2019-09-20 23:08:30 +00:00
|
|
|
import ../utils, ../prefs
|
|
|
|
export utils, prefs
|
2019-09-20 13:03:18 +00:00
|
|
|
|
2020-05-08 00:48:47 +00:00
|
|
|
template savePref*(pref, value: string; req: Request; expire=false): typed =
|
|
|
|
if not expire or pref in cookies(req):
|
|
|
|
setCookie(pref, value, daysForward(when expire: -10 else: 360),
|
|
|
|
httpOnly=true, secure=cfg.useHttps)
|
|
|
|
|
2019-09-06 00:42:35 +00:00
|
|
|
template cookiePrefs*(): untyped {.dirty.} =
|
2020-05-08 00:48:47 +00:00
|
|
|
getPrefs(cookies(request))
|
2019-09-06 00:42:35 +00:00
|
|
|
|
|
|
|
template getPath*(): untyped {.dirty.} =
|
|
|
|
$(parseUri(request.path) ? filterParams(request.params))
|
|
|
|
|
|
|
|
template refPath*(): untyped {.dirty.} =
|
|
|
|
if @"referer".len > 0: @"referer" else: "/"
|
2019-12-04 04:58:18 +00:00
|
|
|
|
|
|
|
proc getNames*(name: string): seq[string] =
|
|
|
|
name.strip(chars={'/'}).split(",").filterIt(it.len > 0)
|
2019-12-22 00:45:07 +00:00
|
|
|
|
2020-01-01 16:24:24 +00:00
|
|
|
proc safeClose*(client: AsyncHttpClient) =
|
|
|
|
try: client.close()
|
2020-03-08 23:17:13 +00:00
|
|
|
except: discard
|
2020-01-01 16:24:24 +00:00
|
|
|
|
2019-12-22 00:45:07 +00:00
|
|
|
proc safeFetch*(url: string): Future[string] {.async.} =
|
|
|
|
let client = newAsyncHttpClient()
|
|
|
|
try: result = await client.getContent(url)
|
|
|
|
except: discard
|
2020-03-08 23:17:13 +00:00
|
|
|
finally: client.safeClose()
|