1 回答

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超1個(gè)贊
您需要使用http.ListenAndServeTLS來啟動(dòng) HTTPS 服務(wù)器。
func main() {
? ? // Set up the handler to serve a file
? ? http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
? ? ? ? w.Header().Set("Content-Type", "text/plain; charset=utf-8")
? ? ? ? http.ServeFile(w, req, "./text.txt")
? ? })
? ? log.Printf("About to listen on 8443. Go to https://127.0.0.1:8443/")
? ? log.Fatal(http.ListenAndServeTLS(":8443", "cert.pem", "key.pem", nil))
}
并啟動(dòng)一個(gè)為目錄提供服務(wù)的 HTTPS 服務(wù)器FileServer
...
log.Fatal(http.ListenAndServeTLS(":8443",?"cert.pem",?"key.pem",?http.FileServer(http.Dir("./static"))))
您可以使用generate_cert.go
創(chuàng)建自簽名證書進(jìn)行測(cè)試。
- 1 回答
- 0 關(guān)注
- 141 瀏覽
添加回答
舉報(bào)