-
Notifications
You must be signed in to change notification settings - Fork 0
/
define.go
103 lines (84 loc) · 2.06 KB
/
define.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package CorsSettings
import (
"github.com/lamgor666/goboot-common/util/castx"
"time"
)
type Settings struct {
allowedOrigins []string
allowedHeaders []string
allowedMethods []string
allowCredentials bool
exposedHeaders []string
maxAge time.Duration
}
func New(settings map[string]interface{}) *Settings {
allowedOrigins := []string{"*"}
if a1 := castx.ToStringSlice(settings["allowedOrigins"]); len(a1) > 0 {
allowedOrigins = a1
}
allowedHeaders := []string{
"Content-Type",
"Content-Length",
"Authorization",
"Accept",
"Accept-Encoding",
"X-Requested-With",
}
if a1 := castx.ToStringSlice(settings["allowedHeaders"]); len(a1) > 0 {
allowedHeaders = a1
}
allowedMethods := []string{
"GET",
"POST",
"PUT",
"PATCH",
"DELETE",
"OPTIONS",
}
if a1 := castx.ToStringSlice(settings["allowedMethods"]); len(a1) > 0 {
allowedMethods = a1
}
exposedHeaders := []string{
"Content-Length",
"Access-Control-Allow-Origin",
"Access-Control-Allow-Headers",
"Cache-Control",
"Content-Language",
"Content-Type",
}
if a1 := castx.ToStringSlice(settings["exposedHeaders"]); len(a1) > 0 {
exposedHeaders = a1
}
var maxAge time.Duration
if d1, ok := settings["maxAge"].(time.Duration); ok && d1 >= 0 {
maxAge = d1
} else if s1, ok := settings["maxAge"].(string); ok && s1 != "" {
maxAge = castx.ToDuration(s1)
}
return &Settings{
allowedOrigins: allowedOrigins,
allowedHeaders: allowedHeaders,
allowedMethods: allowedMethods,
allowCredentials: castx.ToBool(settings["allowCredentials"]),
exposedHeaders: exposedHeaders,
maxAge: maxAge,
}
}
func (st *Settings) AllowedOrigins() []string {
return st.allowedOrigins
}
func (st *Settings) AllowedHeaders() []string {
return st.allowedHeaders
}
func (st *Settings) AllowedMethods() []string {
return st.allowedMethods
}
func (st *Settings) AllowCredentials() bool {
return st.allowCredentials
}
func (st *Settings) ExposedHeaders() []string {
return st.exposedHeaders
}
func (st *Settings) MaxAge() time.Duration {
return st.maxAge
}