網站首頁 編程語言 正文
用golang來實現的webserver通常是是這樣的
//main.go package main import ( "fmt" "io" "net/http" ) func defaultHandler(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "<h1>Golang HTTP</h1>") } func main() { mux := http.NewServeMux() mux.HandleFunc("/", defaultHandler) err := http.ListenAndServe(":80", mux) if err != nil { fmt.Println(err.Error()) } }
服務運行后,我們通常通過http://localhost
的形式來訪問,
而我們要實現的是通過https://localhost
的形式來訪問.
那么如何用golang來實現HTTPS呢?
//main.go package main import ( "fmt" "io" "net/http" ) func defaultHandler(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "<h1>Golang HTTPS</h1>") } func main() { mux := http.NewServeMux() mux.HandleFunc("/", defaultHandler) certFile := "/etc/letsencrypt/live/www.taadis.com/cert.pem" keyFile := "/etc/letsencrypt/live/www.taadis.com/privkey.pem" err := http.ListenAndServeTLS(":443", certFile, keyFile, mux) if err != nil { fmt.Println(err.Error()) } }
源碼比較簡單,主要是把http.ListenAndServe()
替換成ListenAndServeTLS()
。其次注意下端口號的區別,還有就是CA證書的問題,這里我采用了Let's Encrypt。
原文鏈接:https://www.cnblogs.com/taadis/p/12126228.html
相關推薦
- 2022-08-04 Python?threading和Thread模塊及線程的實現_python
- 2022-06-01 python中的mock接口開發示例詳解_python
- 2023-04-02 使用Pytorch如何完成多分類問題_python
- 2023-01-15 C#實現虛擬鍵盤的實例詳解_C#教程
- 2023-12-08 Can‘t open the append-only file: Permission denied
- 2022-05-06 Matplotlib安裝與配置
- 2022-06-01 Python全角與半角之間相互轉換的方法總結_python
- 2022-12-28 jQuery如何設置背景顏色_jquery
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支