Skip to content

Commit f2d4b34

Browse files
committed
small improvements
1 parent 0c1e849 commit f2d4b34

File tree

3 files changed

+12
-28
lines changed

3 files changed

+12
-28
lines changed

context/keys/keys.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const (
2020

2121
RequestIDStr ContextStr = "X-Request-Id"
2222
IPAddressStr ContextStr = "IP-Address"
23+
24+
XOriginalForwardedFor ContextStr = "X-Original-Forwarded-For"
25+
XForwardedFor ContextStr = "X-Forwarded-For"
26+
XRealIP ContextStr = "X-Real-IP"
2327
)
2428

2529
func GetAsString(ctx context.Context, key any) string {

httputils/context.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@ package httputils
33
import (
44
"net/http"
55
"strings"
6+
7+
"github.com/webdevelop-pro/go-common/context/keys"
68
)
79

810
func GetIPAddress(headers http.Header) string {
9-
// ToDo
10-
// Use echo context.RealIP()
1111
ip := "127.0.0.1"
12-
if xOFF := headers.Get("X-Original-Forwarded-For"); xOFF != "" {
12+
if xOFF := headers.Get(keys.XOriginalForwardedFor); xOFF != "" {
1313
i := strings.Index(xOFF, ", ")
1414
if i == -1 {
1515
i = len(xOFF)
1616
}
1717
ip = xOFF[:i]
18-
} else if xFF := headers.Get("X-Forwarded-For"); xFF != "" {
18+
} else if xFF := headers.Get(keys.XForwardedFor); xFF != "" {
1919
i := strings.Index(xFF, ", ")
2020
if i == -1 {
2121
i = len(xFF)
2222
}
2323
ip = xFF[:i]
24-
} else if xrIP := headers.Get("X-Real-IP"); xrIP != "" {
24+
} else if xrIP := headers.Get(keys.XRealIP); xrIP != "" {
2525
ip = xrIP
2626
}
2727
return ip

queue/pclient/utils.go

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,15 @@ package pclient
33
import (
44
"context"
55
"net/http"
6-
"strings"
76

87
"github.com/webdevelop-pro/go-common/context/keys"
8+
"github.com/webdevelop-pro/go-common/httputils"
99
"github.com/webdevelop-pro/go-common/logger"
1010
"github.com/webdevelop-pro/go-common/verser"
1111
)
1212

1313
const HeaderXRequestID = "X-Request-Id"
1414

15-
func GetIPAddress(headers http.Header) string {
16-
ip := "127.0.0.1"
17-
if xOFF := headers.Get("X-Original-Forwarded-For"); xOFF != "" {
18-
i := strings.Index(xOFF, ", ")
19-
if i == -1 {
20-
i = len(xOFF)
21-
}
22-
ip = xOFF[:i]
23-
} else if xFF := headers.Get("X-Forwarded-For"); xFF != "" {
24-
i := strings.Index(xFF, ", ")
25-
if i == -1 {
26-
i = len(xFF)
27-
}
28-
ip = xFF[:i]
29-
} else if xrIP := headers.Get("X-Real-IP"); xrIP != "" {
30-
ip = xrIP
31-
}
32-
return ip
33-
}
34-
3515
func SetDefaultEventCtx(ctx context.Context, event Event) context.Context {
3616
ctx = keys.SetCtxValue(ctx, keys.RequestID, event.RequestID)
3717
ctx = keys.SetCtxValue(ctx, keys.IPAddress, event.IPAddress)
@@ -55,8 +35,8 @@ func SetDefaultEventCtx(ctx context.Context, event Event) context.Context {
5535
func SetDefaultWebhookCtx(ctx context.Context, webhook Webhook) context.Context {
5636
headers := http.Header(webhook.Headers)
5737

58-
requestID := headers.Get(HeaderXRequestID)
59-
IP := GetIPAddress(headers)
38+
requestID := headers.Get(keys.RequestIDStr)
39+
IP := httputils.GetIPAddress(headers)
6040

6141
ctx = keys.SetCtxValue(ctx, keys.RequestID, requestID)
6242
ctx = keys.SetCtxValue(ctx, keys.IPAddress, IP)

0 commit comments

Comments
 (0)