-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d454d32
commit 2b33897
Showing
26 changed files
with
152 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package httputils | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
func GetIPAddress(headers http.Header) string { | ||
// ToDo | ||
// Use echo context.RealIP() | ||
ip := "127.0.0.1" | ||
if xOFF := headers.Get("X-Original-Forwarded-For"); xOFF != "" { | ||
i := strings.Index(xOFF, ", ") | ||
if i == -1 { | ||
i = len(xOFF) | ||
} | ||
ip = xOFF[:i] | ||
} else if xFF := headers.Get("X-Forwarded-For"); xFF != "" { | ||
i := strings.Index(xFF, ", ") | ||
if i == -1 { | ||
i = len(xFF) | ||
} | ||
ip = xFF[:i] | ||
} else if xrIP := headers.Get("X-Real-IP"); xrIP != "" { | ||
ip = xrIP | ||
} | ||
return ip | ||
} | ||
|
||
// Set values in ctx for | ||
// RequestID, IPAddress | ||
func SetDefaultHTTPCtx(ctx context.Context, headers http.Header) context.Context { | ||
// so we don't need echo here | ||
// requestID := headers.Get(echo.HeaderXRequestID) | ||
requestID := headers.Get("X-Request-Id") | ||
IP := GetIPAddress(headers) | ||
|
||
ctx = SetCtxValue(ctx, KeyRequestID, requestID) | ||
ctx = SetCtxValue(ctx, KeyIPAddress, IP) | ||
return ctx | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/webdevelop-pro/go-common/httputils | ||
|
||
go 1.22.1 | ||
|
||
require github.com/pkg/errors v0.9.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= | ||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package logger | ||
|
||
type contextKey rune | ||
|
||
const LogInfoKey contextKey = iota |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.