Example #1
4
func preparePostData(start_idx, end_idx, app_id int, owner_uin string) (string, int) {
	// params
	params := sjson.New()
	params.Set("appId", app_id)
	params.Set("uin", owner_uin)
	params.Set("startNum", start_idx)
	params.Set("endNum", end_idx)
	params.Set("simplify", 1)
	params.Set("allType", 1)
	// interface data
	interface_data := sjson.New()
	interface_data.Set("interfaceName", "qcloud.Qcvm.getCvmList")
	interface_data.Set("para", params)
	// post data in json format
	post_data := sjson.New()
	// unix timestamp
	cur_time := time.Now().Unix()
	// random number as event id
	eventId := rand.New(rand.NewSource(cur_time)).Intn(6553500)
	post_data.Set("version", "1.0")
	post_data.Set("caller", "CGW")
	post_data.Set("callee", "CGW")
	post_data.Set("eventId", eventId)
	post_data.Set("timestamp", cur_time)
	post_data.Set("interface", interface_data)
	post_data.Set("postOperation", make([]string, 0))
	bytes, err := post_data.Encode()
	if err != nil {
		return "", 1
	}
	return string(bytes), 0
}
Example #2
0
func jsonCmd(w http.ResponseWriter, q *http.Request) {
	tabs := strings.Split(q.URL.Path, "/")
	cmd := tabs[len(tabs)-1]
	if cmd == "mem" {
		mem, err := core.Memory()
		if err != nil {
			log.Println(err)
			return
		}
		js := json.New()
		js.Set("free", fmt.Sprintf("%d", mem.Free/1024))
		js.Set("used", fmt.Sprintf("%d", mem.Used/1024))
		js.Set("total", fmt.Sprintf("%d", mem.Total/1024))
		jsstr, _ := js.Encode()
		w.Write(jsstr)
	} else if cmd == "cpu" {
		cpu, err := core.CPU()
		if err != nil {
			log.Println(err)
			return
		}
		js := json.New()
		js.Set("msg", cpu.Message)
		js.Set("usr", fmt.Sprintf("%.2f", cpu.Usr))
		js.Set("sys", fmt.Sprintf("%.2f", cpu.Sys))
		js.Set("idle", fmt.Sprintf("%.2f", cpu.Idle))
		jsstr, _ := js.Encode()
		w.Write(jsstr)
	} else {
		w.Write([]byte("unknow cmd"))
	}
}
Example #3
0
func newValues(ch *ChangeSet) (Values, error) {
	sj := simple.New()
	err := sj.UnmarshalJSON(ch.Data)
	if err != nil {
		return nil, err
	}
	return &jsonValues{ch, sj}, nil
}
Example #4
0
// Then chains the middleware and returns the final http.Handler.
//     New(m1, m2, m3).Then(h)
// is equivalent to:
//     m1(m2(m3(h)))
// When the request comes in, it will be passed to m1, then m2, then m3
// and finally, the given handler
// (assuming every middleware calls the following one).
//
// A chain can be safely reused by calling Then() several times.
//     stdStack := alice.New(ratelimitHandler, csrfHandler)
//     indexPipe = stdStack.Then(indexHandler)
//     authPipe = stdStack.Then(authHandler)
// Note that constructors are called on every call to Then()
// and thus several instances of the same middleware will be created
// when a chain is reused in this way.
// For proper middleware, this should cause no problems.
//
// Then() treats nil as http.DefaultServeMux.
func (c Chain) Then(h *js.Json) (*js.Json, error) {
	var final *js.Json
	if h != nil {
		final = h
	} else {
		final = js.New()
	}
	var err error
	for i := len(c.constructors) - 1; i >= 0; i-- {
		final, err = c.constructors[i](final, err)
	}

	return final, err
}
Example #5
0
func packResponse(returnCode int, returnMessage string, code_list []int, message_list, district_list []string, data interface{}) []byte {
	resp := sjson.New()
	resp.Set("returnCode", returnCode)
	resp.Set("returnMessage", returnMessage)
	resp.Set("data", data)
	resp.Set("codes", code_list)
	resp.Set("msgs", message_list)
	resp.Set("districts", district_list)
	resp_str, err := resp.EncodePretty()
	if err != nil {
		log.Error("EncodePretty error")
		resp_str = []byte("EncodePretty error")
	}
	return resp_str
}
Example #6
0
func NewFilterFromQueryString(q string) (*Filter, error) {
	filter := &Filter{}
	filter.Where = simplejson.New()
	filter.loadInitialValues()

	urlValues, err := url.ParseQuery(q)
	if err != nil {
		return nil, err
	}

	for key, value := range urlValues {
		filter.putUrlValue(key, value[0])
	}

	return filter, nil
}
Example #7
0
func (j *jsonValues) Del(path ...string) {
	// delete the tree?
	if len(path) == 0 {
		j.sj = simple.New()
		return
	}

	if len(path) == 1 {
		j.sj.Del(path[0])
		return
	}

	vals := j.sj.GetPath(path[:len(path)-1]...)
	vals.Del(path[len(path)-1])
	j.sj.SetPath(path[:len(path)-1], vals.Interface())
	return
}
Example #8
0
func VarsCmd(w http.ResponseWriter, q *http.Request) {
	obj, err := core.RequestVars("http://localhost:9999/debug/vars")
	if err != nil {
		log.Println(err)
		return
	}
	jsobj := json.New()
	//log.Println(obj.Alloc)
	jsobj.Set("alloc", obj.Alloc)
	jsobj.Set("sys", obj.Sys)
	jsobj.Set("heapAlloc", obj.HeapAlloc)
	jsobj.Set("heapInuse", obj.HeapInuse)
	jsobj.Set("objects", obj.Objects)

	jsstr, _ := jsobj.Encode()
	w.Write(jsstr)
}
Example #9
0
func getCvmList(w http.ResponseWriter, req *http.Request) {
	// parse parameters
	app_id, owner_uin, district := parseParams(req)
	var resp_str []byte
	if app_id < 0 {
		// fail
		log.Error("invalid app_id : ", app_id)
		resp_str = packResponse(cvmcode.PARAM_ERR, "param error", nil, nil, nil, nil)
		w.Write(resp_str)
		return
	}
	log.Infof("get req -> app_id : %d owner_uin : %s district : %s\n", app_id, owner_uin, district)
	// expect cvm list
	code_list, message_list, district_list, device_list := processGetCvmList(app_id, owner_uin, district)
	log.Info("final code_list => ", code_list)
	log.Info("final message_list => ", message_list)
	log.Info("final district_list => ", district_list)
	log.Info("final device_list => ", device_list)
	final_return_code := cvmcode.CGW_INTERFACE_ALL_FAIL
	final_return_msg := "all cgw interface failed"
	// return ok if there are any interface returns ok
	for _, rt_code := range code_list {
		if rt_code == cvmcode.OK {
			final_return_code = cvmcode.OK
			final_return_msg = "ok"
			break
		}
	}
	device_num := len(device_list)
	cvms := sjson.New()
	cvms.Set("totalNum", device_num)
	cvms.Set("deviceList", device_list)
	// response
	resp_str = packResponse(final_return_code, final_return_msg, code_list, message_list, district_list, cvms)
	w.Write(resp_str)
}
Example #10
0
// New returns a pointer to ImJSON
func New() (*ImJSON, error) {
	return &ImJSON{
		data:        simplejson.New(),
		mu:          &sync.Mutex{},
		versionRepo: NewMapRepo()}, nil
}
Example #11
0
func newValue(s *simple.Json) Value {
	if s == nil {
		s = simple.New()
	}
	return &jsonValue{s}
}