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


Golang Context.Write方法代碼示例

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


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

示例1: search

func search(ctx *web.Context, collection string) {
	ctx.ContentType("json")
	ctx.SetHeader("Access-Control-Allow-Origin", "*", true)

	query := ctx.Params["query"]

	var limit, offset int64
	var err error

	if limit, err = strconv.ParseInt(ctx.Params["limit"], 10, 32); err != nil {
		limit = 10
	}
	if offset, err = strconv.ParseInt(ctx.Params["offset"], 10, 32); err != nil {
		offset = 0
	}

	results, err := c.Search(collection, query, int(limit), int(offset))

	buf := new(bytes.Buffer)
	encoder := json.NewEncoder(buf)

	if err != nil {
		encoder.Encode(err)
		ctx.WriteHeader(err.(*gorc.OrchestrateError).StatusCode)
	} else {
		encoder.Encode(results)
	}

	ctx.Write(buf.Bytes())
}
開發者ID:Rapurva,項目名稱:orchestrate-heroku-search,代碼行數:30,代碼來源:web.go

示例2: handleEntryCreditBalance

func handleEntryCreditBalance(ctx *web.Context, eckey string) {
	type ecbal struct {
		Response string
		Success  bool
	}
	var b ecbal
	adr, err := hex.DecodeString(eckey)
	if err == nil && len(adr) != common.HASH_LENGTH {
		b = ecbal{Response: "Invalid Address", Success: false}
	}
	if err == nil {
		if bal, err := factomapi.ECBalance(eckey); err != nil {
			wsLog.Error(err)
			return
		} else {
			str := fmt.Sprintf("%d", bal)
			b = ecbal{Response: str, Success: true}
		}
	} else {
		b = ecbal{Response: err.Error(), Success: false}
	}

	if p, err := json.Marshal(b); err != nil {
		wsLog.Error(err)
		return
	} else {
		ctx.Write(p)
	}

}
開發者ID:Kalipsol,項目名稱:factomd,代碼行數:30,代碼來源:wsapi.go

示例3: handleFactoidBalance

func handleFactoidBalance(ctx *web.Context, eckey string) {
	type fbal struct {
		Response string
		Success  bool
	}
	var b fbal
	adr, err := hex.DecodeString(eckey)
	if err == nil && len(adr) != common.HASH_LENGTH {
		b = fbal{Response: "Invalid Address", Success: false}
	}
	if err == nil {
		v := int64(common.FactoidState.GetBalance(fct.NewAddress(adr)))
		str := fmt.Sprintf("%d", v)
		b = fbal{Response: str, Success: true}
	} else {
		b = fbal{Response: err.Error(), Success: false}
	}

	if p, err := json.Marshal(b); err != nil {
		wsLog.Error(err)
		return
	} else {
		ctx.Write(p)
	}

}
開發者ID:Kalipsol,項目名稱:factomd,代碼行數:26,代碼來源:wsapi.go

示例4: HandleDirectoryBlock

func HandleDirectoryBlock(ctx *web.Context, hashkey string) {
	state := ctx.Server.Env["state"].(interfaces.IState)

	d := new(DBlock)

	h, err := primitives.HexToHash(hashkey)
	if err != nil {
		wsLog.Error(err)
		ctx.WriteHeader(httpBad)
		ctx.Write([]byte(err.Error()))
		return
	}

	dbase := state.GetDB()

	block, err := dbase.FetchDBlockByKeyMR(h)
	if err != nil {
		wsLog.Error(err)
		ctx.WriteHeader(httpBad)
		ctx.Write([]byte(err.Error()))
		return
	}
	if block == nil {
		block, err = dbase.FetchDBlockByHash(h)
		if err != nil {
			wsLog.Error(err)
			ctx.WriteHeader(httpBad)
			ctx.Write([]byte(err.Error()))
			return
		}
		if block == nil {
			//TODO: Handle block not found
			return
		}
	}

	d.Header.PrevBlockKeyMR = block.GetHeader().GetPrevKeyMR().String()
	d.Header.SequenceNumber = block.GetHeader().GetDBHeight()
	d.Header.Timestamp = block.GetHeader().GetTimestamp() * 60
	for _, v := range block.GetDBEntries() {
		l := new(EBlockAddr)
		l.ChainID = v.GetChainID().String()
		l.KeyMR = v.GetKeyMR().String()
		d.EntryBlockList = append(d.EntryBlockList, *l)
	}

	if p, err := json.Marshal(d); err != nil {
		wsLog.Error(err)
		ctx.WriteHeader(httpBad)
		ctx.Write([]byte(err.Error()))
		return
	} else {
		ctx.Write(p)
	}
}
開發者ID:jjdevbiz,項目名稱:factomd,代碼行數:55,代碼來源:wsapi.go

