Exemple #1
0
func getDeviceList(w rest.ResponseWriter, r *rest.Request) {
	stats.QueryOnlineDevices()

	devInfoList := []devInfo{}
	r.ParseForm()
	dev_ids := r.FormValue("dev_ids")
	if dev_ids != "" {
		ids := strings.Split(dev_ids, ",")
		for _, id := range ids {
			if serverName, err := storage.Instance.CheckDevice(id); err == nil && serverName != "" {
				info := devInfo{
					Id: id,
				}
				devInfoList = append(devInfoList, info)
			}
		}
	} else {
		rest.Error(w, "Missing \"dev_ids\"", http.StatusBadRequest)
		return
	}

	resp := cloud.ApiResponse{}
	resp.ErrNo = cloud.ERR_NOERROR
	resp.Data = devInfoList
	w.WriteJson(resp)
}
Exemple #2
0
func getDevice(w rest.ResponseWriter, r *rest.Request) {
	devId := r.PathParam("devid")

	r.ParseForm()
	token := r.FormValue("token")
	if token == "" {
		rest.Error(w, "Missing \"token\"", http.StatusBadRequest)
		return
	}

	if !checkAuthz(token, devId) {
		log.Warnf("Auth failed. token: %s, device_id: %s", token, devId)
		rest.Error(w, "Authorization failed", http.StatusForbidden)
		return
	}

	stats.QueryDeviceInfo()

	if serverName, err := storage.Instance.CheckDevice(devId); err == nil && serverName != "" {
		resp := cloud.ApiResponse{}
		resp.ErrNo = cloud.ERR_NOERROR
		resp.Data = devInfo{
			Id: devId,
		}
		w.WriteJson(resp)
	} else {
		rest.NotFound(w, r)
		return
	}
}
// POST /files/delete
func FileDelete(w rest.ResponseWriter, r *rest.Request) {
	r.ParseForm()
	id, _ := strconv.ParseInt(r.FormValue("Id"), 10, 64)
	file := database.FindFile(id)

	// Delete from DB
	database.DeleteFile(file)
	w.WriteJson("")
}
// POST /releases/delete
func ReleaseDelete(w rest.ResponseWriter, r *rest.Request) {
	r.ParseForm()
	id, _ := strconv.ParseInt(r.FormValue("Id"), 10, 64)
	release := database.FindRelease(id)
	file := database.FindFile(release.FileId)

	// Delete from DB
	database.DeleteRelease(release)
	database.UnpublishFile(file)
	go database.RefreshBuilds()

	// http.Redirect(w, r, "/admin/releases", http.StatusFound)
	w.WriteJson("")
}
Exemple #5
0
func getAppStats(w rest.ResponseWriter, r *rest.Request) {
	r.ParseForm()
	appId := r.FormValue("appid")
	startDate := r.FormValue("start_date")
	endDate := r.FormValue("end_date")
	if appId == "" {
		rest.Error(w, "missing 'appid'", http.StatusBadRequest)
		return
	}

	var (
		start time.Time
		end   time.Time
		err   error
	)
	if startDate == "" {
		start = time.Now()
	} else {
		if start, err = time.Parse("20060102", startDate); err != nil {
			rest.Error(w, "invalid date format", http.StatusBadRequest)
			return
		}
	}
	if endDate == "" {
		end = time.Now()
	} else {
		if end, err = time.Parse("20060102", endDate); err != nil {
			rest.Error(w, "invalid date format", http.StatusBadRequest)
			return
		}
	}
	if start.After(end) {
		rest.Error(w, "start date greater than end date", http.StatusBadRequest)
		return
	}

	resp := Response{
		ErrNo: 0,
	}
	resp.Data, err = storage.Instance.GetAppStats(appId, start, end)
	if err != nil {
		rest.Error(w, "storage I/O failed", http.StatusInternalServerError)
		log.Warnf("GetAppStats failed: %s", err.Error())
		return
	}
	w.WriteJson(resp)
}
Exemple #6
0
/* API: This returns the file content of given file in project directory
> curl -i http://127.0.0.1:3000/api/files/open?t=
*/
func FileOpenHandler(w rest.ResponseWriter, r *rest.Request) {
	r.ParseForm()
	target := r.Form.Get("target")

	if target == "" {
		rest.Error(w, "Missing target file.", http.StatusBadRequest)
		return
	}

	content, err := ioutil.ReadFile(projectDir + target)
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.WriteJson(&Editable{target, fmt.Sprintf("%s", content), []string{}})
}
Exemple #7
0
func getRouterList(w rest.ResponseWriter, r *rest.Request) {
	type RouterInfo struct {
		Rid   string `json:"rid"`
		Rname string `json:"rname"`
	}

	type ResponseRouterList struct {
		Status int          `json:"status"`
		Descr  string       `json:"descr"`
		List   []RouterInfo `json:"list"`
	}

	resp := ResponseRouterList{}
	resp.Status = STATUS_OTHER_ERR
	r.ParseForm()

	uid := r.FormValue("uid")
	if uid == "" {
		resp.Status = STATUS_INVALID_PARAM
		resp.Descr = "missing 'uid'"
		w.WriteJson(resp)
		return
	}

	devices, err := devcenter.GetDevices(uid, devcenter.DEV_ROUTER)
	if err != nil {
		log.Errorf("GetDevices failed: %s", err.Error())
		resp.Descr = err.Error()
		w.WriteJson(resp)
		return
	}

	for _, dev := range devices {
		router := RouterInfo{
			Rid:   dev.Id,
			Rname: dev.Title,
		}
		resp.List = append(resp.List, router)
	}

	resp.Status = 0
	resp.Descr = "OK"
	w.WriteJson(resp)
}
// POST /releases/update
func ReleaseUpdate(w rest.ResponseWriter, r *rest.Request) {
	r.ParseForm()

	// Parse form and append to struct
	id, _ := strconv.ParseInt(r.FormValue("Id"), 10, 64)
	fileId, _ := strconv.ParseInt(r.FormValue("FileId"), 10, 64)
	release := database.FindRelease(id)
	file := database.FindFile(fileId)
	release.FileId = file.Id
	release.FileName = file.Name
	release.Channel = r.FormValue("Channel")
	release.Changelog = r.FormValue("Changelog")

	// Append to db
	database.UpdateRelease(release)

	// Redirect
	w.WriteJson("")
}
// POST /releases/create
func ReleaseCreate(w rest.ResponseWriter, r *rest.Request) {
	r.ParseForm()
	fileId, _ := strconv.ParseInt(r.FormValue("FileId"), 10, 64)
	file := database.FindFile(fileId)

	// Generate release
	release := database.Release{
		Created:   time.Now().UnixNano(),
		Changelog: r.FormValue("Changelog"),
		Channel:   r.FormValue("Channel"),
		FileId:    file.Id,
		FileName:  file.Name,
	}

	database.CreateRelease(release)
	database.PublishFile(file)
	go database.RefreshBuilds()
	w.WriteJson("")
}