在单个VM中为用Google Go编写的两个网站提供服务

I have a VM from Digital Ocean. It currently has two domains linked to the VM. I do not use any other web server but Golang's built in http module. Performance-wise I like it, and I feel like I have a full control over it.

Currently I am using a single Go program that has multiple websites built in.

http.HandleFunc("test.com/", serveTest)
http.HandleFunc("123.com/", serve123)
http.HandleFunc("/", serve123)

As they are websites, Go program is using port 80 for that. And the problem is when I am trying to update only 1 website, I have to recompile whole thing as they are written in the same code.

1) Is there a way to make it hot-swappable only with Golang (without Nginx or Apache) 2) What would be a standard best practice?

Thank you so much!

Well, you can do hotswapping in go, but I really wouldn't want to do that unless really ncecessary as the complexity added isn't negligible (and I'm not talking about code).

You can have something close with a kind of proxy that would sit in front of the program and do a graceful swap whenever your binary change : the principle is to have the binary on one port, the proxy on another. When a new binary is ready, you run it on another port, and make the proxy redirect to the new port, then gracefully shutdown the old one.

There was a tool for that in Go that I can't remember the name of…

EDIT: not the one I had in mind, but close call https://github.com/rcrowley/goagain

Personnal advice: use a reverse proxy for that, its much more simple to do. My personnal setup is to use h2o to terminate SSL, HTTP2, etc, and send the requests to the various websites running on the background. Not only Go ones, though, but also PHP ones, a Gitlab instance, etc. Its much more flexible, and the performance penalty of the proxy is small…