-
Notifications
You must be signed in to change notification settings - Fork 1
/
cors.go
44 lines (34 loc) · 1.02 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
package service
import (
"github.com/rs/cors"
)
var (
defaultCorsAllowedOrigins = []string{"*"}
defaultCorsAllowedMethods = []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}
defaultCorsAllowedHeaders = []string{
"Access-Control-Allow-Origin",
"Content-Type",
"Authorization",
}
)
// CORS sets up the Cross-Origin settings if the current configuration asks for it
func (s *Service) cors() {
if envBool(EnvCorsEnable) {
opts := cors.Options{
AllowedOrigins: defaultCorsAllowedOrigins,
AllowedMethods: defaultCorsAllowedMethods,
AllowedHeaders: defaultCorsAllowedHeaders,
Debug: envBool(EnvCorsDebug),
}
if len(envStrings(EnvCorsAllowedOrigins)) > 0 {
opts.AllowedOrigins = envStrings(EnvCorsAllowedOrigins)
}
if len(envStrings(EnvCorsAllowedMethods)) > 0 {
opts.AllowedMethods = envStrings(EnvCorsAllowedMethods)
}
if len(envStrings(EnvCorsAllowedHeaders)) > 0 {
opts.AllowedHeaders = envStrings(EnvCorsAllowedHeaders)
}
s.AddHTTPMiddleware(cors.New(opts).Handler)
}
}