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


Golang web.Context類代碼示例

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


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

示例1: 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:FactomProject,項目名稱:FactomCode,代碼行數:30,代碼來源:wsapi.go

示例2: handleEntry

func handleEntry(ctx *web.Context, hash string) {
	type entry struct {
		ChainID string
		Content string
		ExtIDs  []string
	}

	e := new(entry)
	if entry, err := factomapi.EntryByHash(hash); err != nil {
		wsLog.Error(err)
		ctx.WriteHeader(httpBad)
		ctx.Write([]byte(err.Error()))
		return
	} else {
		e.ChainID = entry.ChainID.String()
		e.Content = hex.EncodeToString(entry.Content)
		for _, v := range entry.ExtIDs {
			e.ExtIDs = append(e.ExtIDs, hex.EncodeToString(v))
		}
	}

	if p, err := json.Marshal(e); err != nil {
		wsLog.Error(err)
		ctx.WriteHeader(httpBad)
		ctx.Write([]byte(err.Error()))
		return
	} else {
		ctx.Write(p)
	}
}
開發者ID:FactomProject,項目名稱:FactomCode,代碼行數: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:FactomProject,項目名稱:FactomCode,代碼行數:26,代碼來源:wsapi.go

示例4: CreateWebContext

func CreateWebContext() *web.Context {
	context := new(web.Context)
	context.Server = new(web.Server)
	context.Server.Env = map[string]interface{}{}
	context.Server.Env["state"] = CreateAndPopulateTestState()
	context.ResponseWriter = new(TestResponseWriter)

	return context
}
開發者ID:FactomProject,項目名稱:factomd,代碼行數:9,代碼來源:wsapi.go

示例5: returnV1Msg

func returnV1Msg(ctx *web.Context, msg string, success bool) {

	/* V1 requires call specific case changes that can't be handled with
	interfaces for example.  Block Height needs to return  height as the json item name
	in golang, lower case names are private so won't be returned.
	Deal with the responses in the call specific v1 handlers until they are depricated.
	*/
	bMsg := []byte(msg)
	ctx.Write(bMsg)

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

示例6: 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.ContentType("json")
	ctx.Write(j)
}
開發者ID:FactomProject,項目名稱:fctwallet,代碼行數:12,代碼來源:transaction.go

示例7: HandleV2Error

func HandleV2Error(ctx *web.Context, j *primitives.JSON2Request, err *primitives.JSONError) {
	resp := primitives.NewJSON2Response()
	if j != nil {
		resp.ID = j.ID
	} else {
		resp.ID = nil
	}
	resp.Error = err

	ctx.WriteHeader(httpBad)
	ctx.Write([]byte(resp.String()))
}
開發者ID:FactomProject,項目名稱:factomd,代碼行數:12,代碼來源:wsapiV2.go

示例8: 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:FactomProject,項目名稱:FactomCode,代碼行數:12,代碼來源:wsapi.go

示例9: handleGetRaw

func handleGetRaw(ctx *web.Context, hashkey string) {
	type rawData struct {
		Data string
	}
	//TODO: var block common.BinaryMarshallable
	d := new(rawData)

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

	// try to find the block data in db and return the first one found
	if block, _ := dbase.FetchFBlockByHash(h); block != nil {
		bytes, _ := block.MarshalBinary()
		d.Data = hex.EncodeToString(bytes[:])
	} else if block, _ := dbase.FetchDBlockByHash(h); block != nil {
		bytes, _ := block.MarshalBinary()
		d.Data = hex.EncodeToString(bytes[:])
	} else if block, _ := dbase.FetchABlockByHash(h); block != nil {
		bytes, _ := block.MarshalBinary()
		d.Data = hex.EncodeToString(bytes[:])
	} else if block, _ := dbase.FetchDBlockByMR(h); block != nil {
		bytes, _ := block.MarshalBinary()
		d.Data = hex.EncodeToString(bytes[:])
	} else if block, _ := dbase.FetchEBlockByHash(h); block != nil {
		bytes, _ := block.MarshalBinary()
		d.Data = hex.EncodeToString(bytes[:])
	} else if block, _ := dbase.FetchEBlockByMR(h); block != nil {
		bytes, _ := block.MarshalBinary()
		d.Data = hex.EncodeToString(bytes[:])
	} else if block, _ := dbase.FetchECBlockByHash(h); block != nil {
		bytes, _ := block.MarshalBinary()
		d.Data = hex.EncodeToString(bytes[:])
	} else if block, _ := dbase.FetchEntryByHash(h); block != nil {
		bytes, _ := block.MarshalBinary()
		d.Data = hex.EncodeToString(bytes[:])
	}

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

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

示例10: 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:FactomProject,項目名稱:FactomCode,代碼行數:14,代碼來源:wsapi.go

示例11: HandleGetTransactionsj

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

示例12: handleV1Error

func handleV1Error(ctx *web.Context, err *primitives.JSONError) {
	/*
		if err.Data != nil {
			data, ok := err.Data.(string)
			if ok == true {
		ctx.WriteHeader(httpBad)
				returnMsg(ctx, "", false)
				return
			}
		}
		ctx.WriteHeader(httpBad)
		returnMsg(ctx,"", false)
		return
	*/
	ctx.WriteHeader(httpBad)
	return
}
開發者ID:FactomProject,項目名稱:factomd,代碼行數:17,代碼來源:wsapi.go

示例13: handleDirectoryBlock

func handleDirectoryBlock(ctx *web.Context, keymr string) {
	type eblockaddr struct {
		ChainID string
		KeyMR   string
	}

	type dblock struct {
		Header struct {
			PrevBlockKeyMR string
			SequenceNumber uint32
			Timestamp      uint32
		}
		EntryBlockList []eblockaddr
	}

	d := new(dblock)
	if block, err := factomapi.DBlockByKeyMR(keymr); err != nil {
		wsLog.Error(err)
		ctx.WriteHeader(httpBad)
		ctx.Write([]byte(err.Error()))
		return
	} else {
		d.Header.PrevBlockKeyMR = block.Header.PrevKeyMR.String()
		d.Header.SequenceNumber = block.Header.DBHeight
		d.Header.Timestamp = block.Header.Timestamp * 60
		for _, v := range block.DBEntries {
			l := new(eblockaddr)
			l.ChainID = v.ChainID.String()
			l.KeyMR = v.KeyMR.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)
	}

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

示例14: returnMsg

func returnMsg(ctx *web.Context, msg interface{}, success bool) {
	type rtn struct {
		Response interface{}
		Success  bool
	}
	/*str, ok:=msg.(string)
	if ok == false {
		var err error
		str, err = primitives.EncodeJSONString(msg)
		if err != nil {
			wsLog.Error(err)
			return
		}
	}*/
	r := msg

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

示例15: handleDirectoryBlockHeight

func handleDirectoryBlockHeight(ctx *web.Context) {
	type dbheight struct {
		Height int
	}

	h := new(dbheight)
	if block, err := factomapi.DBlockHead(); err != nil {
		wsLog.Error(err)
		ctx.WriteHeader(httpBad)
		ctx.Write([]byte(err.Error()))
		return
	} else {
		h.Height = int(block.Header.DBHeight)
	}

	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:FactomProject,項目名稱:FactomCode,代碼行數:24,代碼來源:wsapi.go


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