在准嵌入式环境中,速度就是一切。我发现如果我压缩我的.html文件,应用程序的速度会更快。在马提尼中有没有一面旗帜或一种方法可以在飞行中做到这一点?
发布于 2014-06-07 02:22:03
您可以使用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()
}发布于 2014-07-10 01:21:38
这个答案只是为了证明@fabrizioM的答案是有效的:
步骤1:创建服务器
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()
}步骤2:运行服务器
go run main.go第3步:尝试服务器
这是您必须记住包括Accept-Encoding: gzip报头(或等效报头)的步骤。
不使用压缩:
curl --dump-header http://localhost:3000/hellocharset HTTP/1.1 200 OK日期: Wed,09 Jul 2014 17:19:35 GMT Content-Length: 13 Content-Type: text/plain;
=utf-8你好,世界!
使用压缩:
curl --dump-header http://localhost:3000/hello -H 'Accept-Encoding: gzip'HTTP/1.1 200 OK内容编码:
内容类型:文本/纯文本;charset=utf-8变化:接受-编码日期: Wed,09 Jul 2014 17:21:02 GMT Content-Length: 37�n��Q��J gzip
https://stackoverflow.com/questions/24088246
复制相似问题