feat: Redis cluster support + version auto-detect + metrics API

This commit is contained in:
logan committed 2026-05-30 01:44:58 +08:00
1 parent 8384006ae9
commit 3af65f2350
10 files changed
+485 -66

No files matched your search

+51 -4
View File
@@ -68,6 +68,20 @@ func CreateInstance(c *gin.Context) {
c.JSON(http.StatusInternalServerError, gin.H{"detail": err.Error()})
return
}
// Auto-detect version and cluster_mode after creation
go func() {
result := testDBConnectionFull(&instance)
updates := map[string]interface{}{}
if result.Success && result.Version != "" {
updates["version"] = result.Version
}
if result.ClusterMode {
updates["cluster_mode"] = true
}
if len(updates) > 0 {
model.DB.Model(&model.Instance{}).Where("id = ?", instance.ID).Updates(updates)
}
}()
instance.Password = "******"
c.JSON(http.StatusCreated, instance)
}
@@ -121,11 +135,22 @@ func TestConnection(c *gin.Context) {
return
}
success, message := testDBConnection(&instance)
if success {
c.JSON(http.StatusOK, gin.H{"success": true, "message": message})
result := testDBConnectionFull(&instance)
// Update version & cluster_mode in DB if detected
updates := map[string]interface{}{}
if result.Success && result.Version != "" {
updates["version"] = result.Version
}
if result.ClusterMode {
updates["cluster_mode"] = true
}
if len(updates) > 0 {
model.DB.Model(&model.Instance{}).Where("id = ?", instance.ID).Updates(updates)
}
if result.Success {
c.JSON(http.StatusOK, gin.H{"success": true, "message": result.Message, "version": result.Version, "cluster_mode": result.ClusterMode})
} else {
c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": message})
c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": result.Message})
}
}
@@ -152,3 +177,25 @@ func parseID(s string) (int, bool) {
id, err := strconv.Atoi(s)
return id, err == nil
}
// GET /api/v1/instances/:id/metrics — live metrics for an instance
func InstanceMetrics(c *gin.Context) {
id := c.Param("id")
var instance model.Instance
if err := model.DB.First(&instance, id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"detail": "Instance not found"})
return
}
switch instance.DBType {
case "redis":
metrics := getRedisMetrics(&instance)
c.JSON(http.StatusOK, metrics)
default:
// For MySQL/PG: return basic status
c.JSON(http.StatusOK, gin.H{
"status": "online",
"version": instance.Version,
})
}
}