akane.nim 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. # author: Ethosa
  2. # ----- CORE ----- #
  3. import asyncdispatch
  4. import asynchttpserver
  5. # ----- SUPPORT ----- #
  6. import asyncfile # loadtemplate
  7. import strutils # startsWith, endsWith
  8. import tables
  9. import macros
  10. import times
  11. import json # urlParams
  12. import uri # decodeUrl
  13. import std/sha1 # sha1 passwords.
  14. import os
  15. import re # regex
  16. # ----- EXPORT -----
  17. export asyncdispatch
  18. export asynchttpserver
  19. export strutils
  20. export json
  21. export uri
  22. export re
  23. type
  24. ServerRef* = ref object
  25. port*: uint16
  26. address*: string
  27. server*: AsyncHttpServer
  28. var AKANE_DEBUG_MODE*: bool = false ## change it with `newServer proc<#newServer,string,uint16,bool>`_
  29. proc newServer*(address: string = "127.0.0.1",
  30. port: uint16 = 5000, debug: bool = false): ServerRef =
  31. ## Creates a new ServerRef object.
  32. ##
  33. ## Arguments:
  34. ## - ``address`` - server address, e.g. "127.0.0.1"
  35. ## - ``port`` - server port, e.g. 5000
  36. ## - ``debug`` - debug mode
  37. if not existsDir("templates"):
  38. createDir("templates")
  39. AKANE_DEBUG_MODE = debug
  40. return ServerRef(
  41. address: address, port: port,
  42. server: newAsyncHttpServer()
  43. )
  44. proc toStr*(node: JsonNode): string =
  45. if node.kind == JString:
  46. return node.getStr
  47. else:
  48. return $node
  49. proc loadtemplate*(name: string, json: JsonNode = %*{}): Future[string] {.async, inline.} =
  50. ## Loads HTML template from `templates` folder.
  51. ##
  52. ## Arguments:
  53. ## - ``name`` - template's name, e.g. "index", "api", etc.
  54. ## - ``json`` - Json data, which replaces in the template.
  55. ##
  56. ## Replaces:
  57. ## - $(key) -> value
  58. ## - if $(key) { ... } -> ... (if value is true)
  59. ## - if not $(key) { ... } -> ... (if value is false)
  60. ## - for i in 0..$(key) { ... } -> ........., etc
  61. var
  62. file = openAsync(("templates" / name) & ".html")
  63. readed = await file.readAll()
  64. file.close()
  65. for key, value in json.pairs:
  66. # ---- regex patterns ---- #
  67. let
  68. # variable statement, e.g.: $(variable)
  69. variable_stmt = re("(\\$\\s*\\(" & key & "\\))")
  70. # if statement, e.g.: if $(variable) {......}
  71. if_stmt = re("if\\s*(\\$\\s*\\(" & key & "\\))\\s*\\{\\s*([\\s\\S]+?)\\s*\\}")
  72. # if not statement, e.g.: if not $(variable) {......}
  73. if_notstmt = re("if\\s*not\\s*(\\$\\s*\\(" & key & "\\))\\s*\\{\\s*([\\s\\S]+?)\\s*\\}")
  74. # for statement, e.g.: for i in 0..$(variable) {hello, $variable[i]}
  75. forstmt = re(
  76. "for\\s*([\\S]+)\\s*in\\s*(\\d+)\\.\\.(\\$\\s*\\(" & key & "\\))\\s*\\{\\s*([\\s\\S]+?)\\s*\\}")
  77. # ---- converts value to bool ---- #
  78. var value_bool = false
  79. case value.kind:
  80. of JBool:
  81. if value.getBool:
  82. value_bool = true
  83. of JInt:
  84. if value.getInt != 0:
  85. value_bool = true
  86. of JFloat:
  87. if value.getFloat != 0.0:
  88. value_bool = true
  89. of JString:
  90. if value.getStr.len > 0:
  91. value_bool = true
  92. of JArray:
  93. if value.len > 0:
  94. value_bool = true
  95. of JObject:
  96. if value.getFields.len > 0:
  97. value_bool = true
  98. else: discard
  99. # ---- replace ----- #
  100. if readed.contains(if_stmt):
  101. if value_bool:
  102. readed = readed.replacef(if_stmt, "$2")
  103. else:
  104. readed = readed.replacef(if_stmt, "")
  105. if readed.contains(if_notstmt):
  106. if value_bool:
  107. readed = readed.replacef(if_notstmt, "")
  108. else:
  109. readed = readed.replacef(if_notstmt, "$2")
  110. var
  111. matches: array[20, string]
  112. now = 0
  113. while readed.contains(forstmt):
  114. let
  115. (start, stop) = readed.findBounds(forstmt, matches, now)
  116. elem = re("(\\$" & key & "\\[" & matches[0] & "\\])")
  117. var output = ""
  118. for i in parseInt(matches[1])..<value.len:
  119. output &= matches[3].replacef(elem, value[i].toStr)
  120. readed = readed[0..start-1] & output & readed[stop+1..^1]
  121. now += stop
  122. readed = readed.replacef(variable_stmt, value.toStr)
  123. return readed
  124. proc parseQuery*(request: Request): Future[JsonNode] {.async.} =
  125. ## Decodes query.
  126. ## e.g.:
  127. ## "a=5&b=10" -> {"a": "5", "b": "10"}
  128. ##
  129. ## This also have debug output, if AKANE_DEBUG_MODE is true.
  130. var data = request.url.query.split("&")
  131. result = %*{}
  132. for i in data:
  133. let timed = i.split("=")
  134. if timed.len > 1:
  135. result[decodeUrl(timed[0])] = %decodeUrl(timed[1])
  136. if AKANE_DEBUG_MODE:
  137. let
  138. now = times.local(times.getTime())
  139. timed_month = ord(now.month)
  140. month = if timed_month > 9: $timed_month else: "0" & $timed_month
  141. day = if now.monthday > 9: $now.monthday else: "0" & $now.monthday
  142. hour = if now.hour > 9: $now.hour else: "0" & $now.hour
  143. minute = if now.minute > 9: $now.minute else: "0" & $now.minute
  144. second = if now.second > 9: $now.second else: "0" & $now.second
  145. host =
  146. if request.headers.hasKey("host") and request.headers["host"].len > 1:
  147. request.headers["host"] & " "
  148. else:
  149. "new "
  150. echo(
  151. host, request.reqMethod,
  152. " at ", now.year, ".", month, ".", day,
  153. " ", hour, ":", minute, ":", second,
  154. " Request from ", request.hostname,
  155. " to url \"", decodeUrl(request.url.path), "\".")
  156. proc password2hash*(password: string): Future[string] {.async, inline.} =
  157. ## Generates a sha1 from `password`.
  158. ##
  159. ## Arguments:
  160. ## - ``password`` - user password.
  161. return $secureHash(password)
  162. proc validatePassword*(password, hashpassword: string): Future[bool] {.async, inline.} =
  163. ## Validates the password and returns true, if the password is valid.
  164. ##
  165. ## Arguments:
  166. ## - ``password`` - got password from user input.
  167. ## - ``hashpassword`` - response from `password2hash proc <#password2hash,string>`_
  168. return secureHash(password) == parseSecureHash(hashpassword)
  169. macro pages*(server: ServerRef, body: untyped): untyped =
  170. ## This macro provides convenient page adding.
  171. ##
  172. ## `body` should be StmtList.
  173. ## page type can be:
  174. ## - ``equals``
  175. ## - ``startswith``
  176. ## - ``endswith``
  177. ## - ``regex``
  178. ## - ``notfound`` - this page uses without URL argument.
  179. runnableExamples:
  180. let server = newServer(debug=true)
  181. server.pages:
  182. equals("/home"):
  183. echo url
  184. echo urlParams
  185. await request.answer("Home")
  186. # You can also not write `equals("/")`:
  187. "/helloworld":
  188. await request.answer("Hello, world")
  189. var
  190. stmtlist = newStmtList()
  191. notfound_declaration = false
  192. stmtlist.add(
  193. newNimNode(nnkLetSection).add( # let urlParams: JsonNode = await parseQuery(request)
  194. newNimNode(nnkIdentDefs).add(
  195. ident("urlParams"),
  196. ident("JsonNode"),
  197. newCall(
  198. "await",
  199. newCall(
  200. "parseQuery",
  201. ident("request")
  202. )
  203. )
  204. )
  205. ),
  206. newNimNode(nnkLetSection).add( # let decode_url: string = decodeUrl(request.url.path)
  207. newNimNode(nnkIdentDefs).add(
  208. ident("decoded_url"),
  209. ident("string"),
  210. newCall(
  211. "decodeUrl",
  212. newNimNode(nnkDotExpr).add(
  213. newNimNode(nnkDotExpr).add(
  214. ident("request"), ident("url")
  215. ),
  216. ident("path")
  217. )
  218. )
  219. )
  220. )
  221. )
  222. stmtlist.add(newNimNode(nnkIfStmt))
  223. var ifstmtlist = stmtlist[2]
  224. for i in body: # for each page in statment list.
  225. let
  226. current = if i.len == 3: $i[0] else: "equals"
  227. path = if i.len == 3: i[1] else: i[0]
  228. slist = if i.len == 3: i[2] else: i[1]
  229. if (i.kind == nnkCall and
  230. (path.kind == nnkStrLit or path.kind == nnkCallStrLit or path.kind == nnkEmpty) and
  231. slist.kind == nnkStmtList):
  232. if current == "equals":
  233. slist.insert(0, # let url: string = `path`
  234. newNimNode(nnkLetSection).add(
  235. newNimNode(nnkIdentDefs).add(
  236. ident("url"),
  237. ident("string"),
  238. path
  239. )
  240. )
  241. )
  242. ifstmtlist.add( # decoded_url == `path`
  243. newNimNode(nnkElifBranch).add(
  244. newCall("==", path, ident("decoded_url")),
  245. slist))
  246. elif current == "startswith":
  247. slist.insert(0, # let url = decoded_url[`path`.len..^1]
  248. newNimNode(nnkLetSection).add(
  249. newNimNode(nnkIdentDefs).add(
  250. ident("url"),
  251. ident("string"),
  252. newCall(
  253. "[]",
  254. ident("decoded_url"),
  255. newCall(
  256. "..^",
  257. newCall("len", path),
  258. newLit(1))
  259. )
  260. )
  261. )
  262. )
  263. ifstmtlist.add( # decode_url.startsWith(`path`)
  264. newNimNode(nnkElifBranch).add(
  265. newCall(
  266. "startsWith",
  267. ident("decoded_url"),
  268. path),
  269. slist))
  270. elif current == "endswith":
  271. slist.insert(0, # let url: string = decoded_url[0..^`path`.len]
  272. newNimNode(nnkLetSection).add(
  273. newNimNode(nnkIdentDefs).add(
  274. ident("url"),
  275. ident("string"),
  276. newCall(
  277. "[]",
  278. ident("decoded_url"),
  279. newCall(
  280. "..^",
  281. newLit(0),
  282. newCall("+", newLit(1), newCall("len", path))
  283. )
  284. )
  285. )
  286. )
  287. )
  288. ifstmtlist.add( # decode_url.endsWith(`path`)
  289. newNimNode(nnkElifBranch).add(
  290. newCall(
  291. "endsWith",
  292. ident("decoded_url"),
  293. path),
  294. slist))
  295. elif current == "regex":
  296. slist.insert(0, # discard match(decoded_url, `path`, url)
  297. newNimNode(nnkDiscardStmt).add(
  298. newCall("match", ident("decoded_url"), path, ident("url"))
  299. )
  300. )
  301. slist.insert(0, # var url: array[20, string]
  302. newNimNode(nnkVarSection).add(
  303. newNimNode(nnkIdentDefs).add(
  304. ident("url"),
  305. newNimNode(nnkBracketExpr).add(
  306. ident("array"), newLit(20), ident("string")
  307. ),
  308. newEmptyNode()
  309. )
  310. ))
  311. ifstmtlist.add( # decode_url.match(`path`)
  312. newNimNode(nnkElifBranch).add(
  313. newCall("match", ident("decoded_url"), path),
  314. slist))
  315. elif current == "notfound":
  316. notfound_declaration = true
  317. ifstmtlist.add(newNimNode(nnkElse).add(slist))
  318. if not notfound_declaration:
  319. ifstmtlist.add(
  320. newNimNode(nnkElse).add(
  321. newCall( # await request.respond(Http404, "Not found")
  322. "await",
  323. newCall(
  324. "respond",
  325. ident("request"),
  326. ident("Http404"),
  327. newLit("Not found"))
  328. )
  329. )
  330. )
  331. result = newNimNode(nnkProcDef).add(
  332. ident("receivepages"), # procedure name.
  333. newEmptyNode(), # for template and macros
  334. newEmptyNode(), # generics
  335. newNimNode(nnkFormalParams).add( # proc params
  336. newEmptyNode(), # return type
  337. newNimNode(nnkIdentDefs).add( # param
  338. ident("request"), # param name
  339. ident("Request"), # param type
  340. newEmptyNode() # param default value
  341. )
  342. ),
  343. newNimNode(nnkPragma).add( # pragma declaration
  344. ident("async"),
  345. ident("gcsafe")
  346. ),
  347. newEmptyNode(),
  348. stmtlist)
  349. macro answer*(request, message: untyped, http_code = Http200): untyped =
  350. ## Responds from server with utf-8.
  351. ##
  352. ## Translates to:
  353. ## request.respond(Http200, "<head><meta charset='utf-8'></head>" & message)
  354. result = newCall(
  355. "respond",
  356. request,
  357. http_code,
  358. newCall("&", newLit("<head><meta charset='utf-8'></head>"), message)
  359. )
  360. macro error*(request, message: untyped, http_code = Http404): untyped =
  361. ## Responds from server with utf-8.
  362. ##
  363. ## Translates to:
  364. ## request.respond(Http404, "<head><meta charset='utf-8'></head>" & message)
  365. result = newCall(
  366. "respond",
  367. request,
  368. http_code,
  369. newCall("&", newLit("<head><meta charset='utf-8'></head>"), message)
  370. )
  371. macro sendJson*(request, message: untyped, http_code = Http200): untyped =
  372. ## Sends JsonNode with "Content-Type": "application/json" in headers.
  373. ##
  374. ## Translates to:
  375. ## request.respond(
  376. ## Http200,
  377. ## $message,
  378. ## newHttpHeaders([("Content-Type","application/json")]))
  379. result = newCall(
  380. "respond",
  381. request,
  382. http_code,
  383. newCall("$", message),
  384. newCall(
  385. "newHttpHeaders",
  386. newNimNode(nnkBracket).add(
  387. newNimNode(nnkPar).add(
  388. newLit("Content-Type"),
  389. newLit("application/json")
  390. )
  391. )
  392. )
  393. )
  394. macro start*(server: ServerRef): untyped =
  395. ## Starts server.
  396. result = quote do:
  397. if AKANE_DEBUG_MODE:
  398. echo "Server starts on http://", `server`.address, ":", `server`.port
  399. waitFor `server`.server.serve(Port(`server`.port), receivepages, `server`.address)