I want to generate certificate using autocert and using gorila mux, my actual code is:
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
}
my routers is:
r := mux.NewRouter()
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(dir))))
r.HandleFunc("/login.html", LoginHtml)
How can I integrate the gurilla mux in my actual code?
The integration would be like this:
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
}
I would suggest to add some timeouts to the server. I usually go for Read, Write and Idle.