You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
511 B

  1. package middleware
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/unrolled/secure"
  6. )
  7. // 用https把这个中间件在router里面use一下就好
  8. func LoadTls() gin.HandlerFunc {
  9. return func(c *gin.Context) {
  10. middleware := secure.New(secure.Options{
  11. SSLRedirect: true,
  12. SSLHost: "localhost:443",
  13. })
  14. err := middleware.Process(c.Writer, c.Request)
  15. if err != nil {
  16. //如果出现错误,请不要继续。
  17. fmt.Println(err)
  18. return
  19. }
  20. // 继续往下处理
  21. c.Next()
  22. }
  23. }