fasthttp Go
一个用于 fasthttp 的 Go 代码片段,以空 GIF 响应对 /pixel.gif 的请求,GIF 在启动时只解码一次。
fasthttp Go
package main
import (
"encoding/base64"
"log"
"github.com/valyala/fasthttp"
)
var gif, _ = base64.StdEncoding.DecodeString("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")
func handler(ctx *fasthttp.RequestCtx) {
if !ctx.IsGet() || string(ctx.Path()) != "/pixel.gif" {
ctx.NotFound()
return
}
ctx.SetContentType("image/gif")
ctx.Response.Header.Set("Cache-Control", "no-store")
ctx.SetBody(gif)
}
func main() {
log.Fatal(fasthttp.ListenAndServe(":8080", handler))
}
我们运行了此代码片段,并比对了它返回的字节。
验证
curl -sI http://localhost:8080/pixel.gif