1
0
mirror of https://github.com/labstack/echo.git synced 2025-04-27 12:32:09 +02:00

Added response already committed logic

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-03-06 11:12:33 -08:00
parent 91bbd38251
commit 05e8fee3de
5 changed files with 57 additions and 33 deletions

View File

@ -21,7 +21,6 @@ type (
const ( const (
MIME_JSON = "application/json" MIME_JSON = "application/json"
MIME_MP = "application/x-msgpack"
HeaderAccept = "Accept" HeaderAccept = "Accept"
HeaderContentDisposition = "Content-Disposition" HeaderContentDisposition = "Content-Disposition"
@ -46,12 +45,15 @@ func New(opts ...func(*Bolt)) (b *Bolt) {
maxParam: 5, maxParam: 5,
notFoundHandler: func(c *Context) { notFoundHandler: func(c *Context) {
http.Error(c.Response, http.StatusText(http.StatusNotFound), http.StatusNotFound) http.Error(c.Response, http.StatusText(http.StatusNotFound), http.StatusNotFound)
c.Halt()
}, },
methodNotAllowedHandler: func(c *Context) { methodNotAllowedHandler: func(c *Context) {
http.Error(c.Response, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) http.Error(c.Response, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
c.Halt()
}, },
internalServerErrorHandler: func(c *Context) { internalServerErrorHandler: func(c *Context) {
http.Error(c.Response, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(c.Response, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
c.Halt()
}, },
} }
b.Router = NewRouter(b) b.Router = NewRouter(b)
@ -61,6 +63,7 @@ func New(opts ...func(*Bolt)) (b *Bolt) {
params: make(Params, b.maxParam), params: make(Params, b.maxParam),
store: make(store), store: make(store),
i: -1, 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) { b.Router.Add(method, path, func(c *Context) {
c.handlers = h c.handlers = h
c.l = l c.l = l
c.i = -1
c.Next() c.Next()
}) })
} }
@ -152,8 +154,7 @@ func (b *Bolt) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
// Find and execute handler // Find and execute handler
h, c, s := b.Router.Find(r.Method, r.URL.Path) h, c, s := b.Router.Find(r.Method, r.URL.Path)
if h != nil { if h != nil {
c.Response.ResponseWriter = rw c.reset(rw, r)
c.Request = r
h(c) h(c)
} else { } else {
if s == NotFound { if s == NotFound {

View File

@ -3,6 +3,7 @@ package bolt
import ( import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"strings"
) )
type ( type (
@ -30,18 +31,28 @@ func (c *Context) Param(n string) string {
//********** //**********
// Bind // Bind
//********** //**********
func (c *Context) BindJSON(i interface{}) { 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) dec := json.NewDecoder(c.Request.Body)
if err := dec.Decode(i); err != nil { err = dec.Decode(i)
c.bolt.internalServerErrorHandler(c) } else {
// TODO:
} }
if err != nil {
c.bolt.internalServerErrorHandler(c)
return false
}
return true
} }
//************ //************
// Render // Render
//************ //************
func (c *Context) RenderJSON(n int, i interface{}) { func (c *Context) JSON(n int, i interface{}) {
enc := json.NewEncoder(c.Response) enc := json.NewEncoder(c.Response)
c.Response.Header().Set(HeaderContentType, MIME_JSON+"; charset=utf-8")
c.Response.WriteHeader(n) c.Response.WriteHeader(n)
if err := enc.Encode(i); err != nil { if err := enc.Encode(i); err != nil {
c.bolt.internalServerErrorHandler(c) c.bolt.internalServerErrorHandler(c)
@ -67,3 +78,13 @@ func (c *Context) Set(key string, val interface{}) {
func (c *Context) Redirect(n int, url string) { func (c *Context) Redirect(n int, url string) {
http.Redirect(c.Response, c.Request, url, n) 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
}

View File

@ -7,8 +7,8 @@ import (
) )
type user struct { type user struct {
Id string ID string `json:"id"`
Name string Name string `json:"name"`
} }
var users map[string]*user var users map[string]*user
@ -16,25 +16,31 @@ var users map[string]*user
func init() { func init() {
users = map[string]*user{ users = map[string]*user{
"1": &user{ "1": &user{
Id: "1", ID: "1",
Name: "Wreck-It Ralph", Name: "Wreck-It Ralph",
}, },
} }
} }
func createUser(c *bolt.Context) { 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) { func getUsers(c *bolt.Context) {
c.RenderJSON(http.StatusOK, users) c.JSON(http.StatusOK, users)
} }
func getUser(c *bolt.Context) { func getUser(c *bolt.Context) {
c.RenderJSON(http.StatusOK, users[c.P(0)]) c.JSON(http.StatusOK, users[c.P(0)])
} }
func main() { func main() {
b := bolt.New() b := bolt.New()
b.Post("/users", createUser)
b.Get("/users", getUsers) b.Get("/users", getUsers)
b.Get("/users/:id", getUser) b.Get("/users/:id", getUser)
b.Run(":8080") b.Run(":8080")

View File

@ -3,6 +3,7 @@ package bolt
import ( import (
"bufio" "bufio"
"errors" "errors"
"log"
"net" "net"
"net/http" "net/http"
) )
@ -12,12 +13,19 @@ type (
http.ResponseWriter http.ResponseWriter
status int status int
size int size int
committed bool
} }
) )
func (r *response) WriteHeader(c int) { func (r *response) WriteHeader(c int) {
if r.committed {
// TODO: Warning
log.Println("bolt: response already committed")
return
}
r.status = c r.status = c
r.ResponseWriter.WriteHeader(c) r.ResponseWriter.WriteHeader(c)
r.committed = true
} }
func (r *response) Write(b []byte) (n int, err error) { func (r *response) Write(b []byte) (n int, err error) {
@ -55,3 +63,8 @@ func (r *response) Status() int {
func (r *response) Size() int { func (r *response) Size() int {
return r.size return r.size
} }
func (r *response) reset(rw http.ResponseWriter) {
r.ResponseWriter = rw
r.committed = false
}

View File

@ -3,7 +3,6 @@ package bolt
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"sort"
"sync" "sync"
) )
@ -66,22 +65,6 @@ func (n *node) findEdge(l byte) *node {
return nil 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) { func (r *router) Add(method, path string, h HandlerFunc) {
i := 0 i := 0
l := len(path) l := len(path)