func UpdateApp(c *gin.Context) { confWriteMux.Lock() defer confWriteMux.Unlock() var data struct { Key string `json:"key" binding:"required"` Name string `json:"name" binding:"required"` Type string `json:"type" binding:"required"` AuxInfo string `json:"aux_info"` } if err := c.BindJSON(&data); err != nil { Error(c, BAD_POST_DATA, err.Error()) return } if !models.IsValidAppType(data.Type) { Error(c, BAD_REQUEST, "unknown app type: "+data.Type) return } oldApp := memConfApps[data.Key] if oldApp == nil { Error(c, BAD_REQUEST, "app key not exists: "+data.Key) return } if oldApp.Name == data.Name && oldApp.AuxInfo == data.AuxInfo && oldApp.Type == data.Type { Success(c, nil) return } if oldApp.Type == models.APP_TYPE_TEMPLATE && oldApp.Type != data.Type { Error(c, BAD_REQUEST, "can not change template app to real app") return } if memConfApps[data.Key].Name != data.Name { for _, app := range memConfApps { if app.Name == data.Name { Error(c, BAD_REQUEST, "appname already exists: "+data.Name) return } } } app := *oldApp app.Name = data.Name app.AuxInfo = data.AuxInfo if _, err := updateApp(&app, nil, nil); err != nil { Error(c, SERVER_ERROR, err.Error()) return } failedNodes := syncData2SlaveIfNeed(&app, getOpUserKey(c)) if len(failedNodes) > 0 { Success(c, map[string]interface{}{"failed_nodes": failedNodes}) } else { Success(c, nil) } }
func NewApp(c *gin.Context) { confWriteMux.Lock() defer confWriteMux.Unlock() var data struct { Name string `json:"name" binding:"required"` Type string `json:"type" binding:"required"` AuxInfo string `json:"aux_info"` } if err := c.BindJSON(&data); err != nil { Error(c, BAD_POST_DATA, err.Error()) return } if !models.IsValidAppType(data.Type) { Error(c, BAD_REQUEST, "unknown app type: "+data.Type) return } if memConfAppsByName[data.Name] != nil { Error(c, BAD_REQUEST, "appname already exists: "+data.Name) return } app := &models.App{ Key: utils.GenerateKey(), Name: data.Name, UserKey: getOpUserKey(c), Type: data.Type, AuxInfo: data.AuxInfo, CreatedUTC: utils.GetNowSecond(), } if _, err := updateApp(app, nil, nil); err != nil { Error(c, SERVER_ERROR, err.Error()) return } failedNodes := syncData2SlaveIfNeed(app, getOpUserKey(c)) if len(failedNodes) > 0 { Success(c, map[string]interface{}{"failed_nodes": failedNodes}) } else { Success(c, nil) } }