golangGolang 入门资料+笔记

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

2021-03-02  本文已影响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()
    }
}
上一篇下一篇

猜你喜欢

热点阅读