【问题标题】:Anyway to compress HTML output with Martini?无论如何用 Martini 压缩 HTML 输出?
【发布时间】:2014-07-28 02:27:42
【问题描述】:

在准嵌入式环境中,速度就是一切。我发现如果我压缩我的 .html 文件,应用程序会更快。马提尼酒中是否有标志或方式可以即时执行此操作?

【问题讨论】:

    标签: go martini


    【解决方案1】:

    你可以使用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()
    }
    

    【讨论】:

      【解决方案2】:

      这个答案只是为了表明@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()
      }
      

      第 2 步:运行服务器

      go run main.go
      

      第 3 步:试用服务器

      这是您必须记住包含 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!'错误。
      猜你喜欢
      • 1970-01-01
      • 2011-10-11
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      • 2016-03-17
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多