diff --git a/api.go b/api.go index 480c792..2171275 100644 --- a/api.go +++ b/api.go @@ -130,7 +130,7 @@ var ( /** * Required handle for http module */ -func (api API) ServeHTTP(res http.ResponseWriter, req *http.Request) { +func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) { // STEP 1: initialize context ctx := Context{ diff --git a/api_test.go b/api_test.go index 9233993..f13a0ca 100644 --- a/api_test.go +++ b/api_test.go @@ -114,7 +114,7 @@ func TestAPI_ServeHTTP(t *testing.T) { ctx.JSON(`{"message": "Hello World!"}`) }) - dummy := httptest.NewServer(_api) + dummy := httptest.NewServer(&_api) defer dummy.Close() res, err := http.Get(dummy.URL) diff --git a/context.go b/context.go index 25605aa..dd796cb 100644 --- a/context.go +++ b/context.go @@ -6,11 +6,8 @@ package rest import ( - "bytes" - "compress/gzip" "net/http" "net/url" - "strings" "github.com/go-rs/rest-api-framework/render" ) @@ -134,7 +131,6 @@ func (ctx *Context) JSON(data interface{}) { Body: data, } body, err := json.Write(ctx.Response) - //ctx.SetHeader("Content-Type", "application/json;charset=UTF-8") ctx.send(body, err) } @@ -146,7 +142,6 @@ func (ctx *Context) Text(data string) { Body: data, } body, err := txt.Write(ctx.Response) - //ctx.SetHeader("Content-Type", "text/plain;charset=UTF-8") ctx.send(body, err) } @@ -165,30 +160,6 @@ func (ctx *Context) PostSend(task Task) { } ////////////////////////////////////////////////// -func compress(data []byte) (cdata []byte, err error) { - var b bytes.Buffer - gz := gzip.NewWriter(&b) - - _, err = gz.Write(data) - if err != nil { - return - } - - err = gz.Flush() - if err != nil { - return - } - - err = gz.Close() - if err != nil { - return - } - - cdata = b.Bytes() - - return -} - /** * Send data */ @@ -216,13 +187,6 @@ func (ctx *Context) send(data []byte, err error) { ctx.Response.Header().Set(key, val) } - if strings.Contains(ctx.Request.Header.Get("Accept-Encoding"), "gzip") { - data, err = compress(data) - if err == nil { - ctx.Response.Header().Set("Content-Encoding", "gzip") - } - } - ctx.Response.WriteHeader(ctx.status) _, err = ctx.Response.Write(data) diff --git a/examples/server.go b/examples/server.go index 732a46c..364e846 100644 --- a/examples/server.go +++ b/examples/server.go @@ -37,5 +37,5 @@ func main() { fmt.Println("Starting server.") - http.ListenAndServe(":8080", api) + http.ListenAndServe(":8080", &api) } diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f8c29d1 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/go-rs/rest-api-framework + +go 1.12