The best solution to gracefully terminate an asynchttpserverthat that I found!

An exception to be thrown on SIGINT.

[Rosettacode](https://rosettacode.org/wiki/Handle_a_signal#Nim)
    
    
    import asynchttpserver, asyncdispatch
    
    type EKeyboardInterrupt = object of Exception
    
    setControlCHook(proc() {.noconv.} =
      raise newException(EKeyboardInterrupt, "Keyboard Interrupt")
    )
    
    proc main() =
      let server = newAsyncHttpServer()
      
      # let dbConn = await connect2db()
      
      proc cb(req: Request) {.async.} =
        await req.respond(Http200, "Hello World")
      
      try:
        waitFor server.serve(Port(8080), cb)
      except EKeyboardInterrupt:
        echo "Start the server shutdown ..."
      finally:
        # await dbConn.close()
        server.close()
        echo "Server shutdown completed."
        quit(QuitSuccess)
    
    main()
    
    
    
    Run

Reply via email to