forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add gzip and cache controll for http response
- Loading branch information
1 parent
2b2ee67
commit fe353c4
Showing
5 changed files
with
126 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package responsewriter | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
func CacheMiddleware(suffixes ...string) mux.MiddlewareFunc { | ||
return mux.MiddlewareFunc(func(handler http.Handler) http.Handler { | ||
return Cache(handler, suffixes...) | ||
}) | ||
} | ||
|
||
func Cache(handler http.Handler, suffixes ...string) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
i := strings.LastIndex(r.URL.Path, ".") | ||
if i >= 0 { | ||
for _, suffix := range suffixes { | ||
if suffix == r.URL.Path[i+1:] { | ||
w.Header().Set("Cache-Control", "max-age=31536000, public") | ||
} | ||
} | ||
} | ||
handler.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
func NoCache(handler http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") | ||
handler.ServeHTTP(w, r) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package responsewriter | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
type ContentTypeWriter struct { | ||
http.ResponseWriter | ||
} | ||
|
||
func (c ContentTypeWriter) Write(b []byte) (int, error) { | ||
found := false | ||
for k := range c.Header() { | ||
if strings.EqualFold(k, "Content-Type") { | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
c.Header().Set("Content-Type", http.DetectContentType(b)) | ||
} | ||
return c.ResponseWriter.Write(b) | ||
} | ||
|
||
func ContentType(handler http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
writer := ContentTypeWriter{ResponseWriter: w} | ||
handler.ServeHTTP(writer, r) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package responsewriter | ||
|
||
import ( | ||
"compress/gzip" | ||
"io" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
type gzipResponseWriter struct { | ||
io.Writer | ||
http.ResponseWriter | ||
} | ||
|
||
func (w gzipResponseWriter) Write(b []byte) (int, error) { | ||
return w.Writer.Write(b) | ||
} | ||
|
||
func Gzip(handler http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { | ||
handler.ServeHTTP(w, r) | ||
return | ||
} | ||
gz := gzip.NewWriter(w) | ||
defer gz.Close() | ||
gzw := gzipResponseWriter{Writer: gz, ResponseWriter: w} | ||
gzw.Header().Set("Content-Encoding", "gzip") | ||
handler.ServeHTTP(gzw, r) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package responsewriter | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
type MiddlewareChain struct { | ||
middleWares []mux.MiddlewareFunc | ||
} | ||
|
||
func NewMiddlewareChain(middleWares ...mux.MiddlewareFunc) *MiddlewareChain { | ||
return &MiddlewareChain{middleWares: middleWares} | ||
} | ||
|
||
func (m *MiddlewareChain) Handler(handler http.Handler) http.Handler { | ||
rtn := handler | ||
for i := len(m.middleWares) - 1; i >= 0; i-- { | ||
w := m.middleWares[i] | ||
rtn = w.Middleware(rtn) | ||
} | ||
return rtn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters