File tree Expand file tree Collapse file tree 2 files changed +18
-3
lines changed
Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Original file line number Diff line number Diff line change 55package router
66
77import (
8+ "compress/gzip"
89 "encoding/json"
910 "net/http"
1011 "time"
@@ -33,6 +34,9 @@ type Control struct {
3334 // CompactJSON propery defines JSON output format (default is not compact)
3435 compactJSON bool
3536
37+ // gzip propery defines compressed output
38+ gzip bool
39+
3640 // Params is set of parameters
3741 Params []Param
3842
@@ -83,6 +87,11 @@ func (c *Control) CompactJSON(mode bool) {
8387 c .compactJSON = mode
8488}
8589
90+ // UseGZIP defines compressed output
91+ func (c * Control ) UseGZIP (mode bool ) {
92+ c .gzip = mode
93+ }
94+
8695// UseTimer allow caalculate elapsed time of request handling
8796func (c * Control ) UseTimer () {
8897 c .timer = time .Now ()
@@ -111,8 +120,14 @@ func (c *Control) Body(data interface{}) {
111120 }
112121 c .Writer .Header ().Add ("Content-type" , MIMEJSON )
113122 }
114- if c .code > 0 {
123+ if c .gzip {
124+ c .Writer .Header ().Add ("Content-Encoding" , "gzip" )
125+ c .Writer .WriteHeader (c .code )
126+ gz := gzip .NewWriter (c .Writer )
127+ gz .Write (content )
128+ gz .Close ()
129+ } else {
115130 c .Writer .WriteHeader (c .code )
131+ c .Writer .Write (content )
116132 }
117- c .Writer .Write (content )
118133}
Original file line number Diff line number Diff line change 33// license that can be found in the LICENSE file.
44
55/*
6- Package router 0.2.11 provides fast HTTP request router.
6+ Package router 0.2.12 provides fast HTTP request router.
77
88The router matches incoming requests by the request method and the path.
99If a handle is registered for this path and method, the router delegates the
You can’t perform that action at this time.
0 commit comments