...

Source file src/github.com/gin-contrib/cors/_examples/example.go

Documentation: github.com/gin-contrib/cors/_examples

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

View as plain text