-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcors.go
55 lines (52 loc) · 1.24 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
55
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func SetCORSConfig() gin.HandlerFunc {
env := os.Getenv("ENV")
switch env {
case "production":
fmt.Println("CORS: Production")
origins := os.Getenv("ALLOWED_ORIGINS")
originsSlice := strings.Split(origins, ",")
return cors.New(cors.Config{
AllowOrigins: originsSlice,
AllowMethods: []string{"POST", "OPTIONS", "GET"},
AllowHeaders: []string{
"Content-Type",
"Content-Length",
"Accept-Encoding",
"X-CSRF-Token",
"Authorization",
"accept",
"origin",
"Cache-Control",
"X-Requested-With",
"sentry-trace",
"baggage",
},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
})
case "staging":
fmt.Println("CORS: Staging")
// Ensure 'Content-Type' is allowed in staging
return cors.New(cors.Config{
AllowAllOrigins: true,
AllowMethods: []string{"POST", "OPTIONS", "GET"},
AllowHeaders: []string{
"Content-Type", // explicitly allowing JSON Content-Type
},
})
default:
fmt.Println("CORS: Development")
// Allow all origins and headers in development for simplicity
return cors.Default()
}
}