Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static content under public/ with martini.Classic() will not get compressed #9

Open
djhworld opened this issue Aug 7, 2014 · 2 comments

Comments

@djhworld
Copy link

djhworld commented Aug 7, 2014

I've noticed this on my machine, using the example in the README

func main() {
  m := martini.Classic()
  // gzip every request
  m.Use(gzip.All())
  m.Run()
}

All requests after the line m.Use(gzip.All()) will be compressed with gzip (assuming the HTTP request has the correct header) - this is correct as per the requirement in the README Make sure to include the Gzip middleware above other middleware that alter the response body

However, if you have static content under public, none of that will be gzipped. This is because the martini.Classic constructs its handler for static content within the Classic() constructor (see https://github.com/go-martini/martini/blob/master/martini.go#L112), crucially this is before we add our gzip handler.

There are a few ways to resolve this

  1. Store all static content you want to be gzipped in a different folder and then add m.Use(martini.Static("myfolder")) in your code.
  2. Create a new function ClassicGzipped (this is my current approach) that has all the handlers from martini.Classic() but puts the line m.Use(gzip.All()) first, e.g.
    func ClassicGzipped() *martini.ClassicMartini {
        r := NewRouter()
        m := New()
        m.Use(Logger())
        m.Use(Recovery())
        //gzip all requests, including content under public/
        m.Use(gzip.All())
        m.Use(Static("public"))
        m.MapTo(r, (*Routes)(nil))
        m.Action(r.Handle)
        return &ClassicMartini{m, r}
    }

This isn't really a bug, but just an observation - if we agree I'm happy to update the README detailing this gotcha?

@djhworld
Copy link
Author

Any steer on this?

@mrsln
Copy link

mrsln commented Jun 15, 2015

I run into the same issue. I guess @djhworld has his own repo, but I had to change a bit his code:

func ClassicGzipped() *martini.ClassicMartini {
    r := martini.NewRouter()
    m := martini.New()
    m.Use(martini.Logger())
    m.Use(martini.Recovery())
    m.Use(gzip.All())
    m.Use(martini.Static("public"))
    m.MapTo(r, (*martini.Routes)(nil))
    m.Action(r.Handle)
    return &martini.ClassicMartini{m, r}
}

p.s maybe the issue should be opened on the main repo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants