func createApp(w http.ResponseWriter, r *http.Request, t *auth.Token) error { var a app.App var japp jsonApp defer r.Body.Close() body, err := ioutil.ReadAll(r.Body) if err != nil { return err } if err = json.Unmarshal(body, &japp); err != nil { return err } a.Name = japp.Name a.Framework = japp.Framework if japp.Units == 0 { japp.Units = 1 } u, err := t.User() if err != nil { return err } teams, err := u.Teams() if err != nil { return err } if len(teams) < 1 { msg := "In order to create an app, you should be member of at least one team" return &errors.Http{Code: http.StatusForbidden, Message: msg} } err = app.CreateApp(&a, japp.Units, teams) if err != nil { log.Printf("Got error while creating app: %s", err) if e, ok := err.(*errors.ValidationError); ok { return &errors.Http{Code: http.StatusPreconditionFailed, Message: e.Message} } if strings.Contains(err.Error(), "key error") { msg := fmt.Sprintf(`There is already an app named "%s".`, a.Name) return &errors.Http{Code: http.StatusConflict, Message: msg} } return err } msg := map[string]string{ "status": "success", "repository_url": repository.GetUrl(a.Name), } jsonMsg, err := json.Marshal(msg) if err != nil { return err } fmt.Fprintf(w, "%s", jsonMsg) return nil }
func CreateAppHandler(w http.ResponseWriter, r *http.Request, u *auth.User) error { var app app.App var japp jsonApp defer r.Body.Close() body, err := ioutil.ReadAll(r.Body) if err != nil { return err } if err = json.Unmarshal(body, &japp); err != nil { return err } app.Name = japp.Name app.Framework = japp.Framework if japp.Units == 0 { japp.Units = 1 } jsonMsg, err := createAppHelper(&app, u, japp.Units) if err != nil { return err } fmt.Fprint(w, string(jsonMsg)) return nil }