1,自行用命令生成crt文件和key文件
2,同时启动http 和https服务,端口号随意
package main
import (
"shoppingMall/routers"
"github.com/gin-gonic/gin"
"github.com/unrolled/secure"
)
func main() {
r := gin.Default()
rs := gin.Default()
r.Static("/static", "./static")
rs.Static("/static", "./static")
rs.Use(TlsHandler()) // 处理SSL的中间件
routers.DefaultRouterInit(r)
routers.DefaultRouterInit(rs)
go rs.RunTLS(":4433", "./conf/domain.crt", "./conf/domain.key")
r.Run(":3009")
}
func TlsHandler() gin.HandlerFunc {
return func(c *gin.Context) {
secureMiddleware := secure.New(secure.Options{
SSLRedirect: true,
SSLHost: ":443",
})
err := secureMiddleware.Process(c.Writer, c.Request)
if err != nil {
return
}
c.Next()
}
}
routers
package routers
import (
"shoppingMall/controllers/api"
"github.com/gin-gonic/gin"
)
func DefaultRouterInit(r *gin.Engine) {
defaultRouter := r.Group("/")
defaultRouter.GET("/", api.Api{}.Test)
}