Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support parsing request body #67

Merged
merged 4 commits into from
Aug 4, 2020
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,4 @@ Logo is created by [Mahmoud Sayed](https://www.facebook.com/mahmoudsayedae) and

#### Third-party library licenses
- [FastHTTP](https://github.com/valyala/fasthttp/blob/master/LICENSE)
- [json-iterator](https://github.com/json-iterator/go/blob/master/LICENSE)
52 changes: 51 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
package gearbox

import (
"fmt"
"strings"

jsoniter "github.com/json-iterator/go"
"github.com/valyala/fasthttp"
)

// MIME types
const (
MIMEApplicationJSON = "application/json"
)

// Context interface
type Context interface {
Next()
Context() *fasthttp.RequestCtx
Param(key string) string
Query(key string) string
SendBytes(value []byte) Context
SendString(value string) Context
SendJSON(in interface{}) error
Status(status int) Context
Set(key string, value string)
Get(key string) string
Body() string
ParseBody(out interface{}) error
}

// handlerFunc defines the handler used by middleware as return value.
Expand Down Expand Up @@ -50,12 +62,35 @@ func (ctx *context) Context() *fasthttp.RequestCtx {
return ctx.requestCtx
}

// SendString sets body of response as a string
// SendBytes sets body of response for []byte type
func (ctx *context) SendBytes(value []byte) Context {
ctx.requestCtx.Response.SetBodyRaw(value)
return ctx
}

// SendString sets body of response for string type
func (ctx *context) SendString(value string) Context {
ctx.requestCtx.SetBodyString(value)
return ctx
}

// SendJSON converts any interface to json, sets it to the body of response
// and sets content type header to application/json.
func (ctx *context) SendJSON(in interface{}) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
raw, err := json.Marshal(in)
// Check for errors
if err != nil {
return err
}

// Set http headers
ctx.requestCtx.Response.Header.SetContentType(MIMEApplicationJSON)
ctx.requestCtx.Response.SetBodyRaw(raw)

return nil
}

// Status sets the HTTP status code
func (ctx *context) Status(status int) Context {
ctx.requestCtx.Response.SetStatusCode(status)
Expand All @@ -81,3 +116,18 @@ func (ctx *context) Query(key string) string {
func (ctx *context) Body() string {
return GetString(ctx.requestCtx.Request.Body())
}

// ParseBody parses request body into provided struct
// Supports decoding theses types: application/json
func (ctx *context) ParseBody(out interface{}) error {
contentType := GetString(ctx.requestCtx.Request.Header.ContentType())
if strings.HasPrefix(contentType, MIMEApplicationJSON) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(ctx.requestCtx.Request.Body(), out)
}

return fmt.Errorf("content type '%s' is not supported, "+
"please open a request to support it "+
"(https://github.com/gogearbox/gearbox/issues/new?template=feature_request.md)",
contentType)
}
2 changes: 1 addition & 1 deletion gearbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// Exported constants
const (
Version = "1.1.0" // Version of gearbox
Version = "1.1.1" // Version of gearbox
Name = "Gearbox" // Name of gearbox
// http://patorjk.com/software/taag/#p=display&f=Big%20Money-ne&t=Gearbox
banner = `
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/gogearbox/gearbox

go 1.14

require github.com/valyala/fasthttp v1.15.1
require (
github.com/json-iterator/go v1.1.10
github.com/valyala/fasthttp v1.15.1
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/klauspost/compress v1.10.7 h1:7rix8v8GpI3ZBb0nSozFRgbtXKv+hOe+qfEpZqybrAg=
github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.15.1 h1:eRb5jzWhbCn/cGu3gNJMcOfPUfXgXCcQIOHjh9ajAS8=
Expand Down