本文整理汇总了Golang中github.com/valyala/fasthttp.RequestCtx.Success方法的典型用法代码示例。如果您正苦于以下问题:Golang RequestCtx.Success方法的具体用法?Golang RequestCtx.Success怎么用?Golang RequestCtx.Success使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/valyala/fasthttp.RequestCtx
的用法示例。
在下文中一共展示了RequestCtx.Success方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: requestHandler
func requestHandler(ctx *fasthttp.RequestCtx) {
h := &ctx.Request.Header
if !ctx.IsGet() {
ctx.Error("Method not allowed", fasthttp.StatusMethodNotAllowed)
return
}
if string(ctx.RequestURI()) == *statsRequestPath {
var w bytes.Buffer
stats.WriteToStream(&w)
ctx.Success("text/plain", w.Bytes())
return
}
if len(h.Peek("If-None-Match")) > 0 {
resp := &ctx.Response
resp.SetStatusCode(fasthttp.StatusNotModified)
resp.Header.Set("Etag", "W/\"CacheForever\"")
atomic.AddInt64(&stats.IfNoneMatchHitsCount, 1)
return
}
v := keyPool.Get()
if v == nil {
v = make([]byte, 128)
}
key := v.([]byte)
key = append(key[:0], getRequestHost(h)...)
key = append(key, ctx.RequestURI()...)
item, err := cache.GetDeItem(key, time.Second)
if err != nil {
if err != ybc.ErrCacheMiss {
logFatal("Unexpected error when obtaining cache value by key=[%s]: [%s]", key, err)
}
atomic.AddInt64(&stats.CacheMissesCount, 1)
item = fetchFromUpstream(h, key)
if item == nil {
ctx.Error("Service unavailable", fasthttp.StatusServiceUnavailable)
return
}
} else {
atomic.AddInt64(&stats.CacheHitsCount, 1)
}
defer item.Close()
keyPool.Put(v)
contentType, err := loadContentType(h, item)
if err != nil {
ctx.Error("Internal Server Error", fasthttp.StatusInternalServerError)
return
}
rh := &ctx.Response.Header
rh.Set("Etag", "W/\"CacheForever\"")
rh.Set("Cache-Control", "public, max-age=31536000")
buf := item.Value()
buf = buf[len(buf)-item.Available():]
ctx.Success(contentType, buf)
atomic.AddInt64(&stats.BytesSentToClients, int64(len(buf)))
}
示例2: plaintextHandler
// Test 6: Plaintext
func plaintextHandler(ctx *fasthttp.RequestCtx) {
ctx.Success("text/plain", helloWorldBytes)
}