package appconfig import ( "net/http" "backend/internal/dto" appconfigservice "backend/internal/service/appconfig" "github.com/gin-gonic/gin" "gorm.io/gorm" ) // Controller 运行配置中心控制器(仅管理员,挂在 JWT 保护组下) // Logan.Li type Controller struct { svc *appconfigservice.Service } // NewController 创建控制器(复用全局已装载的服务实例,保证读写同一份缓存) // Logan.Li func NewController(db *gorm.DB) *Controller { return &Controller{svc: appconfigservice.Standard(db)} } // Overview 读取全部配置(机密项掩码) // Logan.Li func (ctrl *Controller) Overview(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"code": 0, "data": ctrl.svc.Overview()}) } // Update 批量保存配置 // Logan.Li func (ctrl *Controller) Update(c *gin.Context) { var req dto.RuntimeConfigUpdate if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"}) return } data, err := ctrl.svc.Update(req.Values, c.GetString("username"), c.ClientIP()) if err != nil { c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"code": 0, "message": "运行配置已保存", "data": data}) } // Reset 恢复默认(删除页面值) // Logan.Li func (ctrl *Controller) Reset(c *gin.Context) { var req dto.RuntimeResetRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"}) return } data, err := ctrl.svc.Reset(req.Keys, c.GetString("username"), c.ClientIP()) if err != nil { c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"code": 0, "message": "已恢复环境变量/默认值", "data": data}) }