-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcors.go
45 lines (36 loc) · 1.09 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
package nautilus
import (
"strings"
"github.com/kataras/iris/v12"
)
// CORS is struct containing data of CORS related headers.
type CORS struct {
AllowedOrigins []string
AllowedMethods []string
AllowedHeaders []string
DisableCredentials bool
}
// Handle is a middleware for adding CORS headers in response.
func (c CORS) Handle(context iris.Context) {
origins := "*"
methods := "GET,HEAD,OPTIONS,POST,PUT,PATCH,DELETE"
headers := "Accept,Authorization,Cache-Control,Content-Type,X-Requested-With"
allowCredentials := "true"
if len(c.AllowedOrigins) > 0 {
origins = strings.Join(c.AllowedOrigins, ",")
}
if len(c.AllowedMethods) > 0 {
methods = strings.Join(c.AllowedMethods, ",")
}
if len(c.AllowedHeaders) > 0 {
headers = strings.Join(c.AllowedHeaders, ",")
}
if c.DisableCredentials {
allowCredentials = "false"
}
context.Header("Access-Control-Allow-Origin", origins)
context.Header("Access-Control-Allow-Methods", methods)
context.Header("Access-Control-Allow-Headers", headers)
context.Header("Access-Control-Allow-Credentials", allowCredentials)
context.Next()
}