Skip to content

Commit e157653

Browse files
committed
- Created separate handler for response payload compression
- Passing pointer to server instead of value
1 parent d73b335 commit e157653

File tree

5 files changed

+6
-39
lines changed

5 files changed

+6
-39
lines changed

api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ var (
130130
/**
131131
* Required handle for http module
132132
*/
133-
func (api API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
133+
func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
134134

135135
// STEP 1: initialize context
136136
ctx := Context{

api_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func TestAPI_ServeHTTP(t *testing.T) {
114114
ctx.JSON(`{"message": "Hello World!"}`)
115115
})
116116

117-
dummy := httptest.NewServer(_api)
117+
dummy := httptest.NewServer(&_api)
118118
defer dummy.Close()
119119

120120
res, err := http.Get(dummy.URL)

context.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
package rest
77

88
import (
9-
"bytes"
10-
"compress/gzip"
119
"net/http"
1210
"net/url"
13-
"strings"
1411

1512
"github.com/go-rs/rest-api-framework/render"
1613
)
@@ -134,7 +131,6 @@ func (ctx *Context) JSON(data interface{}) {
134131
Body: data,
135132
}
136133
body, err := json.Write(ctx.Response)
137-
//ctx.SetHeader("Content-Type", "application/json;charset=UTF-8")
138134
ctx.send(body, err)
139135
}
140136

@@ -146,7 +142,6 @@ func (ctx *Context) Text(data string) {
146142
Body: data,
147143
}
148144
body, err := txt.Write(ctx.Response)
149-
//ctx.SetHeader("Content-Type", "text/plain;charset=UTF-8")
150145
ctx.send(body, err)
151146
}
152147

@@ -165,30 +160,6 @@ func (ctx *Context) PostSend(task Task) {
165160
}
166161

167162
//////////////////////////////////////////////////
168-
func compress(data []byte) (cdata []byte, err error) {
169-
var b bytes.Buffer
170-
gz := gzip.NewWriter(&b)
171-
172-
_, err = gz.Write(data)
173-
if err != nil {
174-
return
175-
}
176-
177-
err = gz.Flush()
178-
if err != nil {
179-
return
180-
}
181-
182-
err = gz.Close()
183-
if err != nil {
184-
return
185-
}
186-
187-
cdata = b.Bytes()
188-
189-
return
190-
}
191-
192163
/**
193164
* Send data
194165
*/
@@ -216,13 +187,6 @@ func (ctx *Context) send(data []byte, err error) {
216187
ctx.Response.Header().Set(key, val)
217188
}
218189

219-
if strings.Contains(ctx.Request.Header.Get("Accept-Encoding"), "gzip") {
220-
data, err = compress(data)
221-
if err == nil {
222-
ctx.Response.Header().Set("Content-Encoding", "gzip")
223-
}
224-
}
225-
226190
ctx.Response.WriteHeader(ctx.status)
227191

228192
_, err = ctx.Response.Write(data)

examples/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ func main() {
3737

3838
fmt.Println("Starting server.")
3939

40-
http.ListenAndServe(":8080", api)
40+
http.ListenAndServe(":8080", &api)
4141
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/go-rs/rest-api-framework
2+
3+
go 1.12

0 commit comments

Comments
 (0)