示例5: HandleGetAddresses

func HandleGetAddresses(ctx *web.Context) {
	b := new(Response)
	b.Response = string(GetAddresses())
	b.Success = true
	j, err := json.Marshal(b)
	if err != nil {
		reportResults(ctx, err.Error(), false)
		return
	}
	ctx.Write(j)
}
開發者ID:johncap46,項目名稱:fctwallet,代碼行數:11,代碼來源:transaction.go

示例6: handleGetFee

func handleGetFee(ctx *web.Context) {
	type x struct{ Fee int64 }
	b := new(x)
	b.Fee = int64(common.FactoidState.GetFactoshisPerEC())
	if p, err := json.Marshal(b); err != nil {
		wsLog.Error(err)
		ctx.WriteHeader(httpBad)
		return
	} else {
		ctx.Write(p)
	}
}
開發者ID:Kalipsol,項目名稱:factomd,代碼行數:12,代碼來源:wsapi.go

示例7: reportResults

// True is sccuess! False is failure.  The Response is what the CLI
// should report.
func reportResults(ctx *web.Context, response string, success bool) {
	b := Response{
		Response: response,
		Success:  success,
	}
	if p, err := json.Marshal(b); err != nil {

		ctx.WriteHeader(httpBad)
		return
	} else {
		ctx.Write(p)
	}
}
開發者ID:johncap46,項目名稱:fctwallet,代碼行數:15,代碼來源:transaction.go

示例8: returnMsg

func returnMsg(ctx *web.Context, msg string, success bool) {
	type rtn struct {
		Response string
		Success  bool
	}
	r := rtn{Response: msg, Success: success}

	if p, err := json.Marshal(r); err != nil {
		wsLog.Error(err)
		return
	} else {
		ctx.Write(p)
	}
}
開發者ID:Kalipsol,項目名稱:factomd,代碼行數:14,代碼來源:wsapi.go

示例9: handleProperties

func handleProperties(ctx *web.Context) {

	r := new(common.Properties)
	r.Factomd_Version = common.FACTOMD_VERSION
	r.Protocol_Version = btcd.ProtocolVersion

	if p, err := json.Marshal(r); err != nil {
		wsLog.Error(err)
		ctx.WriteHeader(httpBad)
		ctx.Write([]byte(err.Error()))
		return
	} else {
		ctx.Write(p)
	}
}
開發者ID:Davidx7,項目名稱:FactomCode,代碼行數:15,代碼來源:wsapi.go

示例10: HandleGetTransactions

func HandleGetTransactions(ctx *web.Context) {
	b := new(Response)
	txt, err := GetTransactions(ctx)
	if err != nil {
		reportResults(ctx, err.Error(), false)
		return
	}
	b.Response = string(txt)
	b.Success = true
	j, err := json.Marshal(b)
	if err != nil {
		reportResults(ctx, err.Error(), false)
		return
	}
	ctx.Write(j)
}
開發者ID:johncap46,項目名稱:fctwallet,代碼行數:16,代碼來源:transaction.go

示例11: GetDeployments

func GetDeployments(ctx *web.Context) {
	fmt.Printf("************************************\n")
	deployments, err := LoadDeployments()
	if err != nil {
		fmt.Println(err)
		return
	}
	for _, deployment := range deployments {
		fmt.Printf(" Deployment %s ...\n", deployment.Name)
	}
	b, err := json.Marshal(deployments)
	if err != nil {
		fmt.Println("error:", err)
	}
	ctx.ContentType("json")
	ctx.Write(b)
}
開發者ID:napsy,項目名稱:goready,代碼行數:17,代碼來源:main.go

