2015-11-12 15:14:49 +01:00
|
|
|
# CORS gin's middleware
|
2016-10-19 14:51:59 +02:00
|
|
|
|
|
|
|
[![Build Status](https://travis-ci.org/gin-contrib/cors.svg)](https://travis-ci.org/gin-contrib/cors)
|
|
|
|
[![codecov](https://codecov.io/gh/gin-contrib/cors/branch/master/graph/badge.svg)](https://codecov.io/gh/gin-contrib/cors)
|
|
|
|
[![Go Report Card](https://goreportcard.com/badge/github.com/gin-contrib/cors)](https://goreportcard.com/report/github.com/gin-contrib/cors)
|
|
|
|
[![GoDoc](https://godoc.org/github.com/gin-contrib/cors?status.svg)](https://godoc.org/github.com/gin-contrib/cors)
|
2016-12-01 09:50:15 +01:00
|
|
|
[![Join the chat at https://gitter.im/gin-gonic/gin](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gin-gonic/gin)
|
2016-10-19 14:51:59 +02:00
|
|
|
|
2015-11-12 15:14:49 +01:00
|
|
|
Gin middleware/handler to enable CORS support.
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
2016-10-31 09:19:39 +01:00
|
|
|
### Start using it
|
|
|
|
|
2016-11-09 07:27:26 +01:00
|
|
|
Download and install it:
|
2016-10-31 09:19:39 +01:00
|
|
|
|
|
|
|
```sh
|
2017-06-27 18:18:33 +02:00
|
|
|
$ go get github.com/gin-contrib/cors
|
2016-10-31 09:19:39 +01:00
|
|
|
```
|
|
|
|
|
2016-11-09 07:27:26 +01:00
|
|
|
Import it in your code:
|
2016-10-31 09:19:39 +01:00
|
|
|
|
|
|
|
```go
|
2017-06-27 18:18:33 +02:00
|
|
|
import "github.com/gin-contrib/cors"
|
2016-10-31 09:19:39 +01:00
|
|
|
```
|
|
|
|
|
2016-10-19 14:51:59 +02:00
|
|
|
### Canonical example:
|
2015-11-12 15:14:49 +01:00
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2017-06-27 18:18:33 +02:00
|
|
|
"github.com/gin-contrib/cors"
|
2017-03-18 13:45:46 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
2015-11-12 15:14:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
router := gin.Default()
|
|
|
|
// CORS for https://foo.com and https://github.com origins, allowing:
|
|
|
|
// - PUT and PATCH methods
|
|
|
|
// - Origin header
|
|
|
|
// - Credentials share
|
|
|
|
// - Preflight requests cached for 12 hours
|
|
|
|
router.Use(cors.New(cors.Config{
|
|
|
|
AllowOrigins: []string{"https://foo.com"},
|
|
|
|
AllowMethods: []string{"PUT", "PATCH"},
|
|
|
|
AllowHeaders: []string{"Origin"},
|
|
|
|
ExposeHeaders: []string{"Content-Length"},
|
|
|
|
AllowCredentials: true,
|
|
|
|
AllowOriginFunc: func(origin string) bool {
|
|
|
|
return origin == "https://github.com"
|
|
|
|
},
|
|
|
|
MaxAge: 12 * time.Hour,
|
|
|
|
}))
|
|
|
|
router.Run()
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-10-19 14:51:59 +02:00
|
|
|
### Using DefaultConfig as start point
|
2015-11-12 15:14:49 +01:00
|
|
|
|
|
|
|
```go
|
|
|
|
func main() {
|
2016-10-19 14:51:59 +02:00
|
|
|
router := gin.Default()
|
|
|
|
// - No origin allowed by default
|
|
|
|
// - GET,POST, PUT, HEAD methods
|
|
|
|
// - Credentials share disabled
|
|
|
|
// - Preflight requests cached for 12 hours
|
|
|
|
config := cors.DefaultConfig()
|
|
|
|
config.AllowOrigins = []string{"http://google.com"}
|
|
|
|
config.AddAllowOrigins("http://facebook.com")
|
|
|
|
// config.AllowOrigins == []string{"http://google.com", "http://facebook.com"}
|
2015-11-12 15:14:49 +01:00
|
|
|
|
2016-10-19 14:51:59 +02:00
|
|
|
router.Use(cors.New(config))
|
|
|
|
router.Run()
|
2015-11-12 15:14:49 +01:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-10-19 14:51:59 +02:00
|
|
|
### Default() allows all origins
|
2015-11-12 15:14:49 +01:00
|
|
|
|
|
|
|
```go
|
2015-11-12 15:19:58 +01:00
|
|
|
func main() {
|
2016-10-19 14:51:59 +02:00
|
|
|
router := gin.Default()
|
|
|
|
// same as
|
|
|
|
// config := cors.DefaultConfig()
|
|
|
|
// config.AllowAllOrigins = true
|
|
|
|
// router.Use(cors.New(config))
|
|
|
|
router.Use(cors.Default())
|
|
|
|
router.Run()
|
2015-11-12 15:19:58 +01:00
|
|
|
}
|
2015-11-12 15:14:49 +01:00
|
|
|
```
|