gpt4 book ai didi

go - 使用 gorilla mux 的 Autocert

转载 作者:IT王子 更新时间:2023-10-29 02:20:52 27 4
gpt4 key购买 nike

我想使用 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 集成到我的实际代码中?

最佳答案

整合是这样的:

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
}

我建议给服务器添加一些超时。我通常选择读取、写入和空闲。

关于go - 使用 gorilla mux 的 Autocert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50311532/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com