【发布时间】:2014-07-28 02:27:42
【问题描述】:
在准嵌入式环境中,速度就是一切。我发现如果我压缩我的 .html 文件,应用程序会更快。马提尼酒中是否有标志或方式可以即时执行此操作?
【问题讨论】:
在准嵌入式环境中,速度就是一切。我发现如果我压缩我的 .html 文件,应用程序会更快。马提尼酒中是否有标志或方式可以即时执行此操作?
【问题讨论】:
你可以使用gzip中间件
https://github.com/codegangsta/martini-contrib/tree/master/gzip
import (
"github.com/codegangsta/martini"
"github.com/codegangsta/martini-contrib/gzip"
)
func main() {
m := martini.Classic()
// gzip every request
m.Use(gzip.All())
m.Run()
}
【讨论】:
这个答案只是为了表明@fabrizioM 的答案确实有效:
package main
import (
"github.com/codegangsta/martini"
"github.com/codegangsta/martini-contrib/gzip"
)
func main() {
m := martini.Classic()
// gzip every request
m.Use(gzip.All())
m.Get("/hello", func() string {
return "Hello, World!"
})
m.Run()
}
go run main.go
这是您必须记住包含 Accept-Encoding: gzip 标头(或等效标头)的步骤。
没有压缩:
curl --dump-header http://localhost:3000/hello
HTTP/1.1 200 OK Date: Wed, 09 Jul 2014 17:19:35 GMT Content-Length: 13 Content-Type: text/plain; charset=utf-8 Hello, World!
压缩:
curl --dump-header http://localhost:3000/hello -H 'Accept-Encoding: gzip'
HTTP/1.1 200 OK Content-Encoding: gzip Content-Type: text/plain; charset=utf-8 Vary: Accept-Encoding Date: Wed, 09 Jul 2014 17:21:02 GMT Content-Length: 37 � n��Q��J
【讨论】:
curl --dump-header - http://localhost:3000/hello -H 'Accept-Encoding: gzip' 如果你得到'curl: no URL specified!'错误。