Thanks @xflywind for the tip.

I wrote this code to avoid several global variables. Is there a better 
solution? 
    
    
    import asynchttpserver, asyncdispatch
    
    var stop = false
    
    setControlCHook(proc() {.noconv.} =
      stop = true
      echo "Start the server shutdown ..."
    )
    
    proc shutdown(server: AsyncHttpServer) {.async.} =
      while true:
        if stop:
          server.close()
          echo "Server shutdown completed."
          quit(QuitSuccess)
        await sleepAsync(1000)
    
    
    proc main() =
      let server = newAsyncHttpServer()
      
      # Catch ctrl-c
      # asyncCheck shutdown(server, db, sessions)
      asyncCheck shutdown(server)
      
      proc cb(req: Request) {.async.} =
        await req.respond(Http200, "Hello World")
      
      waitFor server.serve(Port(8080), cb)
    
    main()
    
    
    Run

Reply via email to