美文网首页golangGolang 入门资料+笔记
go-gin框架添加对HTTPS的支持

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

作者: 五岁小孩 | 来源:发表于2021-03-02 09:10 被阅读0次

    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()
        }
    }
    

    相关文章

      网友评论

        本文标题:go-gin框架添加对HTTPS的支持

        本文链接:https://www.haomeiwen.com/subject/xzugxltx.html