feat: 添加认证、日志、响应等基础中间件与服务
This commit is contained in:
1 parent
16a9f64109
commit
329a22f0b1
30 files changed
+1835
-12
No files matched your search
@@ -0,0 +1,41 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"EnGin/internal/global"
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// Redis 统一导出的 Redis 客户端实例
|
||||
var Redis *redis.Client
|
||||
|
||||
// InitRedis 初始化 Redis 连接
|
||||
func InitRedis(addr string, password string, dbIndex int) error {
|
||||
if addr == "" {
|
||||
return nil
|
||||
}
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: addr,
|
||||
Password: password,
|
||||
DB: dbIndex,
|
||||
})
|
||||
// 立即 Ping 一下,验证连接是否成功
|
||||
_, err := rdb.Ping(context.Background()).Result()
|
||||
if err != nil {
|
||||
return fmt.Errorf("连接 Redis 失败: %w", err)
|
||||
}
|
||||
|
||||
Redis = rdb
|
||||
global.Log.Info("Redis 初始化成功")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CloseRedis 关闭 Redis 连接
|
||||
func CloseRedis() {
|
||||
if Redis != nil {
|
||||
Redis.Close()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user