-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhandler.cors.go
More file actions
35 lines (31 loc) · 969 Bytes
/
handler.cors.go
File metadata and controls
35 lines (31 loc) · 969 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
package main
import (
"fmt"
"net/http"
"strings"
)
// corsHandler handles checking CORS options and
// making sure they are valid before continuing to
// process a HTTP request
func corsHandler(cors *routeCORS) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("[cors] sending back headers ...")
if cors == nil {
log.Println("[cors] skipping ...")
return
}
w.Header().Set("Access-Control-Allow-Origin", cors.AllowOrigin)
if cors.AllowMethods != nil {
w.Header().Set("Access-Control-Allow-Methods", strings.Join(cors.AllowMethods, ", "))
}
if cors.AllowHeaders != nil {
w.Header().Set("Access-Control-Allow-Headers", strings.Join(cors.AllowHeaders, ", "))
}
if cors.AllowCredentials != nil {
w.Header().Set("Access-Control-Allow-Credentials", fmt.Sprint(*cors.AllowCredentials))
}
if cors.MaxAge != nil {
w.Header().Set("Access-Control-Allow-Max-Age", fmt.Sprint(*cors.MaxAge))
}
}
}