example.go 703 B

1234567891011121314151617181920212223242526272829
  1. package main
  2. import (
  3. "time"
  4. "github.com/gin-contrib/cors"
  5. "github.com/gin-gonic/gin"
  6. )
  7. func main() {
  8. router := gin.Default()
  9. // CORS for https://foo.com and https://github.com origins, allowing:
  10. // - PUT and PATCH methods
  11. // - Origin header
  12. // - Credentials share
  13. // - Preflight requests cached for 12 hours
  14. router.Use(cors.New(cors.Config{
  15. AllowOrigins: []string{"https://foo.com"},
  16. AllowMethods: []string{"PUT", "PATCH"},
  17. AllowHeaders: []string{"Origin"},
  18. ExposeHeaders: []string{"Content-Length"},
  19. AllowCredentials: true,
  20. AllowOriginFunc: func(origin string) bool {
  21. return origin == "https://github.com"
  22. },
  23. MaxAge: 12 * time.Hour,
  24. }))
  25. router.Run()
  26. }