func addApp2(w http.ResponseWriter, r *http.Request) { var response Response var data map[string]string if err := json.NewDecoder(r.Body).Decode(&data); err != nil { response.ErrNo = ERR_BAD_REQUEST response.ErrMsg = "Bad request" b, _ := json.Marshal(response) http.Error(w, string(b), 400) return } pkg, ok1 := data["pkg"] name, ok2 := data["name"] mobile, ok3 := data["mobile"] email, ok4 := data["email"] desc, ok5 := data["desc"] if !ok1 || !ok2 || !ok3 || !ok4 || !ok5 { errResponse(w, ERR_INVALID_PARAMS, "missing parameters", 400) return } tprefix := getPappID() if tprefix == 0 { errResponse(w, ERR_INTERNAL, "no avaiable appid", 500) return } n, err := storage.Instance.HashExists("db_packages", pkg) if err != nil { errResponse(w, ERR_INTERNAL, "storage I/O failed", 500) return } if n > 0 { errResponse(w, ERR_EXIST, "package exist", 400) return } prefix := strconv.FormatInt(tprefix, 10) tappid := strings.Replace(uuid.New(), "-", "", -1) appId := "id_" + tappid[0:(len(tappid)-len(prefix))] + prefix appKey := "ak_" + utils.RandomAlphabetic(20) appSec := "sk_" + utils.RandomAlphabetic(20) rawapp := storage.RawApp{ Pkg: pkg, Name: name, Mobile: mobile, Email: email, Desc: desc, AppKey: appKey, AppSec: appSec, } b, _ := json.Marshal(rawapp) if _, err := storage.Instance.HashSet("db_apps", appId, b); err != nil { errResponse(w, ERR_INTERNAL, "set 'db_apps' failed", 500) return } if _, err := storage.Instance.HashSet("db_packages", pkg, []byte(appId)); err != nil { errResponse(w, ERR_INTERNAL, "set 'db_packages' failed", 500) return } response.ErrNo = 0 response.Data = map[string]string{ "appid": appId, "appkey": appKey, "appsec": appSec, } b, _ = json.Marshal(response) fmt.Fprintf(w, string(b)) }
func addApp(w http.ResponseWriter, r *http.Request) { var response Response var data map[string]string if err := json.NewDecoder(r.Body).Decode(&data); err != nil { response.ErrNo = ERR_BAD_REQUEST response.ErrMsg = "Bad request" b, _ := json.Marshal(response) http.Error(w, string(b), 400) return } pkg, pkg_ok := data["pkg"] if !pkg_ok { errResponse(w, ERR_INVALID_PARAMS, "missing 'pkg'", 400) return } token, ok := data["token"] if !ok { errResponse(w, ERR_INVALID_PARAMS, "missing 'token'", 400) return } ok, uid := auth.Instance.Auth(token) if !ok { errResponse(w, ERR_AUTHENTICATE, "authenticate failed", 401) return } tprefix := getPappID() if tprefix == 0 { errResponse(w, ERR_INTERNAL, "no avaiable appid", 500) return } n, err := storage.Instance.HashExists("db_packages", pkg) if err != nil { errResponse(w, ERR_INTERNAL, "storage I/O failed", 500) return } if n > 0 { errResponse(w, ERR_EXIST, "package exist", 400) return } prefix := strconv.FormatInt(tprefix, 10) tappid := strings.Replace(uuid.New(), "-", "", -1) appId := "appid_" + tappid[0:(len(tappid)-len(prefix))] + prefix appKey := "appkey_" + utils.RandomAlphabetic(20) appSec := "appsec_" + utils.RandomAlphabetic(20) rawapp := storage.RawApp{ Pkg: pkg, UserId: uid, AppKey: appKey, AppSec: appSec, } b, _ := json.Marshal(rawapp) if _, err := storage.Instance.HashSet("db_apps", appId, b); err != nil { errResponse(w, ERR_INTERNAL, "set 'db_apps' failed", 500) return } if _, err := storage.Instance.HashSet("db_packages", pkg, []byte(appId)); err != nil { errResponse(w, ERR_INTERNAL, "set 'db_packages' failed", 500) return } response.ErrNo = 0 response.Data = map[string]string{ "appid": appId, "appkey": appKey, "appsec": appSec, } b, _ = json.Marshal(response) fmt.Fprintf(w, string(b)) }