我想使用autocert生成证书并使用gorila mux,我的实际代码是:
func main() {
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
//HostPolicy: autocert.HostWhitelist("example.com"),
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
}
我的路由器是:
r := mux.NewRouter()
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(dir))))
r.HandleFunc("/login.html", LoginHtml)
如何在实际代码中集成gurilla mux?
答案 0 :(得分:0)
集成如下:
func main() {
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
//HostPolicy: autocert.HostWhitelist("example.com"),
Cache: autocert.DirCache("./certs"), //Folder for storing certificates
}
r := mux.NewRouter()
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(dir))))
r.HandleFunc("/login.html", LoginHtml)
server := &http.Server{
Addr: ":https",
Handler: r,
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
}
我建议向服务器添加一些超时。我通常会去读,写和空闲。