I'd like to write something like C's switch/case. Is it possible without writing a custom router?
routes:
get "/hello":
get "/helloWorld":
resp "Hello World"
I can write separate handler proc and return http code, headers and content, but may be there is a more beautiful solution?
You can use templates like this: https://play.nim-lang.org/#ix=2pC5
import tables
var routeMap: Table[string, proc()]
template routes(body: untyped) =
# init server pre routs
body
template get(url: string, body: untyped) =
routeMap[url] = proc() =
body
proc resp(data: string) =
discard
routes:
get "/hello":
resp "Hello"
get "/helloWorld":
resp "Hello World"
for k, v in routeMap:
echo k
While not as flexible as @treeform 's solution, but if the naming allows for it, you could do a patterned naming. Either as a regex (see https://github.com/dom96/jester#regex) or simple pattern. For example:
import jester
routes:
get "/hello@suffix":
resp "Hello World"
I also have an experimental fork for plugins also supports subrouters grouped by url prefix, but that is very iffy right now.