This code works as expected. But I wanted to replace the "block routes:" with "routes:" only. Any idea how to do this?
import asynchttpserver, asyncdispatch
template via*(rule_path: string, reqMethods: seq[string], code_to_execute: untyped): untyped =
if $req.reqMethod in reqMethods and req.url.path == rule_path:
code_to_execute
break routes
proc handler(req: Request) {.async.} =
block routes: # <== only routes:
via "/abc", @["GET"]:
await req.respond(Http200, "abc")
via "/xyz", @["GET", "POST"]:
await req.respond(Http200, "xyz")
via "/test", @["POST"]:
await req.respond(Http200, "test")
via "/", @["GET", "POST"]:
await req.respond(Http200, "home")
await req.respond(Http404, "no route")
let server = newAsyncHttpServer()
waitFor server.serve(Port(8080), handler)
Try add the following code:
template routes(body: untyped): untyped {.dirty.}=
block routes:
body