diff --git a/context/acceptencoder.go b/context/acceptencoder.go index fc2775ce..1c3cf1d5 100644 --- a/context/acceptencoder.go +++ b/context/acceptencoder.go @@ -25,15 +25,13 @@ import ( "strconv" "strings" "sync" - - "github.com/astaxie/beego/config" ) var ( - //Content will only be compressed if content length is either unknown or greater than gzipMinLength. - gzipMinLength int //Default size==20B same as nginx defaultGzipMinLength = 20 + //Content will only be compressed if content length is either unknown or greater than gzipMinLength. + gzipMinLength = defaultGzipMinLength //The compression level used for deflate compression. (0-9). gzipCompressLevel int //List of HTTP methods to compress. If not set, only GET requests are compressed. @@ -41,13 +39,14 @@ var ( getMethodOnly bool ) -func InitGzip(cf config.Configer) { - gzipMinLength = cf.DefaultInt("gzipMinLength", defaultGzipMinLength) - gzipCompressLevel = cf.DefaultInt("gzipCompressLevel", flate.BestSpeed) +func InitGzip(minLength, compressLevel int, methods []string) { + if minLength >= 0 { + gzipMinLength = minLength + } + gzipCompressLevel = compressLevel if gzipCompressLevel < flate.DefaultCompression || gzipCompressLevel > flate.BestCompression { gzipCompressLevel = flate.BestSpeed } - methods := cf.DefaultStrings("includedMethods", []string{"GET"}) getMethodOnly = (len(methods) == 0) || (len(methods) == 1 && strings.ToUpper(methods[0]) == "GET") includedMethods = make(map[string]bool, len(methods)) for _, v := range methods { diff --git a/hooks.go b/hooks.go index 1a7937b5..674f2858 100644 --- a/hooks.go +++ b/hooks.go @@ -95,7 +95,11 @@ func registerAdmin() error { func registerGzip() error { if BConfig.EnableGzip { - context.InitGzip(AppConfig) + context.InitGzip( + AppConfig.DefaultInt("gzipMinLength", -1), + AppConfig.DefaultInt("gzipCompressLevel", -1), + AppConfig.DefaultStrings("includedMethods", []string{"GET"}), + ) } return nil }