-
Notifications
You must be signed in to change notification settings - Fork 2
/
cors.go
44 lines (38 loc) · 855 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
44
package ginshared
import (
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
type CorsComponent struct {
DefaultComponent
Enabled bool
}
// func init() {
// core.RegisterComponent(&CorsComponent{})
// }
func (c CorsComponent) OnEngineInited(r *gin.Engine) error {
log := zap.L()
// corsSettings := viper.Sub("CORS")
// if corsSettings != nil {
// corsSettings.Unmarshal(c)
// if c.Enabled {
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"*"},
AllowHeaders: []string{"*"},
ExposeHeaders: []string{"*"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
log.Info("CORS enabled, defaults allow all")
// }
// } else {
// log.Info("CORS is disabled.")
// }
return nil
}
func init() {
RegisterComponent(&CorsComponent{})
}