-
Notifications
You must be signed in to change notification settings - Fork 0
/
cors.go
54 lines (46 loc) · 1.13 KB
/
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
45
46
47
48
49
50
51
52
53
54
package config
import (
"strings"
"github.com/gofiber/fiber/v2/middleware/cors"
)
type corsConfig struct {
AllowCredentials bool `mapstructure:"allow-credentials"`
AllowOrigins string `mapstructure:"allow-origins"`
AllowHeaders string `mapstructure:"allow-headers"`
AllowMethods string `mapstructure:"allow-methods"`
}
func (c *corsConfig) setDefaults() {
// FIXME: can't default bool this way
//
// if !c.AllowCredentials {
// c.AllowCredentials = true
// }
if c.AllowOrigins == "" {
c.AllowOrigins = "*"
}
if c.AllowHeaders == "" {
headers := []string{
"Origin",
"Content-Type",
"Accept",
"Content-Length",
"Accept-Language",
"Accept-Encoding",
"Connection",
"Access-Control-Allow-Origin",
}
c.AllowHeaders = strings.Join(headers, ",")
}
if c.AllowMethods == "" {
c.AllowMethods = "GET,POST,HEAD,PUT,DELETE,PATCH,OPTIONS"
}
}
func (c *corsConfig) ToCorsConfig() cors.Config {
return cors.Config{
// AllowCredentials: c.AllowCredentials,
AllowCredentials: true,
AllowOrigins: c.AllowOrigins,
AllowHeaders: c.AllowHeaders,
AllowMethods: c.AllowMethods,
}
}