-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
83 lines (71 loc) · 1.46 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package middleware
import (
"net"
"net/http"
"strings"
)
var xForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For")
var xRealIP = http.CanonicalHeaderKey("X-Real-IP")
func GetRealIP(r *http.Request) string {
var ip string
if xff := r.Header.Get(xForwardedFor); xff != "" {
i := strings.Index(xff, ", ")
if i == -1 {
i = len(xff)
}
ip = xff[:i]
} else if xrip := r.Header.Get(xRealIP); xrip != "" {
ip = xrip
} else {
ip, _, _ = net.SplitHostPort(r.RemoteAddr)
}
return ip
}
type Extensions map[string]bool
func (this Extensions) Update(news ...Extensions) Extensions {
this = this.Copy()
for _, v := range news {
if v != nil {
for k, v := range v {
this[k] = v
}
}
}
return this
}
func (this *Extensions) PtrUpdate(news ...Extensions) *Extensions {
*this = this.Update(news...)
return this
}
func (this Extensions) Copy() (m Extensions) {
m = Extensions{}
if m != nil {
for k, v := range this {
m[k] = v
}
}
return
}
func (this Extensions) UpdateStrings(values ...string) Extensions {
this = this.Copy()
for _, v := range values {
if v != "" {
switch v[0] {
case '+':
this[v[1:]] = true
case '-':
this[v[1:]] = false
default:
this[v] = true
}
}
}
return this
}
func (this *Extensions) PtrUpdateStrings(values ...string) *Extensions {
*this = this.UpdateStrings(values...)
return this
}
func StringsToExtensions(values ...string) (m Extensions) {
return m.UpdateStrings(values...)
}