mirror of
https://github.com/labstack/echo.git
synced 2024-11-24 08:22:21 +02:00
Added response already committed logic
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
91bbd38251
commit
05e8fee3de
9
bolt.go
9
bolt.go
@ -21,7 +21,6 @@ type (
|
||||
|
||||
const (
|
||||
MIME_JSON = "application/json"
|
||||
MIME_MP = "application/x-msgpack"
|
||||
|
||||
HeaderAccept = "Accept"
|
||||
HeaderContentDisposition = "Content-Disposition"
|
||||
@ -46,12 +45,15 @@ func New(opts ...func(*Bolt)) (b *Bolt) {
|
||||
maxParam: 5,
|
||||
notFoundHandler: func(c *Context) {
|
||||
http.Error(c.Response, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||
c.Halt()
|
||||
},
|
||||
methodNotAllowedHandler: func(c *Context) {
|
||||
http.Error(c.Response, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
||||
c.Halt()
|
||||
},
|
||||
internalServerErrorHandler: func(c *Context) {
|
||||
http.Error(c.Response, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
c.Halt()
|
||||
},
|
||||
}
|
||||
b.Router = NewRouter(b)
|
||||
@ -61,6 +63,7 @@ func New(opts ...func(*Bolt)) (b *Bolt) {
|
||||
params: make(Params, b.maxParam),
|
||||
store: make(store),
|
||||
i: -1,
|
||||
bolt: b,
|
||||
}
|
||||
}
|
||||
|
||||
@ -143,7 +146,6 @@ func (b *Bolt) Handle(method, path string, h []HandlerFunc) {
|
||||
b.Router.Add(method, path, func(c *Context) {
|
||||
c.handlers = h
|
||||
c.l = l
|
||||
c.i = -1
|
||||
c.Next()
|
||||
})
|
||||
}
|
||||
@ -152,8 +154,7 @@ func (b *Bolt) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||
// Find and execute handler
|
||||
h, c, s := b.Router.Find(r.Method, r.URL.Path)
|
||||
if h != nil {
|
||||
c.Response.ResponseWriter = rw
|
||||
c.Request = r
|
||||
c.reset(rw, r)
|
||||
h(c)
|
||||
} else {
|
||||
if s == NotFound {
|
||||
|
31
context.go
31
context.go
@ -3,6 +3,7 @@ package bolt
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type (
|
||||
@ -30,18 +31,28 @@ func (c *Context) Param(n string) string {
|
||||
//**********
|
||||
// Bind
|
||||
//**********
|
||||
func (c *Context) BindJSON(i interface{}) {
|
||||
dec := json.NewDecoder(c.Request.Body)
|
||||
if err := dec.Decode(i); err != nil {
|
||||
c.bolt.internalServerErrorHandler(c)
|
||||
func (c *Context) Bind(i interface{}) bool {
|
||||
var err error
|
||||
ct := c.Request.Header.Get(HeaderContentType)
|
||||
if strings.HasPrefix(ct, MIME_JSON) {
|
||||
dec := json.NewDecoder(c.Request.Body)
|
||||
err = dec.Decode(i)
|
||||
} else {
|
||||
// TODO:
|
||||
}
|
||||
if err != nil {
|
||||
c.bolt.internalServerErrorHandler(c)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
//************
|
||||
// Render
|
||||
//************
|
||||
func (c *Context) RenderJSON(n int, i interface{}) {
|
||||
func (c *Context) JSON(n int, i interface{}) {
|
||||
enc := json.NewEncoder(c.Response)
|
||||
c.Response.Header().Set(HeaderContentType, MIME_JSON+"; charset=utf-8")
|
||||
c.Response.WriteHeader(n)
|
||||
if err := enc.Encode(i); err != nil {
|
||||
c.bolt.internalServerErrorHandler(c)
|
||||
@ -67,3 +78,13 @@ func (c *Context) Set(key string, val interface{}) {
|
||||
func (c *Context) Redirect(n int, url string) {
|
||||
http.Redirect(c.Response, c.Request, url, n)
|
||||
}
|
||||
|
||||
func (c *Context) reset(rw http.ResponseWriter, r *http.Request) {
|
||||
c.Response.reset(rw)
|
||||
c.Request = r
|
||||
c.i = -1
|
||||
}
|
||||
|
||||
func (c *Context) Halt() {
|
||||
c.i = c.l
|
||||
}
|
||||
|
@ -7,8 +7,8 @@ import (
|
||||
)
|
||||
|
||||
type user struct {
|
||||
Id string
|
||||
Name string
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
var users map[string]*user
|
||||
@ -16,25 +16,31 @@ var users map[string]*user
|
||||
func init() {
|
||||
users = map[string]*user{
|
||||
"1": &user{
|
||||
Id: "1",
|
||||
ID: "1",
|
||||
Name: "Wreck-It Ralph",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func createUser(c *bolt.Context) {
|
||||
u := new(user)
|
||||
if c.Bind(u) {
|
||||
users[u.ID] = u
|
||||
c.JSON(http.StatusOK, u)
|
||||
}
|
||||
}
|
||||
|
||||
func getUsers(c *bolt.Context) {
|
||||
c.RenderJSON(http.StatusOK, users)
|
||||
c.JSON(http.StatusOK, users)
|
||||
}
|
||||
|
||||
func getUser(c *bolt.Context) {
|
||||
c.RenderJSON(http.StatusOK, users[c.P(0)])
|
||||
c.JSON(http.StatusOK, users[c.P(0)])
|
||||
}
|
||||
|
||||
func main() {
|
||||
b := bolt.New()
|
||||
b.Post("/users", createUser)
|
||||
b.Get("/users", getUsers)
|
||||
b.Get("/users/:id", getUser)
|
||||
b.Run(":8080")
|
||||
|
17
response.go
17
response.go
@ -3,6 +3,7 @@ package bolt
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
@ -10,14 +11,21 @@ import (
|
||||
type (
|
||||
response struct {
|
||||
http.ResponseWriter
|
||||
status int
|
||||
size int
|
||||
status int
|
||||
size int
|
||||
committed bool
|
||||
}
|
||||
)
|
||||
|
||||
func (r *response) WriteHeader(c int) {
|
||||
if r.committed {
|
||||
// TODO: Warning
|
||||
log.Println("bolt: response already committed")
|
||||
return
|
||||
}
|
||||
r.status = c
|
||||
r.ResponseWriter.WriteHeader(c)
|
||||
r.committed = true
|
||||
}
|
||||
|
||||
func (r *response) Write(b []byte) (n int, err error) {
|
||||
@ -55,3 +63,8 @@ func (r *response) Status() int {
|
||||
func (r *response) Size() int {
|
||||
return r.size
|
||||
}
|
||||
|
||||
func (r *response) reset(rw http.ResponseWriter) {
|
||||
r.ResponseWriter = rw
|
||||
r.committed = false
|
||||
}
|
||||
|
17
router.go
17
router.go
@ -3,7 +3,6 @@ package bolt
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@ -66,22 +65,6 @@ func (n *node) findEdge(l byte) *node {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e edges) Len() int {
|
||||
return len(e)
|
||||
}
|
||||
|
||||
func (e edges) Swap(i, j int) {
|
||||
e[i], e[j] = e[j], e[i]
|
||||
}
|
||||
|
||||
func (e edges) Less(i, j int) bool {
|
||||
return e[i].label < e[j].label
|
||||
}
|
||||
|
||||
func (e edges) Sort() {
|
||||
sort.Sort(e)
|
||||
}
|
||||
|
||||
func (r *router) Add(method, path string, h HandlerFunc) {
|
||||
i := 0
|
||||
l := len(path)
|
||||
|
Loading…
Reference in New Issue
Block a user