Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 0 additions & 36 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
package rest

import (
"bytes"
"compress/gzip"
"net/http"
"net/url"
"strings"

"github.com/go-rs/rest-api-framework/render"
)
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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
*/
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ func main() {

fmt.Println("Starting server.")

http.ListenAndServe(":8080", api)
http.ListenAndServe(":8080", &api)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/go-rs/rest-api-framework

go 1.12