I would like to be able to stop a running gin.Engine, but cannot. I believe the root issue is that https://github.com/gin-gonic/gin/blob/master/gin.go#L301 calls http.ListenAndServe. If instead, it created and returned a server, then that server could be shutdown, thus closing the Engine.
E.g. replace line 301 with:
server := &Server{Addr: address, Handler: engine}
return server, server.ListenAndServe()
Create an engine and run it.
That an engine should either have a close/shutdown/stop command or should return a server that can be run and shutdown independently.
The Run command never halts and provides no way mechanism to shutdown cleanly without issuing a signal.
There is a way in README.md that can shut down gracefully.
see https://github.com/gin-gonic/gin#graceful-shutdown-or-restart
See more example: https://github.com/gin-gonic/examples/tree/master/graceful-shutdown
I'll try what @shlinym suggested, but I do think needing to replace ListenAndServe seems needlessly complicated. Before posting this issue, I had found the "graceful-shutdown" example. The issue with that approach is that it uses signals which will leave the rest of my application in an unknown state. If I want to shutdown my entire application, it's fine, but if I want to leave my application running and shutdown only the Engine, I'm in a pickle.
Most helpful comment
I'll try what @shlinym suggested, but I do think needing to replace ListenAndServe seems needlessly complicated. Before posting this issue, I had found the "graceful-shutdown" example. The issue with that approach is that it uses signals which will leave the rest of my application in an unknown state. If I want to shutdown my entire application, it's fine, but if I want to leave my application running and shutdown only the Engine, I'm in a pickle.