-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
52 lines (43 loc) · 1.15 KB
/
utils.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
package main
import (
"os"
"strings"
"regexp"
"net/url"
"net/http"
)
func GetEnv(env string, defaultValue string) string {
value := os.Getenv(env)
if value == "" {
value = defaultValue
}
return value
}
var slugRe = regexp.MustCompile("[^a-z0-9]+")
func slug(s string) string {
return strings.Trim(slugRe.ReplaceAllString(strings.ToLower(s), "-"), "-")
}
func SetCorsHeaders(res http.ResponseWriter, req *http.Request, cors string, methods string, headers string) {
if cors == "*" && len(req.Header["Referer"]) != 0 {
referer, err := url.Parse(req.Header["Referer"][0])
if err == nil {
cors = referer.Scheme + "://" + referer.Host
}
}
if methods == "" {
methods = "GET,POST,PUT,DELETE"
}
res.Header().Set("Access-Control-Allow-Origin", cors)
res.Header().Set("Access-Control-Allow-Methods", methods)
if headers != "" {
res.Header().Set("Access-Control-Allow-Headers", headers)
}
}
func sliceContains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}