1 ZH_Compress
Lunny Xiao edited this page 2015-01-07 15:18:10 +08:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

  • Compress

Tango拥有一个默认的压缩中间件可以按照扩展名来进行文件的压缩。同时你也可以要求某个Action自动或强制使用某种压缩。比如

type CompressExample struct {
    tango.Compress // 添加这个匿名结构体,要求这个结构体的方法进行自动检测压缩
}

func (CompressExample) Get() string {
    return fmt.Sprintf("This is a auto compress text")
}

o := tango.Classic()
o.Get("/", new(CompressExample))
o.Run()

以上代码默认会检测浏览器是否支持压缩如果支持则看是否支持gzip如果支持gzip则使用gzip压缩如果支持deflate则使用deflate压缩。

type GZipExample struct {
    tango.GZip // add this for ask compress to GZip, if accept-encoding has no gzip, then not compress
}

func (GZipExample) Get() string {
    return fmt.Sprintf("This is a gzip compress text")
}

o := tango.Classic()
o.Get("/", new(GZipExample))
o.Run()

以上代码默认会检测浏览器是否支持gzip压缩如果支持gzip则使用gzip压缩否则不压缩。

type DeflateExample struct {
    tango.Deflate // add this for ask compress to Deflate, if not support then not compress
}

func (DeflateExample) Get() string {
    return fmt.Sprintf("This is a deflate compress text")
}

o := tango.Classic()
o.Get("/", new(DeflateExample))
o.Run()

以上代码默认会检测浏览器是否支持deflate压缩如果支持deflate则使用deflate压缩否则不压缩。