本文整理匯總了Golang中github.com/FactomProject/web.Context.WriteHeader方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.WriteHeader方法的具體用法?Golang Context.WriteHeader怎麽用?Golang Context.WriteHeader使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/FactomProject/web.Context
的用法示例。
在下文中一共展示了Context.WriteHeader方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: 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)
}
}
示例2: 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()))
}
示例3: 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)
}
示例4: 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)
}
}
示例5: reportResults
// True is success! 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.ContentType("json")
ctx.Write(p)
}
}
示例6: 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)
}
}
示例7: 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
}
示例8: HandleCommitEntry
func HandleCommitEntry(ctx *web.Context, name string) {
data, err := ioutil.ReadAll(ctx.Request.Body)
if err != nil {
fmt.Println("Could not read from http request:", err)
ctx.WriteHeader(httpBad)
ctx.Write([]byte(err.Error()))
return
}
err = Wallet.CommitEntry(name, data)
if err != nil {
fmt.Println(err)
ctx.WriteHeader(httpBad)
ctx.Write([]byte(err.Error()))
return
}
}
示例9: 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)
}
示例10: 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
}
}
示例11: 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)
}
示例12: handleChainHead
func handleChainHead(ctx *web.Context, chainid string) {
type chead struct {
ChainHead string
}
c := new(chead)
if mr, err := factomapi.ChainHead(chainid); err != nil {
wsLog.Error(err)
ctx.WriteHeader(httpBad)
ctx.Write([]byte(err.Error()))
return
} else {
c.ChainHead = mr.String()
}
if p, err := json.Marshal(c); err != nil {
wsLog.Error(err)
ctx.WriteHeader(httpBad)
ctx.Write([]byte(err.Error()))
return
} else {
ctx.Write(p)
}
}
示例13: 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)
}
}
示例14: handleDirectoryBlockHead
func handleDirectoryBlockHead(ctx *web.Context) {
type dbhead struct {
KeyMR string
}
h := new(dbhead)
if block, err := factomapi.DBlockHead(); err != nil {
wsLog.Error(err)
ctx.WriteHeader(httpBad)
ctx.Write([]byte(err.Error()))
return
} else {
h.KeyMR = block.KeyMR.String()
}
if p, err := json.Marshal(h); err != nil {
wsLog.Error(err)
ctx.WriteHeader(httpBad)
ctx.Write([]byte(err.Error()))
return
} else {
ctx.Write(p)
}
}
示例15: getParams_
// &key=<key>&name=<name or address>&amount=<amount>
// If no amount is specified, a zero is returned.
func getParams_(ctx *web.Context, params string, ec bool) (
trans fct.ITransaction,
key string,
name string,
address fct.IAddress,
amount int64,
ok bool) {
key = ctx.Params["key"]
name = ctx.Params["name"]
StrAmount := ctx.Params["amount"]
if len(StrAmount) == 0 {
StrAmount = "0"
}
if len(key) == 0 || len(name) == 0 {
str := fmt.Sprintln("Missing Parameters: key='", key, "' name='", name, "' amount='", StrAmount, "'")
reportResults(ctx, str, false)
ok = false
return
}
msg, valid := ValidateKey(key)
if !valid {
reportResults(ctx, msg, false)
ok = false
return
}
amount, err := strconv.ParseInt(StrAmount, 10, 64)
if err != nil {
str := fmt.Sprintln("Error parsing amount.\n", err)
reportResults(ctx, str, false)
ok = false
return
}
// Get the transaction
trans, err = getTransaction(ctx, key)
if err != nil {
reportResults(ctx, "Failure to locate the transaction", false)
ok = false
return
}
// Get the input/output/ec address. Which could be a name. First look and see if it is
// a name. If it isn't, then look and see if it is an address. Someone could
// do a weird Address as a name and fool the code, but that seems unlikely.
// Could check for that some how, but there are many ways around such checks.
if len(name) <= fct.ADDRESS_LENGTH {
we := Wallet.GetRaw([]byte(fct.W_NAME), []byte(name))
if we != nil {
address, err = we.(wallet.IWalletEntry).GetAddress()
if we.(wallet.IWalletEntry).GetType() == "ec" {
if !ec {
reportResults(ctx, "Was Expecting a Factoid Address", false)
ok = false
return
}
} else {
if ec {
reportResults(ctx, "Was Expecting an Entry Credit Address", false)
ok = false
return
}
}
if err != nil || address == nil {
reportResults(ctx, "Should not get an error geting a address from a Wallet Entry", false)
ok = false
return
}
ok = true
return
}
}
if (!ec && !fct.ValidateFUserStr(name)) || (ec && !fct.ValidateECUserStr(name)) {
reportResults(ctx, fmt.Sprintf("The address specified isn't defined or is invalid: %s", name), false)
ctx.WriteHeader(httpBad)
ok = false
return
}
baddr := fct.ConvertUserStrToAddress(name)
address = fct.NewAddress(baddr)
ok = true
return
}