I was hoping someone here could tell me the best way to deploy golang api's. I am concerned about a panic during runtime and the server shutting down.
Does anyone have some life advise for me?
评论:
deusmetallum:
flamin_flamingo_lips:Run it in a docker container with --restart=unless-stopped
That way, of the API dies, the docker container will also die, and docker will restart it.
If you're running in Linux, systemd has similar options, but docker is easier and more portable.
tmornini:Ah, great idea. Thank you.
I'm imagining having it behind an Apache server too? Forwarding requests to a port.
flamin_flamingo_lips:Why?
But sure, if you want/need to.
tmornini:If we didn't have a dedicated server for it. So we could run other projects through Apache.
ZetaHunter:There are always subdomains...
carsncode:Look into using traefik instead of apache then.
flamin_flamingo_lips:You can always put your process into its own function, and inside main(), use recover to catch any panics and call the function again to re-start it.
Note that if this API is using net/http, it already recovers panics and logs them as errors, and the panic only crashes that request, not the whole process.
carsncode:There is some recover magic going on already, but there does seem to be some errors that it can't recover from fully sometimes.
Also I must not be using net/http right in go cause I think anytime it panicked it crashed the server, but I think that may of been from using log.Fatal before. Thanks for your advise though!
sprayAtMeBro:log.Fatal doesn't panic, it calls os.Exit, which is not recoverable.
AnthonySterling:If using systemd you could configure it to restart on failure. Docker doesn't make much sense to me unless that's already a standard deployment tool for you.
If it's just a simple binary, systemd or supervisord would be the way I'd approach this - probably proxied via nginx/apache so I could still have some control over ingress.
