当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
2 Star 0 Fork 1

JUMEI_ARCH/go-plugins
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
gzip.go 1.18 KB
一键复制 编辑 原始数据 按行查看 历史
Asim Aslam 提交于 2017-05-15 15:59 . update comments
// Package gzip is a micro plugin for gzipping http response
package gzip
import (
"compress/gzip"
"io"
"net/http"
"strings"
"github.com/micro/cli"
"github.com/micro/micro/plugin"
)
type gzipWriter struct {
io.Writer
http.ResponseWriter
}
type gzipper struct{}
func (g *gzipper) Flags() []cli.Flag {
return nil
}
func (g *gzipper) Commands() []cli.Command {
return nil
}
func (g *gzipper) Handler() plugin.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// has gzip accept-encoding?
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
h.ServeHTTP(w, r)
return
}
// set the content-encoding
w.Header().Set("Content-Encoding", "gzip")
// create gzip writer
gz := gzip.NewWriter(w)
defer gz.Close()
// create http response writer
gzw := gzipWriter{gz, w}
// serve the request
h.ServeHTTP(gzw, r)
})
}
}
func (g *gzipper) Init(ctx *cli.Context) error {
return nil
}
func (g *gzipper) String() string {
return "gzip"
}
func (w gzipWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}
func New() plugin.Plugin {
return new(gzipper)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/JMArch/go-plugins.git
git@gitee.com:JMArch/go-plugins.git
JMArch
go-plugins
go-plugins
v0.7.0

搜索帮助

0d507c66 1850385 C8b1a773 1850385