-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_cors.go
More file actions
35 lines (31 loc) · 1.01 KB
/
web_cors.go
File metadata and controls
35 lines (31 loc) · 1.01 KB
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
package lambda
import (
"context"
"github.com/aws/aws-lambda-go/events"
)
func CORSAllowHeaders(request events.APIGatewayV2HTTPRequest) map[string]string {
return map[string]string{
"Access-Control-Allow-Origin": GetHeaderParam(&request, "access-control-allow-methods", "*"),
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Methods": GetHeaderParam(&request, "access-control-allow-methods", "GET,POST,PUT,DELETE,OPTIONS"),
"Access-Control-Allow-Headers": GetHeaderParam(&request, "access-control-allow-origin", "*"),
}
}
func CORSFilter(next HandleFunc) HandleFunc {
return UseCORS(next)
}
func UseCORS(next HandleFunc) HandleFunc {
return func(ctx context.Context, req events.APIGatewayV2HTTPRequest) (*events.APIGatewayV2HTTPResponse, error) {
resp, err := next(ctx, req)
if err != nil {
return nil, err
}
if resp.Headers == nil {
resp.Headers = map[string]string{}
}
for k, v := range CORSAllowHeaders(req) {
resp.Headers[k] = v
}
return resp, nil
}
}