// MarshalJSON marshals the app in json format. It returns a JSON object with // the following keys: name, framework, teams, units, repository and ip. func (app *App) MarshalJSON() ([]byte, error) { result := make(map[string]interface{}) result["name"] = app.Name result["platform"] = app.Platform result["teams"] = app.Teams result["units"] = app.Units() result["repository"] = repository.ReadWriteURL(app.Name) result["ip"] = app.Ip result["cname"] = app.CName result["ready"] = app.State == "ready" result["owner"] = app.Owner result["deploys"] = app.Deploys result["memory"] = strconv.Itoa(app.Memory) result["swap"] = "0" if app.Swap > 0 { result["swap"] = strconv.Itoa(app.Swap - app.Memory) } return json.Marshal(&result) }
func createApp(w http.ResponseWriter, r *http.Request, t *auth.Token) error { var a app.App defer r.Body.Close() body, err := ioutil.ReadAll(r.Body) if err != nil { return err } if err = json.Unmarshal(body, &a); err != nil { return err } u, err := t.User() if err != nil { return err } rec.Log(u.Email, "create-app", "name="+a.Name, "platform="+a.Platform, "memory="+strconv.Itoa(a.Memory), "swap="+strconv.Itoa(a.Swap)) canSetMem, _ := config.GetBool("docker:allow-memory-set") if !canSetMem && (a.Memory > 0 || a.Swap > 0) { err := "Memory setting not allowed." log.Errorf("%s", err) return &errors.HTTP{Code: http.StatusForbidden, Message: err} } maxMem, _ := config.GetInt("docker:max-allowed-memory") if maxMem > 0 && a.Memory > maxMem { err := fmt.Sprintf("Invalid memory size. You cannot request more than %dMB.", maxMem) log.Errorf("%s", err) return &errors.HTTP{Code: http.StatusForbidden, Message: err} } maxSwap, _ := config.GetInt("docker:max-allowed-swap") if maxSwap > 0 && a.Swap > maxSwap { err := fmt.Sprintf("Invalid swap size. You cannot request more than %dMB.", maxSwap) log.Errorf("%s", err) return &errors.HTTP{Code: http.StatusForbidden, Message: err} } err = app.CreateApp(&a, u) if err != nil { log.Errorf("Got error while creating app: %s", err) if e, ok := err.(*errors.ValidationError); ok { return &errors.HTTP{Code: http.StatusBadRequest, Message: e.Message} } if _, ok := err.(app.NoTeamsError); ok { return &errors.HTTP{ Code: http.StatusBadRequest, Message: "In order to create an app, you should be member of at least one team", } } if e, ok := err.(*app.AppCreationError); ok { if e.Err == app.ErrAppAlreadyExists { return &errors.HTTP{Code: http.StatusConflict, Message: e.Error()} } if _, ok := e.Err.(*quota.QuotaExceededError); ok { return &errors.HTTP{ Code: http.StatusForbidden, Message: "Quota exceeded", } } } return err } msg := map[string]string{ "status": "success", "repository_url": repository.ReadWriteURL(a.Name), "ip": a.Ip, } jsonMsg, err := json.Marshal(msg) if err != nil { return err } fmt.Fprintf(w, "%s", jsonMsg) return nil }