compress

package
v5.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2022 License: MIT Imports: 13 Imported by: 1

Documentation

Overview

Package compress 提供一个支持内容压缩的中间件

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Compress

type Compress struct {

	// 是否启用压缩功能
	//
	// 在诸如服务器性能吃紧的情况下,可通过修改此值,临时开关压缩功能。
	Enable bool
	// contains filtered or unexported fields
}

Compress 提供压缩功能的中件间

NOTE: Compress 必须是所有有输出功能中间件的最外层。 否则可能造成部分内容被压缩,而部分内容未压缩的情况。

func Classic

func Classic(errlog *log.Logger, ignoreTypes ...string) *Compress

Classic 简单的初始化 Compress 方式

ignoreMethods 被设置为 HEAD 和 OPTIONS;同时添加 deflate, gzip 和 br 三种压缩方式。

func New

func New(errlog *log.Logger, ignoreMethods []string, ignoreTypes ...string) *Compress

New 构建一个支持压缩的中间件

errlog 错误日志的输出通道; ignoreMethods 忽略的请求方法,如果不为空,则这些请求方法的请求将不会被压缩; ignoreTypes 表示不需要进行压缩处理的 mimetype 类型,可以是以下格式:

  • application/json 具体类型;
  • text* 表示以 text 开头的所有类型;

不能传递 *,如果要禁用,可以直接将 Enable 设置为 false。

func (*Compress) AddAlgorithm

func (c *Compress) AddAlgorithm(name string, wf WriterFunc) (ok bool)

AddAlgorithm 添加压缩算法

当前用户的 Accept-Encoding 的匹配到 * 时,按添加顺序查找真正的匹配项。 不能添加名为 identity 和 * 的算法。

如果未添加任何算法,则每个请求都相当于是 identity 规则。

返回值表示是否添加成功,若为 false,则表示已经存在相同名称的对象。

func (*Compress) Middleware

func (c *Compress) Middleware(next http.Handler) http.Handler

Middleware 将当前中间件应用于 next

func (*Compress) MiddlewareFunc

func (c *Compress) MiddlewareFunc(next func(w http.ResponseWriter, r *http.Request)) http.Handler

MiddlewareFunc 将当前中间件应用于 next

func (*Compress) SetAlgorithm

func (c *Compress) SetAlgorithm(name string, wf WriterFunc)

SetAlgorithm 设置压缩算法

如果 w 为 nil,则表示去掉此算法的支持。

type Writer

type Writer interface {
	io.WriteCloser
	Reset(io.Writer)
}

Writer 所有压缩对象实现的接口

func NewBrotli

func NewBrotli(w io.Writer) (Writer, error)

NewBrotli 新建 br 算法

func NewDeflate

func NewDeflate(w io.Writer) (Writer, error)

NewDeflate 新建 deflate 算法

func NewGzip

func NewGzip(w io.Writer) (Writer, error)

NewGzip 新建 gzip 算法

type WriterFunc

type WriterFunc func(w io.Writer) (Writer, error)

WriterFunc 将普通的 io.Writer 封装成 Writer 接口对象

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL