Jelajahi Sumber

add sendPlainText and send macro.

SakiKawasaki 5 tahun lalu
induk
melakukan
be44d64471
4 mengubah file dengan 63 tambahan dan 13 penghapusan
  1. 2 2
      README.md
  2. 1 1
      akane.nimble
  3. 57 7
      akane/akane.nim
  4. 3 3
      tests/test8/main.nim

+ 2 - 2
README.md

@@ -6,8 +6,8 @@
 [![License](https://img.shields.io/github/license/Ethosa/akane)](https://github.com/Ethosa/akane/blob/master/LICENSE)
 [![test](https://github.com/Ethosa/akane/workflows/test/badge.svg)](https://github.com/Ethosa/akane/actions)
 
-<h4>Latest version - 0.1.2</h4>
-<h4>Stable version - 0.1.2</h4>
+<h4>Latest version - 0.1.3</h4>
+<h4>Stable version - 0.1.3</h4>
 </div>
 
 ## Install

+ 1 - 1
akane.nimble

@@ -1,7 +1,7 @@
 [Package]
 name = "akane"
 author = "Ethosa"
-version = "0.1.2"
+version = "0.1.3"
 description = "The Nim asynchronous web framework."
 license = "MIT"
 srcDir = "akane"

+ 57 - 7
akane/akane.nim

@@ -314,7 +314,8 @@ macro pages*(server: ServerRef, body: untyped): untyped =
     if (i.kind == nnkCall and
         (path.kind == nnkStrLit or path.kind == nnkCallStrLit or path.kind == nnkEmpty) and
         slist.kind == nnkStmtList):
-      if current == "equals":
+      case current
+      of "equals":
         slist.insert(0,  # let url: string = `path`
           newNimNode(nnkLetSection).add(
             newNimNode(nnkIdentDefs).add(
@@ -328,7 +329,7 @@ macro pages*(server: ServerRef, body: untyped): untyped =
             slist
           )
         )
-      elif current == "startswith":
+      of "startswith":
         slist.insert(0,  # let url = decoded_url[`path`.len..^1]
           newNimNode(nnkLetSection).add(
             newNimNode(nnkIdentDefs).add(
@@ -348,7 +349,7 @@ macro pages*(server: ServerRef, body: untyped): untyped =
             slist
             )
           )
-      elif current == "endswith":
+      of "endswith":
         slist.insert(0,  # let url: string = decoded_url[0..^`path`.len]
           newNimNode(nnkLetSection).add(
             newNimNode(nnkIdentDefs).add(
@@ -370,7 +371,7 @@ macro pages*(server: ServerRef, body: untyped): untyped =
             slist
           )
         )
-      elif current == "regex":
+      of "regex":
         slist.insert(0,  # discard match(decoded_url, `path`, url)
             newNimNode(nnkDiscardStmt).add(
               newCall("match", ident("decoded_url"), path, ident("url"))
@@ -390,9 +391,11 @@ macro pages*(server: ServerRef, body: untyped): untyped =
           newNimNode(nnkElifBranch).add(
             newCall("match", ident("decoded_url"), path),
             slist))
-      elif current == "notfound":
+      of "notfound":
         notfound_declaration = true
         ifstmtlist.add(newNimNode(nnkElse).add(slist))
+      else:
+        discard
 
   if not notfound_declaration:
     ifstmtlist.add(
@@ -424,6 +427,23 @@ macro pages*(server: ServerRef, body: untyped): untyped =
     stmtlist)
 
 
+macro send*(request, message: untyped, http_code = Http200,
+             headers: HttpHeaders = newHttpHeaders()): untyped =
+  ## Responds from server with utf-8.
+  ##
+  ## Translates to
+  ##
+  ## .. code-block:: nim
+  ##
+  ##    request.respond(Http200, message, headers)
+  ##
+  ## ## Example
+  ## .. code-block:: nim
+  ##
+  ##    await request.send("hello!")
+  result = newCall("respond", request, http_code, message, headers)
+
+
 macro answer*(request, message: untyped, http_code = Http200,
              headers: HttpHeaders = newHttpHeaders()): untyped =
   ## Responds from server with utf-8.
@@ -432,7 +452,7 @@ macro answer*(request, message: untyped, http_code = Http200,
   ##
   ## .. code-block:: nim
   ##
-  ##    request.respond(Http200, "<head><meta charset='utf-8'></head>" & message)
+  ##    request.respond(Http200, "<head><meta charset='utf-8'></head>" & message, headers)
   ##
   ## ## Example
   ## .. code-block:: nim
@@ -482,7 +502,7 @@ macro sendJson*(request, message: untyped, http_code = Http200): untyped =
   ## ## Example
   ## .. code-block:: nim
   ##
-  ##    await request.sendJson(%{"response": "error", "msg": "oops :("})
+  ##    await request.sendJson(%*{"response": "error", "msg": "oops :("})
   result = newCall(
     "respond",
     request,
@@ -500,6 +520,36 @@ macro sendJson*(request, message: untyped, http_code = Http200): untyped =
   )
 
 
+macro sendPlaintext*(request, message: untyped, http_code = Http200): untyped =
+  ## Sends JsonNode with "Content-Type": "application/json" in headers.
+  ##
+  ## Translates to
+  ##
+  ## .. code-block:: nim
+  ##
+  ##    request.respond(Http200, $message, newHttpHeaders([("Content-Type","plain/text")]))
+  ##
+  ## ## Example
+  ## .. code-block:: nim
+  ##
+  ##    await request.sendPlaintext(%*{"response": "error", "msg": "oops :("})
+  result = newCall(
+    "respond",
+    request,
+    http_code,
+    newCall("$", message),
+    newCall(
+      "newHttpHeaders",
+      newNimNode(nnkBracket).add(
+        newNimNode(nnkPar).add(
+          newLit("Content-Type"),
+          newLit("plain/text")
+        )
+      )
+    )
+  )
+
+
 macro start*(server: ServerRef): untyped =
   ## Starts server.
   ##

+ 3 - 3
tests/test8/main.nim

@@ -8,19 +8,19 @@ proc main {.gcsafe.} =
   server.pages:
     "/":  # http://127.0.0.1:5000/
       if request.reqMethod == HttpGet:
-        await request.answer("")
+        await request.sendPlaintext("")
       else:
         await request.error("not GET :(")
 
     "/user":  # http://127.0.0.1:5000/user
       if request.reqMethod == HttpGet:
-        await request.answer("")
+        await request.sendPlaintext("")
       else:
         await request.error("not GET :(")
 
     regex(re"\A/user/id(\d+)\Z"):  # http://127.0.0.1:5000/user/id123456 -> {"id": 123456}
       if request.reqMethod == HttpPost:
-        await request.sendJson(%*{"id": url[0]})
+        await request.sendPlaintext(%*{"id": url[0]})
       else:
         await request.error("not POST :(")