Merge branch 'refs/heads/feat-conf-redis'

This commit is contained in:
junleea 2024-12-21 17:17:00 +08:00
commit 3ab7556b38
4 changed files with 96 additions and 3 deletions

View File

@ -214,9 +214,14 @@ func GetRedis(c *gin.Context) {
//解析请求参数
var req SetRedisReq
if err := c.ShouldBind(&req); err == nil {
if req.Option == "one" {
code, message := service.GetToolRedis(req.Key)
req.Value = message
c.JSON(http.StatusOK, gin.H{"code": code, "message": message, "data": req})
} else if req.Option == "all" {
code, message, data := service.GetAllRedis()
c.JSON(http.StatusOK, gin.H{"code": code, "message": message, "data": data})
}
} else {
c.JSON(http.StatusOK, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
return

View File

@ -86,6 +86,27 @@ type ConfigStruct struct {
// 读取配置文件
func ReadConfig(path string) error {
//查看配置文件是否存在,不存在则创建
_, err := os.Stat(path)
if err != nil {
fmt.Println("Config file not found!")
//创建默认配置
DefaultConfig()
//写入json文件
file, err := os.Create(path)
if err != nil {
fmt.Println("Error creating config file")
return err
}
defer file.Close()
encoder := json.NewEncoder(file)
err = encoder.Encode(&Config)
if err != nil {
fmt.Println("Error encoding config")
}
return err
}
//读json文件
file, err := os.Open(path)
if err != nil {
@ -105,3 +126,26 @@ func ReadConfig(path string) error {
SigningKey = []byte(Config.TOKEN_SECRET)
return err
}
// 默认配置
func DefaultConfig() {
Config.DB = 2
Config.MYSQL_DSN = MYSQL_DSN
Config.PG_DSN = ""
Config.REDIS_ADDR = REDIS_ADDR
Config.TOKEN_USE_REDIS = false
Config.REDIS_User_PW = false
Config.REDIS_PASSWORD = REDIS_PASSWORD
Config.REDIS_DB = REIDS_DB
Config.TOKEN_SECRET = TOKEN_SECRET
Config.CID_BASE_DIR = CID_BASE_DIR
Config.FILE_BASE_DIR = FILE_BASE_DIR
Config.MONITOR = false
Config.SERVER_SQL_LOG = false
Config.SERVER_PORT = "8083"
Config.LOG_SAVE_DAYS = 7
Config.SERVER_USER_TYPE = "master"
Config.MASTER_SERVER_DOMAIN = ""
Config.USER_SYNC_TIME = 86400
Config.SERVER_NAME = "default"
}

View File

@ -68,3 +68,11 @@ func GetToolRedis(key string) (code int, message string) {
return proto.SuccessCode, val
}
}
func GetAllRedis() (code int, msg string, data []worker.RedisInfo) {
data, err := worker.GetAllRedisInfo()
if err != nil {
return proto.OperationFailed, err.Error(), nil
}
return proto.SuccessCode, "success", data
}

View File

@ -460,3 +460,39 @@ func GetRedisSetUnion(key1 string, key2 string) []string {
}
return val
}
type RedisInfo struct {
Key string
Value string
Expire time.Duration
}
// 获取所有的key和value,及其对应的过期时间
func GetAllRedisInfo() ([]RedisInfo, error) {
ctx := context.Background()
keys, err := RedisClient.Keys(ctx, "*").Result()
if err != nil {
fmt.Println("Error getting key: %v", err)
return nil, err
}
var redisInfos []RedisInfo
for _, key := range keys {
val, err := RedisClient.Get(ctx, key).Result()
if err != nil {
fmt.Println("Error getting key: %v", err)
return nil, err
}
expire, err := RedisClient.TTL(ctx, key).Result()
if err != nil {
fmt.Println("Error getting key: %v", err)
return nil, err
}
redisInfo := RedisInfo{
Key: key,
Value: val,
Expire: expire,
}
redisInfos = append(redisInfos, redisInfo)
}
return redisInfos, nil
}