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
Showing
3 changed files
with
142 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/zhufuyi/pkg/krand" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
const ( | ||
// ContextRequestIDKey context request id for context | ||
ContextRequestIDKey = "request_id" | ||
|
||
// HeaderXRequestIDKey http header request ID key | ||
HeaderXRequestIDKey = "X-Request-ID" | ||
) | ||
|
||
// RequestID is a middleware that injects a 'X-Request-ID' into the context and request/response header of each request. | ||
func RequestID() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
// Check for incoming header, use it if exists | ||
requestID := c.Request.Header.Get(HeaderXRequestIDKey) | ||
|
||
// Create request id | ||
if requestID == "" { | ||
requestID = krand.String(krand.R_All, 12) // 生成长度为12的随机字符串 | ||
c.Request.Header.Set(HeaderXRequestIDKey, requestID) | ||
// Expose it for use in the application | ||
c.Set(ContextRequestIDKey, requestID) | ||
} | ||
|
||
// Set X-Request-ID header | ||
c.Writer.Header().Set(HeaderXRequestIDKey, requestID) | ||
|
||
c.Next() | ||
} | ||
} | ||
|
||
// GetRequestIDFromContext returns 'RequestID' from the given context if present. | ||
func GetRequestIDFromContext(c *gin.Context) string { | ||
if v, ok := c.Get(ContextRequestIDKey); ok { | ||
if requestID, ok := v.(string); ok { | ||
return requestID | ||
} | ||
} | ||
|
||
return "" | ||
} | ||
|
||
// GetRequestIDFromHeaders returns 'RequestID' from the headers if present. | ||
func GetRequestIDFromHeaders(c *gin.Context) string { | ||
return c.Request.Header.Get(HeaderXRequestIDKey) | ||
} |
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,13 @@ | ||
package utils | ||
|
||
import ( | ||
"github.com/zhufuyi/pkg/gin/middleware" | ||
|
||
"github.com/gin-gonic/gin" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// RequestID zap logger request ID | ||
func RequestID(c *gin.Context) zap.Field { | ||
return zap.String(middleware.ContextRequestIDKey, middleware.GetRequestIDFromContext(c)) | ||
} |
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,77 @@ | ||
package utils | ||
|
||
import "strconv" | ||
|
||
// StrToInt string to int | ||
func StrToInt(str string) int { | ||
v, _ := strconv.Atoi(str) | ||
return v | ||
} | ||
|
||
// StrToIntE string to int with error | ||
func StrToIntE(str string) (int, error) { | ||
return strconv.Atoi(str) | ||
} | ||
|
||
// StrToUint32 string to uint32 | ||
func StrToUint32(str string) uint32 { | ||
v, _ := strconv.ParseUint(str, 10, 64) | ||
return uint32(v) | ||
} | ||
|
||
// StrToUint32E string to uint32 with error | ||
func StrToUint32E(str string) (uint32, error) { | ||
v, err := strconv.ParseUint(str, 10, 64) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return uint32(v), nil | ||
} | ||
|
||
// StrToUint64 string to uint64 | ||
func StrToUint64(str string) uint64 { | ||
v, _ := strconv.ParseUint(str, 10, 64) | ||
return v | ||
} | ||
|
||
// StrToUint64E string to uint64 with error | ||
func StrToUint64E(str string) (uint64, error) { | ||
return strconv.ParseUint(str, 10, 64) | ||
} | ||
|
||
// StrToFloat32 string to float32 | ||
func StrToFloat32(str string) float32 { | ||
v, _ := strconv.ParseFloat(str, 32) | ||
return float32(v) | ||
} | ||
|
||
// StrToFloat32E string to float32 with error | ||
func StrToFloat32E(str string) (float32, error) { | ||
v, err := strconv.ParseFloat(str, 32) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return float32(v), nil | ||
} | ||
|
||
// StrToFloat64 string to float64 | ||
func StrToFloat64(str string) float64 { | ||
v, _ := strconv.ParseFloat(str, 64) | ||
return v | ||
} | ||
|
||
// StrToFloat64E string to float64 with error | ||
func StrToFloat64E(str string) (float64, error) { | ||
return strconv.ParseFloat(str, 64) | ||
} | ||
|
||
// IntToStr int to string | ||
func IntToStr(v int) string { | ||
return strconv.Itoa(v) | ||
} | ||
|
||
// Uint64ToStr uint64 to string | ||
func Uint64ToStr(v uint64) string { | ||
return strconv.FormatUint(v, 10) | ||
} |