cors.go 793 B

12345678910111213141516171819202122232425262728293031
  1. // Copyright 2020 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package middleware
  5. import (
  6. "time"
  7. "github.com/gin-contrib/cors"
  8. "github.com/gin-gonic/gin"
  9. )
  10. const (
  11. maxAge = 12
  12. )
  13. // Cors add cors headers.
  14. func Cors() gin.HandlerFunc {
  15. return cors.New(cors.Config{
  16. AllowOrigins: []string{"*"},
  17. AllowMethods: []string{"PUT", "PATCH", "GET", "POST", "OPTIONS", "DELETE"},
  18. AllowHeaders: []string{"Origin", "Authorization", "Content-Type", "Accept"},
  19. ExposeHeaders: []string{"Content-Length"},
  20. AllowCredentials: true,
  21. AllowOriginFunc: func(origin string) bool {
  22. return origin == "https://github.com"
  23. },
  24. MaxAge: maxAge * time.Hour,
  25. })
  26. }