这是使用我发现的Go and Let’s Encrypt证书对HTTPS服务器进行的最小自动设置:
package mainimport ( "crypto/tls" "log" "net/http" "golang.org/x/crypto/acme/autocert")func main() { certManager := autocert.Manager{ prompt: autocert.AcceptTOS, HostPolicy: autocert.HostWhitelist("example.com"), //Your domain here Cache: autocert.DirCache("certs"), //Folder for storing certificates } http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello world")) }) server := &http.Server{ Addr: ":https", TLSConfig: &tls.Config{ GetCertificate: certManager.GetCertificate, }, } go http.ListenAndServe(":http", certManager.HTTPHandler(nil)) log.Fatal(server.ListenAndServeTLS("", "")) //Key and cert are coming from Let's Encrypt}有关autocert软件包的更多信息:链接
编辑:由于letencrypt安全性问题,需要使http可用,在此处了解更多信息。作为此修复程序的奖励,我们现在具有http->
https重定向。如果您已收到旧示例的证书,则旧示例将继续工作,但是对于新站点,它将中断。



