初始化v1
This commit is contained in:
33
utility/database/check.go
Normal file
33
utility/database/check.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"nl-video-api/utility/response"
|
||||
)
|
||||
|
||||
// CheckConnection 检查数据库连接
|
||||
func CheckConnection(ctx context.Context) error {
|
||||
return g.DB().PingMaster()
|
||||
}
|
||||
|
||||
// CheckConnectionMiddleware 数据库连接检查中间件
|
||||
func CheckConnectionMiddleware(r *ghttp.Request) {
|
||||
if err := CheckConnection(r.Context()); err != nil {
|
||||
g.Log().Error(r.Context(), "数据库连接失败:", err)
|
||||
response.Error(r, response.CodeInternalError, "数据库服务暂时不可用,请稍后重试")
|
||||
return
|
||||
}
|
||||
r.Middleware.Next()
|
||||
}
|
||||
|
||||
// WithDBCheck 为控制器方法添加数据库连接检查
|
||||
func WithDBCheck(r *ghttp.Request) bool {
|
||||
if err := CheckConnection(r.Context()); err != nil {
|
||||
g.Log().Error(r.Context(), "数据库连接失败:", err)
|
||||
response.Error(r, response.CodeInternalError, "数据库服务暂时不可用,请稍后重试")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user