I have a simple https server serving a simple page like so (no error handling for brevity):
package main
import (
"crypto/tls"
"fmt"
"net/http"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "hello!")
})
xcert, _ := tls.LoadX509KeyPair("cert1.crt", "key1.pem")
tlsConf := &tls.Config{
Certificates: []tls.Certificate{xcert},
}
srv := &http.Server{
Addr: ":https",
Handler: mux,
TLSConfig: tlsConf,
}
srv.ListenAndServeTLS("", "")
}
I want to use a Let's Encrypt TLS certificate to serve the content over https. I would like to be able to do certificate renewals and update the certificate in the server without any downtime.
I tried ruing a goroutine to update the tlsConf:
go func(c *tls.Config) {
xcert, _ := tls.LoadX509KeyPair("cert2.crt", "key2.pem")
select {
case <-time.After(3 * time.Minute):
c.Certificates = []tls.Certificate{xcert}
c.BuildNameToCertificate()
fmt.Println("cert switched!")
}
}(tlsConf)
However, that doesn't work because the server does not "read in" the changed config. Is there anyway to ask the server to reload the TLSConfig?
