t9655.nim 653 B

12345678910111213141516171819202122232425262728293031
  1. discard """
  2. action: "compile"
  3. """
  4. import std/[asynchttpserver, asyncdispatch]
  5. import std/[strformat]
  6. proc main() =
  7. let local = "123"
  8. proc serveIndex(req: Request) {.async, gcsafe.} =
  9. await req.respond(Http200, &"{local}")
  10. proc serve404(req: Request) {.async, gcsafe.} =
  11. echo req.url.path
  12. await req.respond(Http404, "not found")
  13. proc serve(req: Request) {.async, gcsafe.} =
  14. let handler = case req.url.path:
  15. of "/":
  16. serveIndex
  17. else:
  18. serve404
  19. await handler(req)
  20. let server = newAsyncHttpServer()
  21. waitFor server.serve(Port(8080), serve, address = "127.0.0.1")
  22. when isMainModule:
  23. main()