-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
switch to httprouter add gzip/brotli compression if client allows
- Loading branch information
Showing
65 changed files
with
158 additions
and
1,877 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 |
---|---|---|
@@ -1,52 +1,23 @@ | ||
package main | ||
|
||
//go:generate go-bindata -o bindata.go public/... templates/... | ||
|
||
import ( | ||
"fmt" | ||
"html/template" | ||
"path" | ||
) | ||
|
||
func parseAssetsWithFunc(funcMap template.FuncMap, filenames ...string) (*template.Template, error) { | ||
if len(filenames) == 0 { | ||
// Not really a problem, but be consistent. | ||
return nil, fmt.Errorf("html/template: no files named in call to ParseFiles") | ||
} | ||
tresult := template.New("result").Funcs(funcMap) | ||
|
||
for _, filename := range filenames { | ||
tdata, aerr := Asset(filename) | ||
if aerr != nil { | ||
return nil, aerr | ||
} | ||
|
||
_, err := tresult.Parse(string(tdata)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return tresult, nil | ||
return template.New(path.Base(filenames[0])).Funcs(funcMap).ParseFS(contentTemplates, filenames...) | ||
} | ||
|
||
func parseAssets(filenames ...string) (*template.Template, error) { | ||
if len(filenames) == 0 { | ||
// Not really a problem, but be consistent. | ||
return nil, fmt.Errorf("html/template: no files named in call to ParseFiles") | ||
} | ||
tresult := template.New("result") | ||
|
||
for _, filename := range filenames { | ||
tdata, aerr := Asset(filename) | ||
if aerr != nil { | ||
return nil, aerr | ||
} | ||
|
||
_, err := tresult.Parse(string(tdata)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return tresult, nil | ||
return template.New(path.Base(filenames[0])).ParseFS(contentTemplates, filenames...) | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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
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
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,50 @@ | ||
package httpcompress | ||
|
||
import ( | ||
"compress/gzip" | ||
"io" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/andybalholm/brotli" | ||
"github.com/julienschmidt/httprouter" | ||
) | ||
|
||
// CompressResponseWriter is a Struct for manipulating io writer | ||
type CompressResponseWriter struct { | ||
io.Writer | ||
http.ResponseWriter | ||
} | ||
|
||
func (res CompressResponseWriter) Write(b []byte) (int, error) { | ||
if "" == res.Header().Get("Content-Type") { | ||
// If no content type, apply sniffing algorithm to un-gzipped body. | ||
res.Header().Set("Content-Type", http.DetectContentType(b)) | ||
} | ||
return res.Writer.Write(b) | ||
} | ||
|
||
// Middleware force - bool, whether or not to force Compression regardless of the sent headers. | ||
func Middleware(fn httprouter.Handle, force bool) httprouter.Handle { | ||
return func(res http.ResponseWriter, req *http.Request, pm httprouter.Params) { | ||
if !strings.Contains(req.Header.Get("Accept-Encoding"), "br") { | ||
if !strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") && !force { | ||
fn(res, req, pm) | ||
return | ||
} | ||
res.Header().Set("Vary", "Accept-Encoding") | ||
res.Header().Set("Content-Encoding", "gzip") | ||
gz := gzip.NewWriter(res) | ||
defer gz.Close() | ||
cw := CompressResponseWriter{Writer: gz, ResponseWriter: res} | ||
fn(cw, req, pm) | ||
return | ||
} | ||
res.Header().Set("Vary", "Accept-Encoding") | ||
res.Header().Set("Content-Encoding", "br") | ||
br := brotli.NewWriter(res) | ||
defer br.Close() | ||
cw := CompressResponseWriter{Writer: br, ResponseWriter: res} | ||
fn(cw, req, pm) | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
Oops, something went wrong.