-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
89 additions
and
0 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,89 @@ | ||
// Copyright (c) 2018, Janoš Guljaš <[email protected]> | ||
// All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package minifier | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"regexp" | ||
|
||
"github.com/tdewolff/minify" | ||
"github.com/tdewolff/minify/css" | ||
"github.com/tdewolff/minify/html" | ||
"github.com/tdewolff/minify/js" | ||
"github.com/tdewolff/minify/json" | ||
"github.com/tdewolff/minify/svg" | ||
"github.com/tdewolff/minify/xml" | ||
) | ||
|
||
var minifier = minify.New() | ||
|
||
func init() { | ||
htmlMinify := &html.Minifier{ | ||
KeepWhitespace: true, | ||
} | ||
minifier.AddFunc("text/css", css.Minify) | ||
minifier.Add("text/html", htmlMinify) | ||
minifier.Add("text/x-template", htmlMinify) | ||
minifier.AddFunc("text/javascript", js.Minify) | ||
minifier.AddFunc("application/javascript", js.Minify) | ||
minifier.AddFunc("application/x-javascript", js.Minify) | ||
minifier.AddFunc("image/svg+xml", svg.Minify) | ||
minifier.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify) | ||
minifier.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify) | ||
} | ||
|
||
// LogFunc defines the function that is used by NewHandler | ||
// to log error messages from minifier. | ||
type LogFunc func(format string, a ...interface{}) | ||
|
||
// NewHandler returns a minifer http handler that implements common http.Hander | ||
// related interfaces and uses a common configuration for github.com/tdewolff/minify. | ||
func NewHandler(h http.Handler, logFunc LogFunc) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
mw := &minifyResponseWriter{mw: minifier.ResponseWriter(w, r), w: w} | ||
h.ServeHTTP(mw, r) | ||
if err := mw.Close(); err != nil && err != minify.ErrNotExist && logFunc != nil { | ||
logFunc("minifier %q: %v", r.URL.String(), err) | ||
} | ||
}) | ||
} | ||
|
||
type minifyResponseWriter struct { | ||
mw http.ResponseWriter | ||
w http.ResponseWriter | ||
} | ||
|
||
func (w *minifyResponseWriter) Header() http.Header { | ||
return w.mw.Header() | ||
} | ||
|
||
func (w *minifyResponseWriter) CloseNotify() <-chan bool { | ||
return w.w.(http.CloseNotifier).CloseNotify() | ||
} | ||
|
||
func (w *minifyResponseWriter) Flush() { | ||
w.w.(http.Flusher).Flush() | ||
} | ||
|
||
func (w *minifyResponseWriter) Write(b []byte) (int, error) { | ||
return w.mw.Write(b) | ||
} | ||
|
||
func (w *minifyResponseWriter) WriteHeader(s int) { | ||
w.mw.WriteHeader(s) | ||
} | ||
|
||
func (w *minifyResponseWriter) Push(target string, opts *http.PushOptions) error { | ||
return w.w.(http.Pusher).Push(target, opts) | ||
} | ||
|
||
func (w *minifyResponseWriter) Close() error { | ||
if c, ok := w.mw.(io.Closer); ok { | ||
return c.Close() | ||
} | ||
return nil | ||
} |