本文整理汇总了Golang中github.com/hoisie/web.Context.WriteHeader方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.WriteHeader方法的具体用法?Golang Context.WriteHeader怎么用?Golang Context.WriteHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/hoisie/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: 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())
}
示例3: 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)
}
// ctx.WriteHeader(httpOK)
}
示例4: responseJson
func responseJson(ctx *web.Context, statusCode int, obj interface{}) string {
ctx.WriteHeader(statusCode)
if obj != nil {
content, _ := json.MarshalIndent(obj, " ", " ")
return string(content)
}
return ""
}
示例5: renderTemplate
func renderTemplate(webContext *web.Context, template string, action string, context interface{}, statusCode int) {
webContext.WriteHeader(statusCode)
err := templates[template].ExecuteTemplate(webContext, action+".html", context)
if err != nil {
log.Println("[ERROR] renderTemplate: ", err)
webContext.Abort(500, "Unable to process request.")
}
}
示例6: HandleGetRaw
func HandleGetRaw(ctx *web.Context, hashkey string) {
state := ctx.Server.Env["state"].(interfaces.IState)
//TODO: var block interfaces.BinaryMarshallable
d := new(RawData)
h, err := primitives.HexToHash(hashkey)
if err != nil {
wsLog.Error(err)
ctx.WriteHeader(httpBad)
ctx.Write([]byte(err.Error()))
return
}
dbase := state.GetDB()
var b []byte
// try to find the block data in db and return the first one found
if block, _ := dbase.FetchFBlockByKeyMR(h); block != nil {
b, _ = block.MarshalBinary()
} else if block, _ := dbase.FetchDBlockByKeyMR(h); block != nil {
b, _ = block.MarshalBinary()
} else if block, _ := dbase.FetchABlockByKeyMR(h); block != nil {
b, _ = block.MarshalBinary()
} else if block, _ := dbase.FetchEBlockByKeyMR(h); block != nil {
b, _ = block.MarshalBinary()
} else if block, _ := dbase.FetchECBlockByKeyMR(h); block != nil {
b, _ = block.MarshalBinary()
} else if block, _ := dbase.FetchEntryByHash(h); block != nil {
b, _ = block.MarshalBinary()
} else if block, _ := dbase.FetchFBlockByHash(h); block != nil {
b, _ = block.MarshalBinary()
} else if block, _ := dbase.FetchDBlockByHash(h); block != nil {
b, _ = block.MarshalBinary()
} else if block, _ := dbase.FetchABlockByHash(h); block != nil {
b, _ = block.MarshalBinary()
} else if block, _ := dbase.FetchEBlockByHash(h); block != nil {
b, _ = block.MarshalBinary()
} else if block, _ := dbase.FetchECBlockByHash(h); block != nil {
b, _ = block.MarshalBinary()
}
d.Data = hex.EncodeToString(b)
if p, err := json.Marshal(d); err != nil {
wsLog.Error(err)
ctx.WriteHeader(httpBad)
ctx.Write([]byte(err.Error()))
return
} else {
ctx.Write(p)
}
}
示例7: 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)
}
}
示例8: 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)
}
示例9: internalError
/*
Server a 500 Internal Error page
Parameters:
ctx: the context of the http request
err: the error that was encountered
*/
func internalError(ctx *web.Context, err error) {
logger.Printf("500 Internal Server Error: %v\n", err.Error())
sendEmailToAdmins("Redu.se Internal Error", err.Error())
ctx.WriteHeader(500)
commonTemplate(ctx,
"generic.html",
map[string]string{"title_text": "500 Internal Server Error",
"body_text": err.Error(),
})
}
示例10: error404
/*
Serve a 404 Not Found error page
Parameters:
ctx: the context of the http request
urlStr: the URL that the request was trying to access
*/
func error404(ctx *web.Context, urlStr string) {
logger.Printf("404 Error for URL: %v\n", urlStr)
bodyStr := "Could not locate \"" + urlStr + "\" on this server"
ctx.WriteHeader(404)
commonTemplate(ctx,
"generic.html",
map[string]string{"title_text": "404 Page Not Found",
"body_text": bodyStr,
})
}
示例11: 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)
}
}
示例12: 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)
}
}
示例13: 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)
return
}
err = Wallet.CommitEntry(name, data)
if err != nil {
fmt.Println(err)
ctx.WriteHeader(httpBad)
return
}
}
示例14: 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)
}
}
示例15: 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)
}
}