2

In a quasi-embedded environment so speed is everything. I have found that if I compress my .html files, the app is speedier. Is there a flag or way in Martini to do this on the fly?

2 Answers2

5

You can use gzip Middleware

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
  • 46,639
  • 15
  • 102
  • 119
5

This answer is just to show that @fabrizioM's answer actually works:

Step 1: Create the server

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()
}

Step 2: Run the server

go run main.go

Step 3: Try the server

This is the step where you must remember to include the Accept-Encoding: gzip header (or equivalent).

Without compression:

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!

With compression:

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
Attila O.
  • 15,659
  • 11
  • 54
  • 84
  • `curl --dump-header - http://localhost:3000/hello -H 'Accept-Encoding: gzip'` if you get 'curl: no URL specified!' error. – marc_aragones May 10 '16 at 16:17