-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcors.go
More file actions
37 lines (33 loc) · 943 Bytes
/
cors.go
File metadata and controls
37 lines (33 loc) · 943 Bytes
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
package httpx
import (
"strconv"
"strings"
"github.com/go-chi/cors"
)
func corsDefaults(_ Env) *cors.Options {
opts := cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "CONNECT", "TRACE"},
AllowedHeaders: []string{"*"},
AllowCredentials: true,
MaxAge: 300,
}
if s := fromEnv("CORS_ALLOWED_ORIGINS", "CORS_ORIGINS"); s != "" {
opts.AllowedOrigins = strings.Split(s, ",")
}
if s := fromEnv("CORS_ALLOWED_METHODS", "CORS_METHODS"); s != "" {
opts.AllowedMethods = strings.Split(s, ",")
}
if s := fromEnv("CORS_ALLOWED_HEADERS", "CORS_HEADERS"); s != "" {
opts.AllowedHeaders = strings.Split(s, ",")
}
if s := fromEnv("CORS_ALLOW_CREDENTIALS"); s == "false" {
opts.AllowCredentials = false
}
if s := fromEnv("CORS_MAX_AGE"); s != "" {
if n, err := strconv.Atoi(s); err == nil {
opts.MaxAge = n
}
}
return &opts
}