go-gin框架添加对HTTPS的支持

作者: adm 分类: go 发布时间: 2022-12-05

go-gin框架添加对HTTPS的支持

package main

import (
    "github.com/gin-gonic/gin"
    "github.com/unrolled/secure"
)

func main() {
    router := gin.Default()
    router.Use(TlsHandler())
    router.RunTLS(":8088", "xj.pem", "xj.key")
}

func TlsHandler() gin.HandlerFunc {
    return func(c *gin.Context) {
        secureMiddleware := secure.New(secure.Options{
            SSLRedirect: true,
            SSLHost:     "localhost:8080",
        })
        err := secureMiddleware.Process(c.Writer, c.Request)
        if err != nil {
            c.Abort()
            return
        }
        c.Next()
    }
}

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!