示例12: HandleGetFee

func HandleGetFee(ctx *web.Context) {
	state := ctx.Server.Env["state"].(interfaces.IState)

	type x struct{ Fee int64 }

	b := new(x)

	b.Fee = int64(state.GetFactoidState().GetFactoshisPerEC())

	if p, err := json.Marshal(b); err != nil {
		wsLog.Error(err)
		ctx.WriteHeader(httpBad)
		return
	} else {
		ctx.Write(p)
	}
}
開發者ID:jjdevbiz,項目名稱:factomd,代碼行數:17,代碼來源:wsapi.go

示例13: handleCommitChain

func handleCommitChain(ctx *web.Context) {
	type commitchain struct {
		CommitChainMsg string
	}

	c := new(commitchain)
	if p, err := ioutil.ReadAll(ctx.Request.Body); err != nil {
		wsLog.Error(err)
		ctx.WriteHeader(httpBad)
		ctx.Write([]byte(err.Error()))
		return
	} else {
		if err := json.Unmarshal(p, c); err != nil {
			wsLog.Error(err)
			ctx.WriteHeader(httpBad)
			ctx.Write([]byte(err.Error()))
			return
		}
	}

	commit := common.NewCommitChain()
	if p, err := hex.DecodeString(c.CommitChainMsg); err != nil {
		wsLog.Error(err)
		ctx.WriteHeader(httpBad)
		ctx.Write([]byte(err.Error()))
		return
	} else {
		_, err := commit.UnmarshalBinaryData(p)
		if err != nil {
			wsLog.Error(err)
			ctx.WriteHeader(httpBad)
			ctx.Write([]byte(err.Error()))
			return
		}
	}

	if err := factomapi.CommitChain(commit); err != nil {
		wsLog.Error(err)
		ctx.WriteHeader(httpBad)
		ctx.Write([]byte(err.Error()))
		return
	}

	//	ctx.WriteHeader(httpOK)
}
開發者ID:Kalipsol,項目名稱:factomd,代碼行數:45,代碼來源:wsapi.go

示例14: handleRevealEntry

func handleRevealEntry(ctx *web.Context) {
	type revealentry struct {
		Entry string
	}

	e := new(revealentry)
	if p, err := ioutil.ReadAll(ctx.Request.Body); err != nil {
		wsLog.Error(err)
		ctx.WriteHeader(httpBad)
		ctx.Write([]byte(err.Error()))
		return
	} else {
		if err := json.Unmarshal(p, e); err != nil {
			wsLog.Error(err)
			ctx.WriteHeader(httpBad)
			ctx.Write([]byte(err.Error()))
			return
		}
	}

	entry := common.NewEntry()
	if p, err := hex.DecodeString(e.Entry); err != nil {
		wsLog.Error(err)
		ctx.WriteHeader(httpBad)
		ctx.Write([]byte(err.Error()))
		return
	} else {
		_, err := entry.UnmarshalBinaryData(p)
		if err != nil {
			wsLog.Error(err)
			ctx.WriteHeader(httpBad)
			ctx.Write([]byte(err.Error()))
			return
		}
	}

	if err := factomapi.RevealEntry(entry); err != nil {
		wsLog.Error(err)
		ctx.WriteHeader(httpBad)
		ctx.Write([]byte(err.Error()))
		return
	}

	//	ctx.WriteHeader(httpOK)
}
開發者ID:Kalipsol,項目名稱:factomd,代碼行數:45,代碼來源:wsapi.go

示例15: HandleDirectoryBlockHead

func HandleDirectoryBlockHead(ctx *web.Context) {
	state := ctx.Server.Env["state"].(interfaces.IState)

	h := new(DBHead)

	h.KeyMR = state.GetPreviousDirectoryBlock().GetKeyMR().String()

	fmt.Println(h.KeyMR)

	if p, err := json.Marshal(h); err != nil {
		wsLog.Error(err)
		ctx.WriteHeader(httpBad)
		ctx.Write([]byte(err.Error()))
		return
	} else {
		ctx.Write(p)
	}

}
開發者ID:jjdevbiz,項目名稱:factomd,代碼行數:19,代碼來源:wsapi.go


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