-
Notifications
You must be signed in to change notification settings - Fork 10
/
cors.go
43 lines (39 loc) · 985 Bytes
/
cors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package gin
import (
krakendcors "github.com/devopsfaith/krakend-cors"
"github.com/devopsfaith/krakend/config"
"github.com/gin-gonic/gin"
"gopkg.in/gin-contrib/cors.v1"
)
// New returns a gin.HandlerFunc with the CORS configuration provided in the ExtraConfig
func New(e config.ExtraConfig) gin.HandlerFunc {
tmp := krakendcors.ConfigGetter(e)
if tmp == nil {
return nil
}
cfg, ok := tmp.(krakendcors.Config)
if !ok {
return nil
}
var allowAllOrigins bool
if len(cfg.AllowOrigins) == 0 {
allowAllOrigins = true
} else {
for _, origin := range cfg.AllowOrigins {
if origin == "*" {
allowAllOrigins = true
cfg.AllowOrigins = nil
break
}
}
}
return cors.New(cors.Config{
AllowAllOrigins: allowAllOrigins,
AllowOrigins: cfg.AllowOrigins,
AllowMethods: cfg.AllowMethods,
AllowHeaders: cfg.AllowHeaders,
ExposeHeaders: cfg.ExposeHeaders,
AllowCredentials: cfg.AllowCredentials,
MaxAge: cfg.MaxAge,
})
}