mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 04:57:07 +08:00
expose status code (or anything) by wrapping http.ResponseWriter
This commit is contained in:
parent
5eb0e10a78
commit
a6c6ebce8f
16
gin.go
16
gin.go
@ -35,7 +35,7 @@ type (
|
|||||||
// manage the flow, validate the JSON of a request and render a JSON response for example.
|
// manage the flow, validate the JSON of a request and render a JSON response for example.
|
||||||
Context struct {
|
Context struct {
|
||||||
Req *http.Request
|
Req *http.Request
|
||||||
Writer http.ResponseWriter
|
Writer ResponseWriter
|
||||||
Keys map[string]interface{}
|
Keys map[string]interface{}
|
||||||
Errors ErrorMsgs
|
Errors ErrorMsgs
|
||||||
Params httprouter.Params
|
Params httprouter.Params
|
||||||
@ -101,7 +101,7 @@ func (engine *Engine) handle404(w http.ResponseWriter, req *http.Request) {
|
|||||||
handlers := engine.combineHandlers(engine.handlers404)
|
handlers := engine.combineHandlers(engine.handlers404)
|
||||||
c := engine.createContext(w, req, nil, handlers)
|
c := engine.createContext(w, req, nil, handlers)
|
||||||
if engine.handlers404 == nil {
|
if engine.handlers404 == nil {
|
||||||
http.NotFound(c.Writer, c.Req)
|
http.NotFound(&c.Writer, c.Req)
|
||||||
} else {
|
} else {
|
||||||
c.Writer.WriteHeader(404)
|
c.Writer.WriteHeader(404)
|
||||||
}
|
}
|
||||||
@ -125,7 +125,7 @@ func (engine *Engine) ServeFiles(path string, root http.FileSystem) {
|
|||||||
|
|
||||||
// ServeHTTP makes the router implement the http.Handler interface.
|
// ServeHTTP makes the router implement the http.Handler interface.
|
||||||
func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
engine.router.ServeHTTP(w, req)
|
engine.router.ServeHTTP(&ResponseWriter{w, -1}, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) Run(addr string) {
|
func (engine *Engine) Run(addr string) {
|
||||||
@ -138,7 +138,7 @@ func (engine *Engine) Run(addr string) {
|
|||||||
|
|
||||||
func (group *RouterGroup) createContext(w http.ResponseWriter, req *http.Request, params httprouter.Params, handlers []HandlerFunc) *Context {
|
func (group *RouterGroup) createContext(w http.ResponseWriter, req *http.Request, params httprouter.Params, handlers []HandlerFunc) *Context {
|
||||||
return &Context{
|
return &Context{
|
||||||
Writer: w,
|
Writer: ResponseWriter{w, -1},
|
||||||
Req: req,
|
Req: req,
|
||||||
index: -1,
|
index: -1,
|
||||||
engine: group.engine,
|
engine: group.engine,
|
||||||
@ -178,7 +178,7 @@ func (group *RouterGroup) Handle(method, p string, handlers []HandlerFunc) {
|
|||||||
p = path.Join(group.prefix, p)
|
p = path.Join(group.prefix, p)
|
||||||
handlers = group.combineHandlers(handlers)
|
handlers = group.combineHandlers(handlers)
|
||||||
group.engine.router.Handle(method, p, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
|
group.engine.router.Handle(method, p, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
|
||||||
group.createContext(w, req, params, handlers).Next()
|
group.createContext(&ResponseWriter{w, -1}, req, params, handlers).Next()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -330,7 +330,7 @@ func (c *Context) JSON(code int, obj interface{}) {
|
|||||||
encoder := json.NewEncoder(c.Writer)
|
encoder := json.NewEncoder(c.Writer)
|
||||||
if err := encoder.Encode(obj); err != nil {
|
if err := encoder.Encode(obj); err != nil {
|
||||||
c.Error(err, obj)
|
c.Error(err, obj)
|
||||||
http.Error(c.Writer, err.Error(), 500)
|
http.Error(&c.Writer, err.Error(), 500)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -344,7 +344,7 @@ func (c *Context) XML(code int, obj interface{}) {
|
|||||||
encoder := xml.NewEncoder(c.Writer)
|
encoder := xml.NewEncoder(c.Writer)
|
||||||
if err := encoder.Encode(obj); err != nil {
|
if err := encoder.Encode(obj); err != nil {
|
||||||
c.Error(err, obj)
|
c.Error(err, obj)
|
||||||
http.Error(c.Writer, err.Error(), 500)
|
http.Error(&c.Writer, err.Error(), 500)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -361,7 +361,7 @@ func (c *Context) HTML(code int, name string, data interface{}) {
|
|||||||
"name": name,
|
"name": name,
|
||||||
"data": data,
|
"data": data,
|
||||||
})
|
})
|
||||||
http.Error(c.Writer, err.Error(), 500)
|
http.Error(&c.Writer, err.Error(), 500)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
19
response_writer.go
Normal file
19
response_writer.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ResponseWriter struct {
|
||||||
|
http.ResponseWriter
|
||||||
|
status int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *ResponseWriter) WriteHeader(code int) {
|
||||||
|
w.status = code
|
||||||
|
w.ResponseWriter.WriteHeader(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *ResponseWriter) Status() int {
|
||||||
|
return w.status
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user