u
This commit is contained in:
1 parent
2d036f4d95
commit
f373cf956d
22 files changed
+568
-280
No files matched your search
@@ -6,13 +6,11 @@ import (
|
||||
"base-framework/pkg/router"
|
||||
"base-framework/pkg/utils/jwt"
|
||||
"errors"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Auth() router.HandlerFunc {
|
||||
return func(c *router.Context) {
|
||||
log.Println("[Auth] 开始鉴权")
|
||||
|
||||
tokenHeader := c.Header("Authorization")
|
||||
userIdHeader := c.Header("userId")
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"base-framework/pkg/logx"
|
||||
"base-framework/pkg/reponse"
|
||||
"base-framework/pkg/router"
|
||||
"base-framework/pkg/routinex"
|
||||
"base-framework/pkg/utils/strutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
@@ -34,15 +37,31 @@ func Error() router.HandlerFunc {
|
||||
lastErr := errs[len(errs)-1]
|
||||
stack := lastErr.StackTrace()
|
||||
location := extractBusinessLocation(stack)
|
||||
trackId := routinex.Get(logx.TrackID)
|
||||
|
||||
info := reponse.ErrorInfo{
|
||||
Method: c.Request.Method,
|
||||
Path: c.Request.URL.Path,
|
||||
Message: lastErr.Error(),
|
||||
Stack: stack,
|
||||
Location: location,
|
||||
var trackIDStr string
|
||||
if trackId != nil {
|
||||
trackIDStr = strutil.ToString(trackId) // 用你之前写的工具类
|
||||
}
|
||||
|
||||
var info = reponse.ErrorInfo{
|
||||
Method: c.Request.Method,
|
||||
Path: c.Request.URL.Path,
|
||||
Message: lastErr.Error(),
|
||||
//Stack: stack,
|
||||
Location: location,
|
||||
TrackId: trackIDStr,
|
||||
}
|
||||
|
||||
logx.Logger.Error(
|
||||
"系统异常",
|
||||
logx.String("trackID", trackIDStr),
|
||||
logx.String("method", c.Request.Method),
|
||||
logx.String("path", c.Request.URL.Path),
|
||||
logx.String("location", location),
|
||||
logx.String("stack", stack),
|
||||
)
|
||||
|
||||
reponse.Error(c).ErrorInfo(info).Send()
|
||||
c.Abort()
|
||||
}
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"base-framework/pkg/logx"
|
||||
"base-framework/pkg/router"
|
||||
"time"
|
||||
"base-framework/pkg/routinex"
|
||||
"base-framework/pkg/utils/uid"
|
||||
)
|
||||
|
||||
func Logger() router.HandlerFunc {
|
||||
return func(c *router.Context) {
|
||||
start := time.Now()
|
||||
// 执行后续中间件和处理器
|
||||
routinex.Set(logx.TrackID, uid.NextID())
|
||||
|
||||
c.Next()
|
||||
elapsed := time.Since(start)
|
||||
// 即使业务中断,这里也能执行,打印耗时
|
||||
println("耗时:", elapsed.String())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"base-framework/pkg/router"
|
||||
"base-framework/pkg/routinex"
|
||||
)
|
||||
|
||||
func Routinex() router.HandlerFunc {
|
||||
return func(c *router.Context) {
|
||||
defer routinex.Clear()
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user