-
Notifications
You must be signed in to change notification settings - Fork 0
/
cors.go
46 lines (42 loc) · 1.11 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
package middleware
import (
"github.com/gin-gonic/gin"
"github.com/patsnapops/http-headers"
"net/http"
)
const SEP = ", "
// CORS (Cross-Origin Resource Sharing)
func CORS() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set(hh.AccessControlAllowOrigin, "*")
c.Writer.Header().Set(hh.AccessControlMaxAge, "86400")
c.Writer.Header().Set(hh.AccessControlAllowMethods,
http.MethodConnect+SEP+
http.MethodDelete+SEP+
http.MethodGet+SEP+
http.MethodHead+SEP+
http.MethodOptions+SEP+
http.MethodPost+SEP+
http.MethodPut)
c.Writer.Header().Set(hh.AccessControlAllowHeaders,
hh.XRequestedWith+SEP+
hh.ContentType+SEP+
hh.Origin+SEP+
hh.Authorization+SEP+
hh.Accept+SEP+
hh.AcceptEncoding+SEP)
c.Writer.Header().Set(hh.AccessControlExposeHeaders,
hh.CacheControl+SEP+
hh.ContentLanguage+SEP+
hh.ContentLength+SEP+
hh.ContentType+SEP+
hh.Expires+SEP+
hh.LastModified+SEP)
c.Writer.Header().Set(hh.AccessControlAllowCredentials, "true")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(200)
} else {
c.Next()
}
}
}