Add Reader
This commit is contained in:
parent
69754a9946
commit
4d46f5c770
11
default.go
11
default.go
@ -1,6 +1,7 @@
|
|||||||
package router
|
package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -16,3 +17,13 @@ func defaultErrorHandler(c *Context, err interface{}) {
|
|||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
c.String(500, `internal server error`)
|
c.String(500, `internal server error`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func defaultReader(c *Context, dst interface{}) bool {
|
||||||
|
err := json.NewDecoder(c.Request.Body).Decode(dst)
|
||||||
|
if err != nil {
|
||||||
|
c.NoContent(400)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
12
router.go
12
router.go
@ -1,7 +1,6 @@
|
|||||||
package router
|
package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
@ -24,9 +23,13 @@ type ErrorHandle func(*Context, interface{})
|
|||||||
// Middleware TODO:
|
// Middleware TODO:
|
||||||
type Middleware func(Handle) Handle
|
type Middleware func(Handle) Handle
|
||||||
|
|
||||||
|
// Binder reads input to dst, returns true is successful
|
||||||
|
type Reader func(c *Context, dst interface{}) bool
|
||||||
|
|
||||||
// Router is the router itself
|
// Router is the router itself
|
||||||
type Router struct {
|
type Router struct {
|
||||||
routes []route
|
routes []route
|
||||||
|
Reader Reader
|
||||||
Renderer Renderer
|
Renderer Renderer
|
||||||
middleware []Middleware
|
middleware []Middleware
|
||||||
NotFoundHandler Handle
|
NotFoundHandler Handle
|
||||||
@ -36,7 +39,7 @@ type Router struct {
|
|||||||
|
|
||||||
// New returns a new Router
|
// New returns a new Router
|
||||||
func New() *Router {
|
func New() *Router {
|
||||||
return &Router{NotFoundHandler: defaultNotFoundHandler, MethodNotAllowedHandler: defaultMethodNotAllowedHandler, ErrorHandler: defaultErrorHandler}
|
return &Router{Reader: defaultReader, NotFoundHandler: defaultNotFoundHandler, MethodNotAllowedHandler: defaultMethodNotAllowedHandler, ErrorHandler: defaultErrorHandler}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use adds a global middleware
|
// Use adds a global middleware
|
||||||
@ -161,12 +164,11 @@ func handlePOST(r *Router, f interface{}) Handle {
|
|||||||
return func(c *Context) error {
|
return func(c *Context) error {
|
||||||
data := reflect.New(inputRt)
|
data := reflect.New(inputRt)
|
||||||
|
|
||||||
err := json.NewDecoder(c.Request.Body).Decode(data.Interface())
|
if !r.Reader(c, data.Interface()) {
|
||||||
c.Request.Body.Close()
|
c.Request.Body.Close()
|
||||||
if err != nil {
|
|
||||||
c.NoContent(400) // TODO: send info about error (BindError)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
c.Request.Body.Close()
|
||||||
|
|
||||||
out := funcRv.Call([]reflect.Value{reflect.ValueOf(c), data.Elem()})
|
out := funcRv.Call([]reflect.Value{reflect.ValueOf(c), data.Elem()})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user