當前位置: 首頁>>代碼示例>>Golang>>正文


Golang HttpResponse.Warnings方法代碼示例

本文整理匯總了Golang中wharf/util.HttpResponse.Warnings方法的典型用法代碼示例。如果您正苦於以下問題:Golang HttpResponse.Warnings方法的具體用法?Golang HttpResponse.Warnings怎麽用?Golang HttpResponse.Warnings使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在wharf/util.HttpResponse的用法示例。


在下文中一共展示了HttpResponse.Warnings方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: CreateTaskHandler

/*function:create task from the user command.
	we will create the task and store it in Tasks
param:
	r: r.Body include the string of the user command
	w:	w.Body include the result of the create.response will be marshal to w.
*/
func CreateTaskHandler(w http.ResponseWriter, r *http.Request) {
	var response util.HttpResponse
	var thisTask Task
	response.Status = util.INVALID_INPUT

	var contents []byte
	contents = make([]byte, 1000)
	length, err := r.Body.Read(contents)
	if err != nil && err != io.EOF {
		fmt.Fprintf(os.Stderr, "CreateHandler: can not read from http resposeWriter\n")
		fmt.Fprintf(os.Stderr, "%s", err)
		response.Warnings = []string{"CreateHandler: can not read from http resposeWriter\n" + err.Error()}
		io.WriteString(w, response.String())
		return
	}
	var res util.SendCmd
	if *FlagDebug {
		fmt.Println("contents:", string(contents))
	}
	//make sure the char in contents should not include '\0'
	contentsRight := contents[:length]
	errunmarshal := json.Unmarshal(contentsRight, &res)
	if errunmarshal != nil {
		fmt.Fprintf(os.Stderr, "CreateHandler: Unmarshal failed for contents: ")
		fmt.Fprintf(os.Stderr, "%s", errunmarshal)
		response.Warnings = []string{"CreateHandler: Unmarshal failed for contents: " + errunmarshal.Error()}
		io.WriteString(w, response.String())
		return
	} else {
		//now we will create our task here: filter, allocator, (image server) ,scheduler
		var userRequest CreateRequest
		userRequest.Init()
		for thisflag, flagvalue := range res.Data {
			switch {
			case strings.EqualFold(thisflag, "i"):
				userRequest.ImageName = flagvalue
			case strings.EqualFold(thisflag, "t"):
				userRequest.TypeName = flagvalue
				if !strings.EqualFold(flagvalue, "mpi") && !strings.EqualFold(flagvalue, "single") {
					fmt.Fprintf(os.Stderr, `the type of the task is not supported yet. Only "single" and "mpi" is supported.`)
					response.Warnings = []string{`the type of the task is not supported yet. Only "single" and "mpi" is supported.`}
					io.WriteString(w, response.String())
					return
				}
			case strings.EqualFold(thisflag, "n"):
				userRequest.TaskName = flagvalue
			case strings.EqualFold(thisflag, "s"):
				userRequest.Stratergy = flagvalue
				if !strings.EqualFold(thisflag, "MEM") && !strings.EqualFold(thisflag, "COM") {
					response.Warnings = []string{`Only MEM and COM are valid for -s flag`}
					io.WriteString(w, response.String())
					return
				}
			case strings.EqualFold(thisflag, "c"):
				userRequest.TotalCpuNum, _ = strconv.Atoi(flagvalue)
			case strings.EqualFold(thisflag, "C"):
				userRequest.ContainerNumMax, _ = strconv.Atoi(flagvalue)
			case strings.EqualFold(thisflag, "l"):
				value, _ := strconv.ParseFloat(flagvalue, 32)
				userRequest.OverLoadMax = float32(value)
			case strings.EqualFold(thisflag, "f"):
				filename := flagvalue
				readerme, openerr := os.Open(filename)
				if openerr != nil {
					fmt.Fprintf(os.Stderr, "CreateHandler:%s", openerr)
					response.Warnings = []string{"CreateHandler" + openerr.Error()}
					io.WriteString(w, response.String())
					return
				}
				unmarshalerr := util.UnmarshalReader(readerme, &(userRequest.ResNode))
				if unmarshalerr != nil {
					response.Warnings = []string{unmarshalerr.Error()}
					io.WriteString(w, response.String())
				}
			default:
				fmt.Fprintf(os.Stderr, "CreateHandler: %s flag invalid", thisflag)
				response.Warnings = []string{"CreateHandler: " + thisflag + "flag invalid"}
				io.WriteString(w, response.String())
				return
			}
		}

		var err error
		endpoint := []string{"http://" + MasterConfig.EtcdNode.Ip + ":" + MasterConfig.EtcdNode.Port}
		err = UpdateEtcdForUse(endpoint, MasterConfig.EtcdNode.Key, true, true)
		if err != nil {
			if *FlagDebug {
				util.PrintErr("Failded to Etcd data!")
			}
			response.Warnings = []string{err.Error()}
			io.WriteString(w, response.String())
			return
		}
		//Debug
//.........這裏部分代碼省略.........
開發者ID:qzan9,項目名稱:wharf,代碼行數:101,代碼來源:manager.go


注:本文中的wharf/util.HttpResponse.Warnings方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。