-
Notifications
You must be signed in to change notification settings - Fork 1
/
minify.go
95 lines (79 loc) · 1.95 KB
/
minify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package middleware
import (
"bytes"
"net/http"
"regexp"
"strconv"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/css"
"github.com/tdewolff/minify/html"
"github.com/tdewolff/minify/js"
)
var (
minifier *minify.Minify
mediaType *regexp.Regexp
)
func init() {
minifier = minify.New()
minifier.AddFunc("text/css", css.Minify)
minifier.AddFunc("text/html", html.Minify)
minifier.AddFunc("text/javascript", js.Minify)
mediaType = regexp.MustCompile("text/[html|css|javascript]")
}
type minifyWriter struct {
http.ResponseWriter
Body *bytes.Buffer
code int
wroteHeader bool
}
func (m *minifyWriter) Header() http.Header {
return m.ResponseWriter.Header()
}
func (m *minifyWriter) WriteHeader(code int) {
if !m.wroteHeader {
m.code = code
m.wroteHeader = true
m.ResponseWriter.WriteHeader(code)
}
}
func (m *minifyWriter) Write(b []byte) (int, error) {
h := m.ResponseWriter.Header()
if h.Get("Content-Type") == "" {
h.Set("Content-Type", http.DetectContentType(b))
}
if !m.wroteHeader {
m.WriteHeader(http.StatusOK)
}
if m.Body != nil {
m.Body.Write(b)
}
return len(b), nil
}
/*
Minify middleware is simple and generic using 'tdewolff/minify' package.
Middleware minifies HTML, Inline CSS and Inline JS. Compatible with Goji,
Gorilla, Gin & net/http (amongst many others).
Refer: https://github.com/jeevatkm/middleware/examples
*/
func Minify(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mw := &minifyWriter{
ResponseWriter: w,
Body: &bytes.Buffer{},
}
h.ServeHTTP(mw, r)
hdr := w.Header()
ct := hdr.Get("Content-Type")
if mediaType.MatchString(ct) {
rb, err := minify.Bytes(minifier, ct, mw.Body.Bytes())
if err != nil {
_ = err // unsupported mediatype error or internal
}
hdr.Del("Content-Length")
hdr.Set("Content-Length", strconv.Itoa(len(rb)))
w.Write(rb)
} else {
w.Write(mw.Body.Bytes())
}
})
}