2024-10-18 14:41:55 +08:00
|
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
import (
|
2024-10-18 17:21:19 +08:00
|
|
|
|
"github.com/Superdanda/hade/framework/gin"
|
2024-10-18 14:41:55 +08:00
|
|
|
|
"log"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// recovery机制,将协程中的函数异常进行捕获
|
2024-10-18 17:21:19 +08:00
|
|
|
|
func Cost() gin.HandlerFunc {
|
2024-10-18 14:41:55 +08:00
|
|
|
|
// 使用函数回调
|
2024-10-18 17:21:19 +08:00
|
|
|
|
return func(c *gin.Context) {
|
2024-10-18 14:41:55 +08:00
|
|
|
|
// 记录开始时间
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
2024-10-18 17:21:19 +08:00
|
|
|
|
log.Printf("api uri start: %v", c.Request.RequestURI)
|
2024-10-18 14:41:55 +08:00
|
|
|
|
// 使用next执行具体的业务逻辑
|
|
|
|
|
c.Next()
|
|
|
|
|
|
|
|
|
|
// 记录结束时间
|
|
|
|
|
end := time.Now()
|
|
|
|
|
cost := end.Sub(start)
|
2024-10-18 17:21:19 +08:00
|
|
|
|
log.Printf("api uri end: %v, cost: %v", c.Request.RequestURI, cost.Seconds())
|
2024-10-18 14:41:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|