代码拉取完成,页面将自动刷新
// 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)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。